/* SCRIPTOL LIBRARY (c) 2002-2004 by Denis G. Sureau http://www.scriptol.com LICENCE Along with the free Scriptol to native compiler is provided the libsol.a or libsol.lib library, it contains the TEXT, DYN and ARRAY classes and lot of functions compatibles with the Php library. This library is the original work of Denis G. Sureau and has been registered at INPI 25 june 2002. The Php compatible functions are original work also. They have nothing to do with Php sources. The library is provided to allow the user to link it with its Scriptol compiled program. He is allowed to include the library in binary form into its program and distribute its programs with the library included into, with no restriction or condition or fee due. He is not allowed to distribute the compiled library as it, but as a part of an open-source program. The compiled library may be included among other files of the open source distribution and headers of the file to include, that must hold my name and the address of the Scriptol site. The source of the library is provided to users to allow them either to modify it according to their need, or to compile it in a different file format, for a different compiler. The users are not permitted to distribute the source of the library in no any case, even modified. */ #if !defined(_DYN_HEADER) #ifndef _DYN_HEADER #define _DYN_HEADER class item; class dyn; class sol_pair; class array; class text; class object : public gc_cleanup { public: object() { } }; #if defined(MSVC) | defined(MINGW) | defined(BCC) struct dirent { long d_ino; unsigned short d_reclen; unsigned short d_namlen; char* d_name; }; typedef struct { struct _finddata_t dd_dta; struct dirent dd_dir; long dd_handle; short dd_stat; char dd_name[1]; } DIR; #endif std::ostream& operator<< (std::ostream& out, dyn); std::ostream& operator<< (std::ostream& out, dyn *); //extern dyn Nil; class nil : public gc_cleanup { public: int flag; union { bool b; // bool for error double n; text *t; array *a; sol_pair *p; FILE *f; void *o; DIR *dir; natural l; }; nil(); ~nil() {} bool is(); static bool is(bool); static bool is(int); static bool is(natural); static bool is(double); static bool is(text &); static bool is(array &); static bool is(dyn &); static bool is(FILE *); static bool is(DIR *); static bool is(object *); }; /** C++ implementation of dynamic variables, with Scriptol's methods. */ class dyn : public nil { public: static dyn error; void set(unsigned char a); void set(char); void set(bool); void set(int); void set(unsigned int); void set(natural); void set(float); void set(double); void set(const char *); void set(const text &); void set(text *); void set(const dyn &); void set(dyn *); void set(sol_pair *); void set(array *); void set(const array &); void set(FILE *); void set(DIR *); void set(void *); void set(const _arr32 &); dyn() { flag = 0; b = false; } /** Constructor: a dyn mays be created from a scalar, another dyn, an object or an array. */ dyn(const dyn &a); inline dyn(unsigned char a) { set(a); } inline dyn(char a) { set(a); } inline dyn(bool a) { set(a); } inline dyn(int a) { set(a); } inline dyn(unsigned int a) { set(a); } inline dyn(natural a) { set(a); } inline dyn(float a) { set(a); } inline dyn(double a) { set(a); } inline dyn(const char *a) { set(a); } inline dyn(const text &a) { set(a); } inline dyn(text *a) { set(a); } inline dyn(dyn *a) { set(a); } inline dyn(sol_pair *a) { set(a); } inline dyn(array *a) { set(a); } inline dyn(const array &a) { set(a); } inline dyn(FILE *a) { set(a); } inline dyn(DIR *a) { set(a); } inline dyn(void *a) { set(a); } inline dyn(_arr32 *a) { set(a); } inline bool isPair() { return flag == TYPE_PAIR; } inline bool isInt() { return flag == TYPE_INT; } /** Returns true if the dyn is an integer. */ inline bool isInteger() { return flag == TYPE_INT; } /** Returns true if the dyn is a natural. */ inline bool isNatural() { return isNumber(); } /** Returns true if the dyn is a number. */ bool isNumber(); /** Returns true if the dyn is a text. */ inline bool isText() { return flag == TYPE_TEXT; } /** Returns true if the dyn is an mixed array. */ inline bool isArray() { return flag == TYPE_ARRAY; } /** Returns true if the dyn is a boolean. */ inline bool isBoolean() { return flag == TYPE_BOOL; } /** Returns true if the dyn is a file. */ inline bool isFile() { return flag == TYPE_FILE; } /** Returns true if the dyn is a typed array. */ inline bool isList() { return flag == TYPE_TARRAY; } /** Static. Returns true if the parameter is a string of digits. */ static bool isNumber(const char *); static bool isNumber(const text &t); static bool isNumber(text *t); /** Returns the type of the array if the dyn is a typed array. */ int arrayType(); /** Returns the len if the dyn is a text. If it is a number, returns the number of bytes in memory. */ int len(); /** Return the size if the dyn is an array. */ int size(); bool toBoolean(); /** Converts the dyn to an integer. Return 0 if the dyn was not a number nor a text. */ int toInt(); inline int toInteger() { return toInt(); } /** Converts the dyn to a natural. Return 0 if the dyn was not a number nor a text. */ natural toNatural(); /** Converts the dyn to a real. Return 0 if the dyn was not a number nor a text. */ double toReal(); /** Converts the dyn to a number. Return 0 if the dyn was not a number nor a text. */ double toNumber(); sol_pair *toPair(); /** Converts the dyn to a text. Return nil if the dyn can't be converted. Numbers, strings, array may be. */ text toText(); // any data /** Converts the dyn to C string of chars. Return nil if the dyn can't be converted. */ char *toStr(); // any data /** Converts the dyn to an array. Return an empty array if the dyn was not an array. */ array *toArray(); /** Converts the dyn to a dict. Return an empty dict if the dyn was not a dict. */ array *toDict(); /** Converts the dyn to a file. Return null if the dyn was not a file. */ FILE *toFile(); text toFunction(); /** Converts the dyn to an object. Return an empty object if the dyn was not an object. */ void *toObject(); /** Returns the typed array assigned to the dyn (default no parameter), but it mays also convert a scalar to a typed array having its type. Ex: x.toList($(TYPE_TEXT)) builds a text array with one element. */ void *toList(int = TYPE_TARRAY); /** Static. Converts the parameter to a boolean. */ static bool toBoolean(dyn); /** Static. Converts the parameter to an integer. */ static int toInt(dyn); static inline int toInteger(dyn d) { return toInt(d); } /** Static. Converts the parameter to a natural. */ static natural toNatural(dyn); /** Static. Converts the parameter to a real. */ static double toReal(dyn); static double toReal(const char *); /** Static. Converts the parameter to a number. */ static double toNumber(dyn); /** Static. Converts the parameter to a string of chars. */ static char *toStr(dyn); /** Static. Converts the parameter to an array. */ static array *toArray(dyn); /** Static. Converts the parameter to a dict. */ static array *toDict(dyn); static sol_pair *toPair(dyn); /** Static. Converts the parameter to a text. */ static text toText(dyn); /** Static. Converts the parameter to a file. */ static FILE *toFile(dyn); /** Static. Converts the parameter to a typed array. */ static void *toList(dyn); bool equal(double, const char *); bool equal(double, text *); void clone(dyn); void operator=(dyn a); template void operator=(ANY a) { set(a); } bool operator==(const dyn &); bool operator==(dyn *); bool operator==(int); bool operator==(natural); bool operator==(double); bool operator==(const char *); bool operator==(const text &); bool operator==(bool); bool operator==(const array &); bool operator!=(int); bool operator!=(natural); bool operator!=(double); bool operator!=(const dyn &); bool operator!=(dyn *); bool operator!=(const char *); bool operator!=(const text &); bool operator!=(bool); bool operator!=(const array &); bool operator<(int); bool operator<(natural); bool operator<(double); bool operator<(const dyn &d); bool operator<(dyn *); bool operator<(const char *); bool operator<(const text &); bool operator<(bool); bool operator<(const array &); bool operator>(int); bool operator>(natural); bool operator>(double); bool operator>(const dyn &d); bool operator>(dyn *); bool operator>(const char *); bool operator>(const text &); bool operator>(bool); bool operator>(const array &); inline bool operator>=(int b) { return !operator<(b); } inline bool operator>=(double b) { return !operator<(b); } inline bool operator>=(bool b) { return !operator<(b); } inline bool operator>=(const text &b) { return !operator<(b); } inline bool operator>=(const char *b) { return !operator<(b); } inline bool operator>=(const array &b) { return !operator<(b); } inline bool operator>=(const dyn &b) { return !operator<(b); } inline bool operator>=(dyn *b) { return !operator<(b); } inline bool operator<=(int b) { return !operator>(b); } inline bool operator<=(double b) { return !operator>(b); } inline bool operator<=(bool b) { return !operator>(b); } inline bool operator<=(const text &b) { return !operator>(b); } inline bool operator<=(const char *b) { return !operator>(b); } inline bool operator<=(const array &b) { return !operator>(b); } inline bool operator<=(const dyn &b) { return !operator>(b); } inline bool operator<=(dyn *b) { return !operator>(b); } double operator+(int); double operator+(natural); double operator+(double); char *operator+(const char *); text operator+(const text &); dyn operator+(const dyn &); array operator+(const array &); array operator+(array *); double operator-(int); double operator-(natural); double operator-(double); dyn operator-(const dyn &); array operator-(const array &); double operator*(int); double operator*(natural); double operator*(double); double operator*(dyn); double operator/(int); double operator/(natural); double operator/(double); double operator/(dyn); double operator%(int); double operator%(natural); double operator%(double); double operator%(dyn); int operator&(dyn); int operator&(int); int operator&(double); int operator|(dyn); int operator|(int); int operator|(double); bool operator&&(dyn); bool operator&&(int); bool operator&&(double); bool operator||(dyn); bool operator||(int); bool operator||(double); void operator+=(dyn); void operator+=(int); void operator+=(natural); void operator+=(double); void operator+=(const char *); void operator+=(const text &); void operator+=(const array &); void operator-=(dyn); void operator-=(int); void operator-=(natural); void operator-=(double); void operator-=(const array &); void operator*=(dyn); void operator*=(int); void operator*=(natural); void operator*=(double); void operator/=(dyn); void operator/=(int); void operator/=(natural); void operator/=(double); void operator<<=(int); void operator>>=(int); bool operator!(); array &operator,(dyn); dyn at(int); inline dyn at(natural a) { return at( (int) a); } dyn at(const text &); dyn at(const char *); dyn operator[](int); inline dyn operator[](natural a) { return operator[]((int) a); } dyn operator[](const text &); dyn operator[](const char *); dyn operator[](dyn); void setAt(int, dyn); inline void setAt(natural a, dyn b) { setAt((int) a, b); } void setAt(int, const text &); inline void setAt(natural a, const text &b) { setAt((int) a, b); } void setAt(int, const char *); inline void setAt(natural a, const char *b) { setAt((int) a, b); } static void setNil() { error.setFalse(); } static bool isNil(int a) { return a == 0; } static bool isNil(natural a) { return a == 0; } static bool isNil(dyn); bool isNil(); static dyn dummyNil; void setFalse() { flag = TYPE_BOOL; b = 0; } //===== text text trim(); text lTrim(); text rTrim(); text lower(); text upper(); void cat(char *); void cat(dyn); void cat(const text &); //===== array: iterators void reset(); dyn inc(); dyn dec(); dyn value(); dyn key(); dyn begin(); dyn end(); int index(); //==== stack void push(dyn); dyn pop(); dyn shift(); void unshift(dyn); array slice(int, int, int); void splice(int, int, array, int); array slice(natural, natural, natural); void splice(natural, natural, array, natural); double sum(); //===== void display(); static text str(dyn); int nomatch(char); void boolToNum(); }; text operator+(text, dyn); text operator+(const char *, dyn); dyn operator+(int , dyn); dyn operator+(double , dyn); dyn operator+(array *, dyn); dyn operator-(int , dyn); dyn operator-(double , dyn); dyn operator-(array *, dyn); dyn operator*(int , dyn); dyn operator*(double , dyn); dyn operator/(int , dyn); dyn operator/(double , dyn); int operator&(int , dyn); int operator&(double , dyn); int operator|(int , dyn); int operator|(double , dyn); inline bool operator==(int a, dyn b) { return b == a; } inline bool operator==(double a, dyn b) { return b == a; } inline bool operator==(const text &t, dyn d) { return d == t; } inline bool operator==(bool b, dyn d) { return d == b; } inline bool operator==(const char *t, dyn d) { return d == t; } inline bool operator!=(int a, dyn b) { return b != a; } inline bool operator!=(double a, dyn b) { return b != a; } inline bool operator!=(const text &t, dyn d) { return d != t; } inline bool operator!=(bool b, dyn d) { return d != b; } inline bool operator!=(const char *t, dyn d) { return d != t; } inline bool operator<=(int a, dyn b) { return b > a; } inline bool operator<=(double a, dyn b) { return b > a; } inline bool operator<=(const text &t, dyn d) { return d > t; } inline bool operator<=(const char *t, dyn d) { return d > t; } inline bool operator>=(int a, dyn b) { return b < a; } inline bool operator>=(double a, dyn b) { return b < a; } inline bool operator>=(const text &t, dyn d) { return d > t; } inline bool operator>=(const char *t, dyn d) { return d < t; } inline bool operator>(int i, dyn b) { return b.operator<=(i); } inline bool operator>(double d, dyn b) { return b.operator<=(d); } inline bool operator>(const text &t, dyn d) { return d.operator<=(t); } inline bool operator>(const char *t, dyn d) { return d.operator>=(t); } inline bool operator<(int i, dyn b) { return b.operator>=(i); } inline bool operator<(double d, dyn b) { return b.operator>=(d); } inline bool operator<(const text &t, dyn d) { return d.operator>=(t); } inline bool operator<(const char *t, dyn d) { return d.operator<=(t); } void operator+=(int, dyn); void operator+=(double, dyn); void operator+=(text, dyn); void operator+=(char *, dyn); void operator-=(int, dyn); void operator-=(double, dyn); void operator*=(int, dyn); void operator*=(double, dyn); void operator/=(int, dyn); void operator/=(double, dyn); void operator&=(int, dyn); void operator&=(double, dyn); void operator|=(int, dyn); void operator|=(double, dyn); bool operator&&(int, dyn); bool operator&&(double, dyn); bool operator||(int, dyn); bool operator||(double, dyn); #endif #endif