Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
17 Oct 2004 1  
Using STL to build a simple DOM model and its scripting scheme.

Contents

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

Introduction

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.

�color�-------------------------------------------------------------------
      <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�-------------------------------------------------------------------
      <Button-state> 107

�Always-on-top�-------------------------------------------------------------
      <if top> 0

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

[ ^ ]

Features

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:
       p.add("window_position");
  2. Add variables to it:
       p.add("window_position","top",50);

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

   p.add("long_section","long_val",1232323);

The proper way is:

   p.add("long_section");
   p.add("long_section","long_val",1232323);

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

  if(p.get("section"))
    p.get("section","long_val",lVar);

To delete a section:

   p.del("section");

To delete a variable

   p.del("section","variable_name");

To delete all:

  p.del_all();

To change a value:

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

To add a comment string:

   p.add_comment("infinite long string");

or

   p.add_comment("string",iPos);

To get a comment string:

  char szBuffer[iEnoughSpace];
  p.get_comment(szBuffer,iPos);

To delete a comment string:

  p.del_comment(iPos);

To parse from a file:

  p.get_file_en_parse("file_path");

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

To save the dom tree to a file:

 p.get_all_en_save("file_path");

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 
	{
private:
	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);

public:
	mParse();
	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);
	//TODO:

	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);
	//TODO:

	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);
	//TODO:

	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

	};

[ ^ ]

Disclaimer

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

[ ^ ]

History

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.

License

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