Click here to Skip to main content
12,884,110 members (34,080 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 17 Oct 2004

Using STL to build a simple DOM model and its scripting scheme

, 17 Oct 2004
Rate this:
Please Sign up or sign in to vote.
Using STL to build a simple DOM model and its scripting scheme.


  1. Introduction.
  2. TextFile.
  3. Features.
  4. How it works.
  5. Internal Working.
  6. Disclaimer.
  7. History.
[ ^ ]


STL provides with a very rich set of containers for storing objects dynamically. The code so built upon it is platform independent. While learning STL two years back I began to code some thing and it turned out to be a scripting scheme to store settings like .ini files. Here the code may or may not be perfect but I have made it to work error free. Yes, it runs on VC 6, 7.0 and may run on VC 7.1, Intel, Borland 5.5, and Borland Builder, Mingw port of GCC, GCC, Digital Mars and host of others…

It demonstrates building a unique DOM model parsing a text file. It is a tree with three levels of storage. The text file generated is as shown below:

[ ^ ]

Text file

This is a typical scripting scheme by which the DOM tree is saved to a file.

This is the database file used by the color C++ dialog box. Don't edit this. This is highly configurable and replaces the ini and registry system of Windows. For more information see about.

      <c-val:0> 264754
      <c-val:1> 792892
      <c-val:2> 1321030
      <c-val:3> 1849168
      <c-val:4> 2377306
      <c-val:5> 2905444
      <c-val:6> 3433582
      <c-val:7> 3961720
      <c-val:8> 4489858
      <c-val:9> 5017996
      <c-val:10> 5546134
      <c-val:11> 6074272
      <c-val:12> 6602410
      <c-val:13> 7130548
      <c-val:14> 7658686
      <c-val:15> 8186824
      <RGB-init> 0

      <Button-state> 107

      <if top> 0

      <TopMax-x> -1
      <TopMax-y> -1
      <TopMin-x> -1
      <TopMin-y> -1
      <Rect-left> 490
      <Rect-top> 196
      <Rect-right> 690
      <Rect-bottom> 378

[ ^ ]


It can store infinite length string as comments in side the tree. It has overloaded functions for storing and retrieving values for different data type, i.e. only one function call can be used to retrieve an integer, floating point value, or string listing for that number. It has good error correction built into it.

Since it uses file streams of STL and all the functions used for it are from Standard C++ library it is platform independent. I.e. it will run on Win32, Linux, Mac, HP-UNIX, and ….

Since it is open source with no strings attached you can modify, delete, remove, add, replace, dump it, reject it, and even can claim it as yours.

[ ^ ]

How it works (for users)

First create an object of class parser:

parser p;

To add variables to it:

  1. First add a section:
  2. Add variables to it:

If the following line is used, it will return an error since the section is not present.


The proper way is:


To get variables from it, you can check for the section first.


To delete a section:


To delete a variable


To delete all:


To change a value:

p.set("section","variable_name", new_value);

To add a comment string:

p.add_comment("infinite long string");



To get a comment string:

char szBuffer[iEnoughSpace];

To delete a comment string:


To parse from a file:


If there is an error while parsing, you will get an error.

To save the dom tree to a file:


Actually you can even add objects by casting them as (char*) and appending a 0.

[ ^ ]

Internal working

It uses three types of objects. First object stores value name and value (class vh), second object stores the first object and comment string (class section) and the third object stores second objects and other functions. (class mParse). Other classes and members are self explanatory.

The mParse class. 
class mParse 
	list <section, allocator<section> > root;
	section _section;
	HANDLE hFile;
	char *fname;//path+file name
	unsigned char *buffer;
	DWORD dwBufferSize;
	bool condense_to_buffer(void);
	bool open_file(void);//just gets the buffer from given file
	bool free_buffer(void);//generic file open and close
	bool phrase_from_file(void);

	bool get_file_en_parse(char *szFname);
	bool get_all_en_save(char *szFname);

	bool add(char *szSection, char *szName, char *szValue);
	bool add(char *szSection, char *szName, bool bValue);
	bool add(char *szSection, char *szName, int iVlaue);
	bool add(char *szSection, char *szName, unsigned int uiValue)
		int _iTemp = uiValue;//make it signed
		bool bRetval = add(szSection, szName, _iTemp);
		return bRetval;
	bool add(char *szSection, char *szName, long lValue);
	bool add(char *szSection, char *szName, float fValue);
	bool add(char *szSection, char *szName, double dValue);
	bool add(char *szSection, char *szName, DWORD dwValue);

	bool add(char *szSection);
	bool del(char *szSection);
	bool del(char *szSection, char *szName);
	bool del_all(char *szSection, char *szName);

	bool get(char *szSection);//checks if the section is available 
	bool get(char *szSection, char *szName, char *szReturnValue);
	bool get(char *szSection, char *szName, bool &bReturnValue);
	bool get(char *szSection, char *szName, int &iReturnValue);
	bool get(char *szSection, char *szName, unsigned int &uiReturnValue)
		int _iTemp = 0;
		bool bRetval = get(szSection, szName, _iTemp);
		uiReturnValue = _iTemp;
		return bRetval;
	bool get(char *szSection, char *szName, long &lReturnValue);
	bool get(char *szSection, char *szName, float &fReturnValue);
	bool get(char *szSection, char *szName, double &dReturnValue);
	bool get(char *szSection, char *szName, DWORD &dwReturnValue);
	bool get(char *szSection, char *szName, 
	     char *szReturnValue, int iValuNo); 

	bool set(char *szSection, char *szName, char *szValue);
	bool set(char *szSection, char *szName, bool bValue);
	bool set(char *szSection, char *szName, int iValue);
	bool set(char *szSection, char *szName, unsigned int iValue)
		int _iTemp = iValue;
		return set(szSection, szName, _iTemp);
	bool set(char *szSection, char *szName, long lValue);
	bool set(char *szSection, char *szName, float fValue);
	bool set(char *szSection, char *szName, double dValue);
	bool set(char *szSection, char *szName, DWORD dwValue);
	bool set(char *szSection, char *szName, char *szValue, int iValuNo);

	//binary data related
	bool add_binary(char *szSection, char *szName, 
	  void *data, long data_size);
	bool get_binary(char *szSection, char *szName, 
	  void *data, long data_size);
	bool set_binary(char *szSection, char *szName, 
	  void *data, long data_size);
	bool del_binary(char *szSection, char *szNames); 

	//comment related
	void add_comment(char *szComment);
	void add_comment(char *szComment, int iInsertPos); 
	bool get_comment(char *szReturnValue, int iCommentNo);
	bool replace_comment(char *szReplaceString, int iCommentNo);
	bool del_comment(int iCommentNo);

	void del_all(void); //destroys the entire mParse

[ ^ ]


It is only a demonstration and it may not suit particular requirements and for use in a program.

[ ^ ]


It was a small program written two years back. It may contain errors and may have not used STL containers efficiently. If there is some mistake in writing and the program, it is regretted.

It is my first article on CodeProject...

I am currently looking for some persons to join in my open source project. If any one wants to contribute he/she is most welcome. My project is not personal and every one is invited... I am developing a multiplatform IDE for C/C++ with its own class lib, APIs and classes using pure C/C++.

Please visit SourceForge for details.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralPlease reformat this! Pin
Jim Crafton18-Oct-04 8:28
memberJim Crafton18-Oct-04 8:28 
GeneralRe: Please reformat this! Pin
Sanjit Rath19-Oct-04 8:09
memberSanjit Rath19-Oct-04 8:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170422.1 | Last Updated 17 Oct 2004
Article Copyright 2004 by sanjit_rath
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid