Results 1 to 14 of 14

Thread: unable to save QCStrings properly in a buffer

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #13
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: unable to save QCStrings properly in a buffer

    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

    Qt Code:
    1. loc = QTextCodec::codecForName("ISO8859-7");//extended ASCII with greek
    2. loc->setCodecForCStrings(loc);
    To copy to clipboard, switch view to plain text mode 

    then the code is ..

    Qt Code:
    1. {
    2. QCString tOT = loc->fromUnicode(tempOperTel);
    3. QCString tON = loc->fromUnicode(tempOperName);
    4. QCString tSN = loc->fromUnicode(tempStatNum);
    5. QCString tSL = loc->fromUnicode(tempStatLoc);
    6. QCString tID = loc->fromUnicode(tempInstallDate);
    7. QCString tMD = loc->fromUnicode(tempMaintenDate);
    8.  
    9. //i have commented the lines below since they didn't show the greek characters
    10. //correctly
    11. /*QCString tOT = tempOperTel.local8Bit();
    12.   QCString tON = tempOperName.local8Bit();
    13.   QCString tSN = tempStatNum.local8Bit();
    14.   QCString tSL = tempStatLoc.local8Bit();
    15.   QCString tID = tempInstallDate.local8Bit();
    16.   QCString tMD = tempMaintenDate.local8Bit();*/
    17.  
    18. int tOTL = tOT.length();
    19. int tONL = tON.length();
    20. int tSNL = tSN.length();
    21. int tSLL = tSL.length();
    22. int tIDL = tID.length();
    23. int tMDL = tMD.length();
    24.  
    25. QCString tmp=tOT+tON+tSN+tSL+tID+tMD;
    26. const char *savingStrs = tmp;
    27. int savingLength = tmp.length() ;
    28.  
    29. //lock the file for writing
    30. memset (&lock, 0, sizeof(lock));
    31. lock.l_type = F_WRLCK;
    32. fcntl (fd, F_SETLKW, &lock);
    33.  
    34. *((char*)file_memory+wOpcodeOffset) = 9;
    35. struct ShMem *infoVar=(ShMem*)((char*)file_memory);
    36.  
    37. infoVar->oTL = tOTL;
    38. infoVar->oNL = tONL;
    39. infoVar->sNL = tSNL;
    40. infoVar->sLL = tSLL;
    41. infoVar->iDL = tIDL;
    42. infoVar->mDL = tMDL;
    43. memcpy(&(infoVar->oAndS_str), savingStrs, savingLength);
    44.  
    45. //unlock the file to allow access
    46. lock.l_type = F_UNLCK;
    47. fcntl (fd, F_SETLKW, &lock);
    48. }
    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:


    Qt Code:
    1. {
    2. int tOTL=0;
    3. int tONL=0;
    4. int tSNL=0;
    5. int tSLL=0;
    6. int tIDL=0;
    7. int tMDL=0;
    8. int readingLength;
    9.  
    10. //lock the file for writing
    11. memset (&lock, 0, sizeof(lock));
    12. lock.l_type = F_WRLCK;
    13. fcntl (fd, F_SETLKW, &lock);
    14.  
    15. struct ShMem *infoVar=(ShMem*)((char*)file_memory);
    16.  
    17. tOTL = infoVar->oTL;
    18. tONL = infoVar->oNL;
    19. tSNL = infoVar->sNL;
    20. tSLL = infoVar->sLL;
    21. tIDL = infoVar->iDL;
    22. tMDL = infoVar->mDL;
    23. readingLength = tOTL + tONL + tSNL + tSLL + tIDL + tMDL ;
    24. char readingStrs[readingLength];
    25.  
    26. memcpy(&readingStrs, &(infoVar->oAndS_str), readingLength);
    27.  
    28. //unlock the file to allow access
    29. lock.l_type = F_UNLCK;
    30. fcntl (fd, F_SETLKW, &lock);
    31.  
    32. char tOT[tOTL+1]; memcpy(tOT,readingStrs,tOTL); tOT[tOTL]='\0';
    33. char tON[tONL+1]; memcpy(tON,(readingStrs+tOTL),tONL); tON[tONL]='\0';
    34. char tSN[tSNL+1]; memcpy(tSN,(readingStrs+tOTL+tONL),tSNL); tSN[tSNL]='\0';
    35. char tSL[tSLL+1]; memcpy(tSL,(readingStrs+tOTL+tONL+tSNL),tSLL); tSL[tSLL]='\0';
    36. char tID[tIDL+1]; memcpy(tID,(readingStrs+tOTL+tONL+tSNL+tSLL),tIDL); tID[tIDL]='\0';
    37. char tMD[tMDL+1]; memcpy(tMD,(readingStrs+tOTL+tONL+tSNL+tSLL+tIDL),tMDL); tMD[tMDL]='\0';
    38.  
    39. operatorTel = loc->toUnicode(tOT);
    40. operatorName = loc->toUnicode(tON);
    41. stationNum = loc->toUnicode(tSN);
    42. stationLoc = loc->toUnicode(tSL);
    43. installationDate = loc->toUnicode(tID);
    44. maintenanceDate = loc->toUnicode(tMD);
    45.  
    46. //do smth now
    47. }
    To copy to clipboard, switch view to plain text mode 

    am i using QTextCodec correctly?

    now for saving to File :

    Qt Code:
    1. void monitorForm::writeToGuiFile()
    2. {
    3. QCString buffer=loc->fromUnicode(stationNum)+"\nEOV\n"+loc->fromUnicode(stationLoc)+"\nEOV\n"+
    4. loc->fromUnicode(operatorName)+"\nEOV\n"+loc->fromUnicode(operatorTel)+"\nEOV\n"+
    5. loc->fromUnicode(installationDate)+"\nEOV\n"+loc->fromUnicode(maintenanceDate)+"\nEOV\n";
    6. QFile file(guiFileName);
    7. if ( file.open( IO_WriteOnly ) )
    8. {
    9. QTextStream stream( &file );
    10. stream<<buffer;
    11. }
    12. file.close();
    13. }
    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

    Qt Code:
    1. int monitorForm::readFromGuiFile()
    2. {
    3. QFile file(guiFileName);
    4. QCString lines[6]="";
    5. QCString curLine="";
    6. QCString sumLine="";
    7. int i=0;
    8. if ( file.open( IO_ReadOnly ) )
    9. {
    10. QTextStream stream( &file );
    11. while (!stream.atEnd())
    12. {
    13. curLine=stream.readLine();
    14. if ((curLine!="EOV"))
    15. {
    16. if (curLine!="")
    17. sumLine=sumLine+curLine+"\n";
    18. }
    19. else
    20. {
    21. lines[i]=sumLine;
    22. sumLine="";
    23. i++;
    24. }
    25. }//while
    26. file.close();
    27.  
    28. //remove the last char which is always \n due to the way
    29. //the file open parser (just above) works
    30. lines[0].remove( (lines[0].length()-1) ,1);
    31. lines[1].remove( (lines[1].length()-1) ,1);
    32. lines[2].remove( (lines[2].length()-1) ,1);
    33. lines[3].remove( (lines[3].length()-1) ,1);
    34. lines[4].remove( (lines[4].length()-1) ,1);
    35. lines[5].remove( (lines[5].length()-1) ,1);
    36.  
    37. stationNum=loc->toUnicode(lines[0]);
    38. stationLoc=loc->toUnicode(lines[1]);
    39. operatorName=loc->toUnicode(lines[2]);
    40. operatorTel=loc->toUnicode(lines[3]);
    41. installationDate=loc->toUnicode(lines[4]);
    42. maintenanceDate=loc->toUnicode(lines[5]);
    43.  
    44. return (0);
    45. }
    46. else
    47. return (1);
    48.  
    49. }
    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?
    Last edited by jacek; 15th November 2006 at 20:39. Reason: wrapped too long line

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.