Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2008
    Posts
    397

    Reading File Contents and filtering

    Hi guys,

    Scenario:
    I want to create a level file (yay me, but booo for attempts)
    Ive got an example level text file:

    Code:
    Level
    {
    	name: Trial,
    	Location: "Bin/Level/Trial.obj",
    },
    
    PlayerStart
    {
    	X: 0,
    	Y: 0,
    	Z: 0,
    	size: 0.1, 
    	weapons
    	{		
    	},
    },
    now I have read each line into a vector<string> dataFromFile;
    so its all read in fine, no issues there, its the filtering that im not having issues with,

    I have a start of a struct
    Code:
    struct CurrentLevel
    {
    	char *LevelName;
    	char *LevelLocation;
    		//TODO - Player Target
    		//TODO - Map Target
    		//TODO - actors - undefined size array?(struct in its-self)
    };
    and I want to read and filter the contents of that vector into the structure.

    trying to decide the best way to filter the contents between the { },
    I thought of using regex but I only know that in perl, cant seem to get it correct for c++.
    also tried string comparision but not quite sure how to handle multiple lines.

    anyone any ideas, examples?

    Robert
    Programming:
    C++ (Win32 and MFC), Perl, OpenGL, OpenGL ES 2, Cobol, Javascript/Jquery/ExtJS4, DirectX 10, VB6, XHTML, PHP, CSS, SQL, XML, C# (Basic), Unreal Script (Basic), XNA (Basic)

    Modelling:
    3DsMax, Maya, Zbrush

    Game Engines:
    Unreal Development Kit, Unity

    Other:
    Photoshop, Flash/Scaleform

  2. #2
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    you'll probably not getting away without the use of regular expressions: check out boost.regex, which has become part of the standard with c++11 and is already shipping with some compilers.

    as an alternative, consider using an embeddable scripting language like lua to get rid of the tedious file parsing altogether.

  3. #3
    Join Date
    Nov 2008
    Posts
    397
    hmmm, probably right, completly forgot about lua, that might solve my problems, will let you know im sure thnx
    Programming:
    C++ (Win32 and MFC), Perl, OpenGL, OpenGL ES 2, Cobol, Javascript/Jquery/ExtJS4, DirectX 10, VB6, XHTML, PHP, CSS, SQL, XML, C# (Basic), Unreal Script (Basic), XNA (Basic)

    Modelling:
    3DsMax, Maya, Zbrush

    Game Engines:
    Unreal Development Kit, Unity

    Other:
    Photoshop, Flash/Scaleform

  4. #4
    Join Date
    Oct 2006
    Posts
    1,548
    Also take a look at XML. You can get this going in no time at all with TinyXML.

    Lua is way more powerful though.
    [quote][\quote]

  5. #5
    Join Date
    Mar 2004
    Location
    Anacortes, WA
    Posts
    4,168
    JSON (look at the bottom for links to C++ implementations) is very similar to your syntax, doesn't require a scripting engine and isn't XML (because XML is a terrible, terrible thing).

    Of course, writing a recursive decent lexer/parser for your language would be trivial - as would using ANTLR to write them for you. If you're interested in this sort of thing (it really is a great exercise), check out (this or this) and this. Also, 3dbuzz is releasing a series of videos (albeit in C#) very soon that goes over the parsing and lexing process.
    Need any help? Feel free to PM me - or send an email directly to nelson@3dbuzz.com!

  6. #6
    Join Date
    Oct 2006
    Posts
    1,548
    XML isn't terrible at all.

    For simple projects, its great.
    [quote][\quote]

  7. #7
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    for most intents and purposes, xml and json are exactly the same. of course they use a slightly different syntax, but that's a mere matter of taste. however, xml comes with a couple of features that can make things more complicated. personally, i wouldn't be bothered by that, as they usually do not get in the way when all you want to do is store some data. but if you're looking for the simplest solution, json might be a more reasonable choice.

    data records in lua are also quite similar to json (just as lua the language is quite similar to javascript), with the notable exception that json is just for data description, while lua is still a turing-complete language. this becomes useful when adding procedurally generated portions to your data. for example, if you store the maps for a game level in lua, you can generate parts of your level procedurally without any effort. that might be a total overkill for what you want to do here, but as with xml, it does not get in the way of storing data, so i consider it a valid alternative.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •