Results 1 to 12 of 12

Thread: Parsing Text File --> Guide Needed

  1. #1
    Join Date
    Oct 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Parsing Text File --> Guide Needed

    Hi All,

    it my pleasure to join the Qt Center. I really enjoy programming with Qt.
    But I have some problems in reading text file and processing the data. I need your guide to help in my issue and improving my skills in
    programming.

    My issue is I have read a file with a standard structure format, which is:
    Qt Code:
    1. Date Aug-01-1962
    2. START
    3. &ITEM Name='A' MAX= 4322.00 /
    4. &ITEM Name='BB' MAX= 2323.00 /
    5. &ITEM Name='BBB' MAX= 6183.00 /
    6. &ITEM Name='BBA' MAX= 1383.00 /
    7. &ITEM Name='ABB' MAX= 1407.00 /
    8. &ITEM Name='DDA' MAX= 1371.00 /
    9. &ITEM Name='AFF' MAX= 5785.00 /
    10. END
    To copy to clipboard, switch view to plain text mode 

    I have to item name and its MAX value or other values and processing them and have ability to change these values in the original text file.
    Could please help how to token each line and check how many values there and get them separately in effective way.

    Your help is appreciated,
    Ali

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Parsing Text File --> Guide Needed

    Hard to say without seeing the grammar for the input...

    for simple stuff:
    read the file line by line;
    match the lines against regexs;
    add what you gathered from the parsed/matched line to your parse state;
    perhaps you might need a state machine if the input is more complex
    (to detect things like: here should have been the "START" token, but I got a "&ITEM" instead)

    for more complicated stuff:
    write a real parser (e.g. recursive descent)

    HTH

  3. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parsing Text File --> Guide Needed

    A sample regexp that could help you to extract data from files of the specified format (provided there are no extra features of the format you did not mention... as caduel said, a more precise grammar would help) :

    Qt Code:
    1. QRegExp pattern("\\s+&INDEX\\s+(?:(\\w+)=\\s*(\'[^\']+\'|\\S+)\\s+)+/")
    To copy to clipboard, switch view to plain text mode 
    You can match this regexp against every line of your input. If it matches then you can extract data using the list of capturedTexts() (item n is the attribute name (e.g Name or MAX in your example) and n+1 is the corresponding value, as a quoted string or a sequence of non-whitespace characters, n starting from 1) :

    Qt Code:
    1. // supposing pattern has been matched against a string you can iterate as follow :
    2. int max = pattern.numCaptures() - 1;
    3. QStringList caps = pattern.capturedTexts();
    4. for ( int i = 1; i < max; i += 2 )
    5. {
    6. QString attributeName = caps.at(i);
    7. QString attributeValue = caps.at(i + 1);
    8.  
    9. // do something with that data now
    10. }
    To copy to clipboard, switch view to plain text mode 
    Note : I have not tested this code so there might be a compilation error left but the principle is there.
    Current Qt projects : QCodeEdit, RotiDeCode

  4. #4
    Join Date
    Oct 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Parsing Text File --> Guide Needed

    Thanks caduel

    it is great idea but very advance for my application, but later it may help me.

    Thanks fullmetalcoder

    That is what I have tried and work with me as I want.


    Thanks for your response.
    Ali

  5. #5
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parsing Text File --> Guide Needed

    If you're not locked in to the file format, if you change your file format to XML you may have an easier time parsing using QT's built in XML and DOM classes

  6. #6
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parsing Text File --> Guide Needed

    Not such a good advice really. XML is a very "trendy" format lately. Many people use it everywhere, mostly to do things it has not been designed for in the first place, just because it saves them the effort of writing proper loading/saving routines themselves. XML is great, no doubt, but it is a design error (though it might be a decent fallback choice for time reasons) to use it for, say, storing settings or any kind of data that do not match the following criteria :

    • REQUIRES flexible hierarchy
    • makes the XML signal/noise ratio low, i.e the space taken by tags is WAY smaller than that taken by the actual data (typically, such data fall in the "documents" category)

    Also, it is worth keeping in mind that most XML parsers, while being easy to use are not lightning fast and that even the fastest will be significantly slower that a parser for a simpler language (may it be homemade or something like JSON or another xml alternative...)
    Current Qt projects : QCodeEdit, RotiDeCode

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Parsing Text File --> Guide Needed

    I sense a flamework coming

    I tend to disagree about XML. STN ratio doesn't matter as storage and bandwith are becoming cheaper and cheaper and besides XML yields an excellent compression ratio. As for settings, it definitely IS something that requires flexible hierarchy if you plan future enhancements - back-and-forth compatibility is then trivial.

    On the other hand I do agree that this notation is used much too often nowadays.

  8. #8
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parsing Text File --> Guide Needed

    Quote Originally Posted by wysota View Post
    I sense a flamework coming
    Not from here. I agree completely on speed and STN.

    Based on my own usage pattern, I suspect its overused because its easy to get up and running quickly and easy to modify later.

    I was just pointing out the existence of the classes in case it fit the bill.

    Probably all moot since he does refer to the data files being in a standard format, with no suggestion he can alter the file format.

  9. #9
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parsing Text File --> Guide Needed

    Quote Originally Posted by wysota View Post
    As for settings, it definitely IS something that requires flexible hierarchy if you plan future enhancements - back-and-forth compatibility is then trivial.
    Well, of course it depends what kind of settings you need to store. Quite often, flat text files can actually be enough and if hierarchy is needed it can still be achieved using indentation or a similar method.

    Anyway, we're getting off topic...
    Current Qt projects : QCodeEdit, RotiDeCode

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Parsing Text File --> Guide Needed

    Quote Originally Posted by fullmetalcoder View Post
    Quite often, flat text files can actually be enough and if hierarchy is needed it can still be achieved using indentation or a similar method.
    I more meant that version A introduces parameter X which version A-1 can't handle so when you move settings from A to A-1, it will or will not preserve parameter X and when you run version A back, the setting will or will not be there. It works the other way round with the default values as well. Hmm... I don't know if what I have written is possible to understand, sorry

    Anyway, we're getting off topic...
    Right

  11. #11
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parsing Text File --> Guide Needed

    Quote Originally Posted by wysota View Post
    I more meant that version A introduces parameter X which version A-1 can't handle so when you move settings from A to A-1, it will or will not preserve parameter X and when you run version A back, the setting will or will not be there. It works the other way round with the default values as well. Hmm... I don't know if what I have written is possible to understand, sorry
    What you say is clear (at least to me) but wrong.The forward/backward compatibility is NOT specific to the backend used (XML, JSON, "flat" text, ...), as long as it is structured, but to the way the app handles it.

    Whathever the format used, some applications read through the data and apply the settings on start and then overwrite it when saving settings (that would be using Qt xml stream reading/writing for instance).

    Some however, whatever the format used, will convert it to an internal data structure in a lossless, and only apply settings they recognize (that would be using QDomDocument for instance).
    Current Qt projects : QCodeEdit, RotiDeCode

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Parsing Text File --> Guide Needed

    Quote Originally Posted by fullmetalcoder View Post
    What you say is clear (at least to me) but wrong.The forward/backward compatibility is NOT specific to the backend used (XML, JSON, "flat" text, ...), as long as it is structured, but to the way the app handles it.
    Yes, that's true. But what I mean is that you don't need to do anything in your application to support it when using XML, as long as you don't regenerate the tree from scratch. With other backends this is also possible but not that straightforward.

    Some however, whatever the format used, will convert it to an internal data structure in a lossless, and only apply settings they recognize (that would be using QDomDocument for instance).
    There is a good chance things like comments in the settings file will be lost when converting back and forth. But as I said - I mean this is very easy with XML and a little harder with "other" backends.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Opening text file fails after autostartup on windows
    By yogourta in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2008, 03:52
  3. Replies: 1
    Last Post: 3rd September 2008, 14:16
  4. Text file to PDF convertion using C++ code
    By joseph in forum General Discussion
    Replies: 2
    Last Post: 21st August 2008, 01:28
  5. Text file parsing too slow
    By Potch in forum Newbie
    Replies: 5
    Last Post: 11th July 2008, 21:16

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.