Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C++

Building a simple C++ script compiler from Scintilla and CINT

Rate me:
Please Sign up or sign in to vote.
4.74/5 (26 votes)
8 Jul 2006CPOL7 min read 154.4K   7.6K   85  
How to build a simple C++ script compiler from Scintilla and CINT.
/*********************************************************************
* strstream.h
*
*********************************************************************/

#pragma if !defined(G__STRSTREAM_H) && !defined(G__SSTREAM_H)

#include <iostream.h>

#ifndef G__STRSTREAM_H
#define G__STRSTREAM_H


/*********************************************************************
* ostrstream, istrstream
*
*********************************************************************/

class strstream;

class ostrstream {
	char *buf;
	int size;
	int point;
      public:
	ostrstream(char *p,int bufsize);

	ostrstream& operator <<(char c);
	ostrstream& operator <<(char *s);
	ostrstream& operator <<(long i);
	ostrstream& operator <<(unsigned long i);
	ostrstream& operator <<(double d);
	ostrstream& operator <<(void *p);
};

ostrstream::ostrstream(char *p,int bufsize)
{
	if(p==NULL) {
		fprintf(stderr,"NULL pointer given to istrstream\n");
		return;
	}
	buf=p;
	size=bufsize;
	point=0;
}

ostrstream& ostrstream::operator <<(char c)
{
	if(point+1<size-1) sprintf(buf+point,"%c",c);
	return(*this);
}

ostrstream& ostrstream::operator <<(char *s)
{
	int len;
	if(point+strlen(s)<size-1) sprintf(buf+point,"%s",s);
	return(*this);
}

ostrstream& ostrstream::operator <<(long i)
{
	char temp[50];
	sprintf(temp,"%d",i);
	if(point+strlen(temp)<size-1) sprintf(buf+point,"%s",temp);
	return(*this);
}

ostrstream& ostrstream::operator <<(unsigned long i)
{
	char temp[50];
	sprintf(temp,"%u",i);
	if(point+strlen(temp)<size-1) sprintf(buf+point,"%s",temp);
	return(*this);
}

ostrstream& ostrstream::operator <<(double d)
{
	char temp[50];
	sprintf(temp,"%g",d);
	if(point+strlen(temp)<size-1) sprintf(buf+point,"%s",temp);
	return(*this);
}

ostrstream& ostrstream::operator <<(void *p)
{
	char temp[50];
	sprintf(temp,"0x%x",p);
	if(point+strlen(temp)<size-1) sprintf(buf+point,"%s",temp);
	return(*this);
}



/*********************************************************************
* istrstream
*
* NOT COMPLETE
*********************************************************************/

class istrstream {
	char *buf;
	int size;
	int point;
      public:
	istrstream(char *p,int bufsize);

	istrstream& operator >>(char& c);
	istrstream& operator >>(char *s);
	istrstream& operator >>(short& s);
	istrstream& operator >>(int& i);
	istrstream& operator >>(long i);
	istrstream& operator >>(unsigned char& c);
	istrstream& operator >>(unsigned short& s);
	istrstream& operator >>(unsigned int i);
	istrstream& operator >>(unsigned long i);
	istrstream& operator >>(double d);
	istrstream& operator >>(float d);
};

istrstream::istrstream(char *p,int bufsize)
{
	if(p==NULL) {
		fprintf(stderr,"NULL pointer given to istrstream\n");
	}
	buf=p;
	size=bufsize;
	point=0;
}


istrstream& istrstream::operator >>(char& c)
{
	sscanf(buf+point,"%c",&c);
	++point;
	return(*this);
}

istrstream& istrstream::operator >>(char *s)
{
	sscanf(buf+point,"%s",s);
	point += strlen(s);
	return(*this);
}

istrstream& istrstream::operator >>(short& s)
{
	sscanf(buf+point,"%hd",&s);
	return(*this);
}

istrstream& istrstream::operator >>(int& i)
{
	sscanf(buf+point,"%d",&i);
	return(*this);
}

istrstream& istrstream::operator >>(long& i)
{
	sscanf(buf+point,"%ld",&i);
	return(*this);
}

istrstream& istrstream::operator >>(unsigned char& c)
{
	int i;
	sscanf(buf+point,"%u",&i);
	c = i;
	return(*this);
}
istrstream& istrstream::operator >>(unsigned short& s)
{
	sscanf(buf+point,"%hu",&s);
	return(*this);
}
istrstream& istrstream::operator >>(unsigned int& i)
{
	sscanf(buf+point,"%u",&i);
	return(*this);
}
istrstream& istrstream::operator >>(unsigned long& i)
{
	sscanf(buf+point,"%lu",&i);
	return(*this);
}

istrstream& istrstream::operator >>(float& f)
{
	sscanf(buf+point,"%g",&f);
	return(*this);
}

istrstream& istrstream::operator >>(double& d)
{
	sscanf(buf+point,"%lg",&d);
	return(*this);
}




/*********************************************************************
* iostrstream
*
* NOT COMPLETE
*********************************************************************/
class iostrstream : public istrstream , public ostrstream {
	iostrstream(char *p,int bufsize)
		: istrstream(p,bufsize) , ostrstream(p,bufsize) { }
};


#else

istrstream::istrstream() {
}

typedef ostrstream ostringstream;
typedef istrstream istringstream;
//typedef strstream stringstream;

#endif

#pragma endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions