there is one final (hopefully!) problem i am having however. if i save greek chars on a QFile using streaming operations and re-read them i get ???? (as many ? as there where supposed to be greek chars).
in the beginning i would get crazy QCString lengths (for 3 greek chars i'd get a length of 24 or smth), so i played around with QTextCodec abit and managed to make - at least measuring the length correctly - work. Then i remove from aQLabel->setText() the QString::fromLocal8Bit function and on saving and reading from shared memory the characters appear and work as expected. Here is the code that DOES work for the shared memory. Then i will post the write - read to QFile functions.
here is the saving code for shared memory:
the loc variable is QTextCodec * , declared as private variable in this form and initialized in init() as
loc
= QTextCodec::codecForName("ISO8859-7");
//extended ASCII with greek loc->setCodecForCStrings(loc);
loc = QTextCodec::codecForName("ISO8859-7");//extended ASCII with greek
loc->setCodecForCStrings(loc);
To copy to clipboard, switch view to plain text mode
then the code is ..
{
QCString tOT = loc->fromUnicode(tempOperTel);
QCString tON = loc->fromUnicode(tempOperName);
QCString tSN = loc->fromUnicode(tempStatNum);
QCString tSL = loc->fromUnicode(tempStatLoc);
QCString tID = loc->fromUnicode(tempInstallDate);
QCString tMD = loc->fromUnicode(tempMaintenDate);
//i have commented the lines below since they didn't show the greek characters
//correctly
/*QCString tOT = tempOperTel.local8Bit();
QCString tON = tempOperName.local8Bit();
QCString tSN = tempStatNum.local8Bit();
QCString tSL = tempStatLoc.local8Bit();
QCString tID = tempInstallDate.local8Bit();
QCString tMD = tempMaintenDate.local8Bit();*/
int tOTL = tOT.length();
int tONL = tON.length();
int tSNL = tSN.length();
int tSLL = tSL.length();
int tIDL = tID.length();
int tMDL = tMD.length();
QCString tmp=tOT+tON+tSN+tSL+tID+tMD;
const char *savingStrs = tmp;
int savingLength = tmp.length() ;
//lock the file for writing
memset (&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
fcntl (fd, F_SETLKW, &lock);
*((char*)file_memory+wOpcodeOffset) = 9;
struct ShMem *infoVar=(ShMem*)((char*)file_memory);
infoVar->oTL = tOTL;
infoVar->oNL = tONL;
infoVar->sNL = tSNL;
infoVar->sLL = tSLL;
infoVar->iDL = tIDL;
infoVar->mDL = tMDL;
memcpy(&(infoVar->oAndS_str), savingStrs, savingLength);
//unlock the file to allow access
lock.l_type = F_UNLCK;
fcntl (fd, F_SETLKW, &lock);
}
{
QCString tOT = loc->fromUnicode(tempOperTel);
QCString tON = loc->fromUnicode(tempOperName);
QCString tSN = loc->fromUnicode(tempStatNum);
QCString tSL = loc->fromUnicode(tempStatLoc);
QCString tID = loc->fromUnicode(tempInstallDate);
QCString tMD = loc->fromUnicode(tempMaintenDate);
//i have commented the lines below since they didn't show the greek characters
//correctly
/*QCString tOT = tempOperTel.local8Bit();
QCString tON = tempOperName.local8Bit();
QCString tSN = tempStatNum.local8Bit();
QCString tSL = tempStatLoc.local8Bit();
QCString tID = tempInstallDate.local8Bit();
QCString tMD = tempMaintenDate.local8Bit();*/
int tOTL = tOT.length();
int tONL = tON.length();
int tSNL = tSN.length();
int tSLL = tSL.length();
int tIDL = tID.length();
int tMDL = tMD.length();
QCString tmp=tOT+tON+tSN+tSL+tID+tMD;
const char *savingStrs = tmp;
int savingLength = tmp.length() ;
//lock the file for writing
memset (&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
fcntl (fd, F_SETLKW, &lock);
*((char*)file_memory+wOpcodeOffset) = 9;
struct ShMem *infoVar=(ShMem*)((char*)file_memory);
infoVar->oTL = tOTL;
infoVar->oNL = tONL;
infoVar->sNL = tSNL;
infoVar->sLL = tSLL;
infoVar->iDL = tIDL;
infoVar->mDL = tMDL;
memcpy(&(infoVar->oAndS_str), savingStrs, savingLength);
//unlock the file to allow access
lock.l_type = F_UNLCK;
fcntl (fd, F_SETLKW, &lock);
}
To copy to clipboard, switch view to plain text mode
and then i reuse the QTextCodec to read the data from the shared memory:
the reading code for shared memory is:
{
int tOTL=0;
int tONL=0;
int tSNL=0;
int tSLL=0;
int tIDL=0;
int tMDL=0;
int readingLength;
//lock the file for writing
memset (&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
fcntl (fd, F_SETLKW, &lock);
struct ShMem *infoVar=(ShMem*)((char*)file_memory);
tOTL = infoVar->oTL;
tONL = infoVar->oNL;
tSNL = infoVar->sNL;
tSLL = infoVar->sLL;
tIDL = infoVar->iDL;
tMDL = infoVar->mDL;
readingLength = tOTL + tONL + tSNL + tSLL + tIDL + tMDL ;
char readingStrs[readingLength];
memcpy(&readingStrs, &(infoVar->oAndS_str), readingLength);
//unlock the file to allow access
lock.l_type = F_UNLCK;
fcntl (fd, F_SETLKW, &lock);
char tOT[tOTL+1]; memcpy(tOT,readingStrs,tOTL); tOT[tOTL]='\0';
char tON[tONL+1]; memcpy(tON,(readingStrs+tOTL),tONL); tON[tONL]='\0';
char tSN[tSNL+1]; memcpy(tSN,(readingStrs+tOTL+tONL),tSNL); tSN[tSNL]='\0';
char tSL[tSLL+1]; memcpy(tSL,(readingStrs+tOTL+tONL+tSNL),tSLL); tSL[tSLL]='\0';
char tID[tIDL+1]; memcpy(tID,(readingStrs+tOTL+tONL+tSNL+tSLL),tIDL); tID[tIDL]='\0';
char tMD[tMDL+1]; memcpy(tMD,(readingStrs+tOTL+tONL+tSNL+tSLL+tIDL),tMDL); tMD[tMDL]='\0';
operatorTel = loc->toUnicode(tOT);
operatorName = loc->toUnicode(tON);
stationNum = loc->toUnicode(tSN);
stationLoc = loc->toUnicode(tSL);
installationDate = loc->toUnicode(tID);
maintenanceDate = loc->toUnicode(tMD);
//do smth now
}
{
int tOTL=0;
int tONL=0;
int tSNL=0;
int tSLL=0;
int tIDL=0;
int tMDL=0;
int readingLength;
//lock the file for writing
memset (&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
fcntl (fd, F_SETLKW, &lock);
struct ShMem *infoVar=(ShMem*)((char*)file_memory);
tOTL = infoVar->oTL;
tONL = infoVar->oNL;
tSNL = infoVar->sNL;
tSLL = infoVar->sLL;
tIDL = infoVar->iDL;
tMDL = infoVar->mDL;
readingLength = tOTL + tONL + tSNL + tSLL + tIDL + tMDL ;
char readingStrs[readingLength];
memcpy(&readingStrs, &(infoVar->oAndS_str), readingLength);
//unlock the file to allow access
lock.l_type = F_UNLCK;
fcntl (fd, F_SETLKW, &lock);
char tOT[tOTL+1]; memcpy(tOT,readingStrs,tOTL); tOT[tOTL]='\0';
char tON[tONL+1]; memcpy(tON,(readingStrs+tOTL),tONL); tON[tONL]='\0';
char tSN[tSNL+1]; memcpy(tSN,(readingStrs+tOTL+tONL),tSNL); tSN[tSNL]='\0';
char tSL[tSLL+1]; memcpy(tSL,(readingStrs+tOTL+tONL+tSNL),tSLL); tSL[tSLL]='\0';
char tID[tIDL+1]; memcpy(tID,(readingStrs+tOTL+tONL+tSNL+tSLL),tIDL); tID[tIDL]='\0';
char tMD[tMDL+1]; memcpy(tMD,(readingStrs+tOTL+tONL+tSNL+tSLL+tIDL),tMDL); tMD[tMDL]='\0';
operatorTel = loc->toUnicode(tOT);
operatorName = loc->toUnicode(tON);
stationNum = loc->toUnicode(tSN);
stationLoc = loc->toUnicode(tSL);
installationDate = loc->toUnicode(tID);
maintenanceDate = loc->toUnicode(tMD);
//do smth now
}
To copy to clipboard, switch view to plain text mode
am i using QTextCodec correctly?
now for saving to File :
void monitorForm::writeToGuiFile()
{
QCString buffer=loc->fromUnicode(stationNum)+"\nEOV\n"+loc->fromUnicode(stationLoc)+"\nEOV\n"+
loc->fromUnicode(operatorName)+"\nEOV\n"+loc->fromUnicode(operatorTel)+"\nEOV\n"+
loc->fromUnicode(installationDate)+"\nEOV\n"+loc->fromUnicode(maintenanceDate)+"\nEOV\n";
if ( file.open( IO_WriteOnly ) )
{
stream<<buffer;
}
file.close();
}
void monitorForm::writeToGuiFile()
{
QCString buffer=loc->fromUnicode(stationNum)+"\nEOV\n"+loc->fromUnicode(stationLoc)+"\nEOV\n"+
loc->fromUnicode(operatorName)+"\nEOV\n"+loc->fromUnicode(operatorTel)+"\nEOV\n"+
loc->fromUnicode(installationDate)+"\nEOV\n"+loc->fromUnicode(maintenanceDate)+"\nEOV\n";
QFile file(guiFileName);
if ( file.open( IO_WriteOnly ) )
{
QTextStream stream( &file );
stream<<buffer;
}
file.close();
}
To copy to clipboard, switch view to plain text mode
if say QString operatorName is in greek, and i check the text file after the operation, i observe ??? instead of the text. So if i manually erase the ??? and put a greek word back in, and i read the file from my code, the data is shown correctly in the QLabels.
here is the reading QFile function
int monitorForm::readFromGuiFile()
{
QCString lines[6]="";
QCString curLine="";
QCString sumLine="";
int i=0;
if ( file.open( IO_ReadOnly ) )
{
while (!stream.atEnd())
{
curLine=stream.readLine();
if ((curLine!="EOV"))
{
if (curLine!="")
sumLine=sumLine+curLine+"\n";
}
else
{
lines[i]=sumLine;
sumLine="";
i++;
}
}//while
file.close();
//remove the last char which is always \n due to the way
//the file open parser (just above) works
lines[0].remove( (lines[0].length()-1) ,1);
lines[1].remove( (lines[1].length()-1) ,1);
lines[2].remove( (lines[2].length()-1) ,1);
lines[3].remove( (lines[3].length()-1) ,1);
lines[4].remove( (lines[4].length()-1) ,1);
lines[5].remove( (lines[5].length()-1) ,1);
stationNum=loc->toUnicode(lines[0]);
stationLoc=loc->toUnicode(lines[1]);
operatorName=loc->toUnicode(lines[2]);
operatorTel=loc->toUnicode(lines[3]);
installationDate=loc->toUnicode(lines[4]);
maintenanceDate=loc->toUnicode(lines[5]);
return (0);
}
else
return (1);
}
int monitorForm::readFromGuiFile()
{
QFile file(guiFileName);
QCString lines[6]="";
QCString curLine="";
QCString sumLine="";
int i=0;
if ( file.open( IO_ReadOnly ) )
{
QTextStream stream( &file );
while (!stream.atEnd())
{
curLine=stream.readLine();
if ((curLine!="EOV"))
{
if (curLine!="")
sumLine=sumLine+curLine+"\n";
}
else
{
lines[i]=sumLine;
sumLine="";
i++;
}
}//while
file.close();
//remove the last char which is always \n due to the way
//the file open parser (just above) works
lines[0].remove( (lines[0].length()-1) ,1);
lines[1].remove( (lines[1].length()-1) ,1);
lines[2].remove( (lines[2].length()-1) ,1);
lines[3].remove( (lines[3].length()-1) ,1);
lines[4].remove( (lines[4].length()-1) ,1);
lines[5].remove( (lines[5].length()-1) ,1);
stationNum=loc->toUnicode(lines[0]);
stationLoc=loc->toUnicode(lines[1]);
operatorName=loc->toUnicode(lines[2]);
operatorTel=loc->toUnicode(lines[3]);
installationDate=loc->toUnicode(lines[4]);
maintenanceDate=loc->toUnicode(lines[5]);
return (0);
}
else
return (1);
}
To copy to clipboard, switch view to plain text mode
i have written it like this since the variable stationLoc might contain \n character since it stores an address. so i needed separator entries (EOV= end of variable).
so any ideas what might be wrong with saving?
Bookmarks