#pragma interface #ifndef MISC_H #define MISC_H #include class ARRAY_OBJ{ protected: char modified; /*~PROTOBEG~ ARRAY_OBJ */ public: ARRAY_OBJ (void); virtual int edit (void); void rstmodified (void); void setmodified (void); virtual int was_modified (void); virtual ~ARRAY_OBJ (void); /*~PROTOEND~ ARRAY_OBJ */ }; class ARRAY: public ARRAY_OBJ{ protected: int is_owner; // Is this object allowed to delete // the objects it contains ? ARRAY_OBJ **tb; int nb; int maxtb; // Current maximum length of the table int increm; // growth rate of tb /*~PROTOBEG~ ARRAY */ public: ARRAY (void); void add (ARRAY_OBJ *pt); void clear (void); int editone (ARRAY_OBJ *e); int editone (int no); protected: ARRAY_OBJ *getitem (int no)const; public: int getnb (void)const; protected: void grow (void); private: void grow_realloc (int newmax); public: void insert (int pos, ARRAY_OBJ *pt); void invert (void); int lookup (ARRAY_OBJ *o)const; int manage_edit (ARRAY_OBJ *e, int code); int manage_edit (ARRAY_OBJ *e, int code, int insertpos); void moveto (ARRAY_OBJ *o, int newpos); void neverdelete (void); int remove (ARRAY_OBJ *obj); int remove (int no); void remove_all (void); int remove_del (ARRAY_OBJ *obj); int remove_del (int no); void remove_last (int cut); void rstmodified (void); void set (int pos, ARRAY_OBJ *pt); void setgrowth (int _increm); int size (void)const; void sort (int (*cmp)(const ARRAY_OBJ *, const ARRAY_OBJ *)); virtual int was_modified (void); virtual int write (void); virtual ~ARRAY (void); /*~PROTOEND~ ARRAY */ }; template< class T> class ARRAY_OBJS: public ARRAY{ public: T *getitem(int no) const { return (T*)ARRAY::getitem(no); } void add (T*p) { ARRAY::add (p); } ARRAY_OBJS(){} ~ARRAY_OBJS(){} }; #ifndef SSTRING_H #include "sstring.h" #endif #ifndef CONFIGF_H #include "configf.h" #endif class ARRAY_KEY: public SSTRING{ bool owner; // May delete the object public: ARRAY_OBJ *obj; /*~PROTOBEG~ ARRAY_KEY */ public: ARRAY_KEY (const char *_key, ARRAY_OBJ *_obj, bool is_owner); ~ARRAY_KEY (void); /*~PROTOEND~ ARRAY_KEY */ }; class ARRAY_KEYS: public ARRAY{ /*~PROTOBEG~ ARRAY_KEYS */ public: void add (const char *key, ARRAY_OBJ *obj, bool owner); void add (const char *key, ARRAY_OBJ *obj); ARRAY_KEY *getitem (int no)const; ARRAY_KEY *getobj (const char *key)const; ARRAY_OBJ *getval (const char *key)const; void sort (void); /*~PROTOEND~ ARRAY_KEYS */ }; class SSTRING_KEY: public ARRAY_KEY{ /*~PROTOBEG~ SSTRING_KEY */ public: SSTRING_KEY (const char *_key, const char *_val); SSTRING *getobj (void); const char *getobjval (void); void setobjval (const SSTRING *val); void setobjval (const SSTRING&val); void setobjval (const char *val); /*~PROTOEND~ SSTRING_KEY */ }; class SSTRING_KEYS: public ARRAY_KEYS{ /*~PROTOBEG~ SSTRING_KEYS */ public: SSTRING_KEYS (void); void add (const char *key, const char *val); SSTRING_KEY *getitem (int no)const; SSTRING_KEY *getobj (const char *key)const; const char *getval (const char *key)const; ~SSTRING_KEYS (void); /*~PROTOEND~ SSTRING_KEYS */ }; class CONTEXT_LOCK{ class CONTEXT_LOCK_PRIVATE *priv; /*~PROTOBEG~ CONTEXT_LOCK */ public: CONTEXT_LOCK (const char *_key1, const char *_key2); CONTEXT_LOCK (const char *key); private: void check (bool msg)const; void init (const char *_key1, const char *_key2, int _flags); public: bool isok (int seconds)const; bool isok (void)const; ~CONTEXT_LOCK (void); /*~PROTOEND~ CONTEXT_LOCK */ }; class MESSAGE_DEF: public ARRAY_OBJ{ class MESSAGE_DEF_PRIVATE *priv; /*~PROTOBEG~ MESSAGE_DEF */ public: MESSAGE_DEF (const char *msg); MESSAGE_DEF (const char *msg, const char *argnames[]); const char **getargs (void)const; const char *getmodule (void)const; const char *getmsg (void)const; ~MESSAGE_DEF (void); /*~PROTOEND~ MESSAGE_DEF */ }; class DIALOG; #ifndef DIALOG_DEF_H #include "dialog_def.h" #endif class PRIVILEGE; class CONFDB; class SSTREAM; class USERACCT_COMNGS; class FILE_CFG; #include "module_api.h" #include #include #include #include #include struct PARAM_STRING{ const char *ptr; PARAM_STRING (const char *_ptr){ ptr = _ptr; } PARAM_STRING (const std::string &s){ ptr = s.c_str(); } }; inline bool operator == (const PARAM_STRING &p1, const PARAM_STRING &p2) { return strcmp(p1.ptr,p2.ptr)==0;} inline bool operator != (const PARAM_STRING &p1, const PARAM_STRING &p2) { return strcmp(p1.ptr,p2.ptr)!=0;} inline bool operator == (const PARAM_STRING &p, const std::string &s) { return s == p.ptr;} inline bool operator == (const std::string &s, const PARAM_STRING &p) { return s == p.ptr;} inline bool operator != (const PARAM_STRING &p, const std::string &s) { return s != p.ptr;} inline bool operator != (const std::string &s, const PARAM_STRING &p) { return s != p.ptr;} inline bool operator == (const PARAM_STRING &p, const char *s) { return strcmp(p.ptr,s)==0;} inline bool operator == (const char *s, const PARAM_STRING &p) { return strcmp(p.ptr,s)==0;} inline bool operator != (const PARAM_STRING &p, const char *s) { return strcmp(p.ptr,s)!=0;} inline bool operator != (const char *s, const PARAM_STRING &p) { return strcmp(p.ptr,s)!=0;} struct PARAM_VECTOR_STRING_BEGIN{ bool is_vs; std::vector::const_iterator vs_it; std::vector::const_iterator vp_it; PARAM_VECTOR_STRING_BEGIN(const std::vector *vs, bool is_end){ if (is_end){ vs_it = vs->end(); }else{ vs_it = vs->begin(); } is_vs = true; } PARAM_VECTOR_STRING_BEGIN(const std::vector *vp, bool is_end){ if (is_end){ vp_it = vp->end(); }else{ vp_it = vp->begin(); } is_vs = false; } PARAM_VECTOR_STRING_BEGIN & operator ++(){ if (is_vs){ vs_it++; }else{ vp_it++; } return *this; } bool operator != (PARAM_VECTOR_STRING_BEGIN &e){ if (is_vs){ return vs_it != e.vs_it; }else{ return vp_it != e.vp_it; } } const char *operator *() const{ if (is_vs){ return vs_it->c_str(); }else{ return *vp_it; } } }; // This is a generic class used to pass either a vector or vector // It is used mainly by build-protocol struct PARAM_VECTOR_STRING { const std::vector *vs; const std::vector*vp; PARAM_VECTOR_STRING(const std::vector &_vs){ vs = &_vs; vp = NULL; } PARAM_VECTOR_STRING(const std::vector &_vp){ vs = NULL; vp = &_vp; } PARAM_VECTOR_STRING_BEGIN begin() const{ if (vs != NULL){ return PARAM_VECTOR_STRING_BEGIN(vs,false); }else{ return PARAM_VECTOR_STRING_BEGIN(vp,false); } } PARAM_VECTOR_STRING_BEGIN end() const { if (vs != NULL){ return PARAM_VECTOR_STRING_BEGIN(vs,true); }else{ return PARAM_VECTOR_STRING_BEGIN(vp,true); } } size_t size() const { if (vs != NULL){ return vs->size(); }else{ return vp->size(); } } }; // Used by the function copystring struct copystring_copybuf{ const char *pt; std::string buf; bool end = false; bool skipped = false; copystring_copybuf(PARAM_STRING p){ pt = p.ptr; } void stop(){ end = true; } void insert (const char car){ buf += car; } void insert (const char *str){ buf += str; } void replace(unsigned nbskip, const char car){ buf += car; skip(nbskip); } void replace(unsigned nbskip, const char *str){ buf += str; skip(nbskip); } void replace(unsigned nbskip, const std::string str){ buf += str; skip(nbskip); } void skip (unsigned n){ skipped=true; while (*pt != '\0' && n > 0){ pt++; n--; } } void keep (unsigned n){ skipped=true; while (*pt != '\0' && n > 0){ buf += *pt++; n--; } } void clear(){ buf.clear(); } }; #include "misc.p" // Used to specify limits with the splitline and splitlineq functions template class limits_t{ public: T &val; T min; T max; limits_t(T &_val, T _min, T _max):val(_val),min(_min),max(_max){} }; template void splitline_assign(bool &ok, limits_t &a, const std::string &v) { splitline_assign(ok,a.val,v); if (ok && (a.val < a.min || a.val > a.max)) ok = false; } template inline void splitline_reset (limits_t &a) { splitline_reset(a.val); } template limits_t limits(T &a, T min, T max){ return limits_t(a,min,max); } template class enums_t; template enums_t enums(T &val, std::initializer_list elements); // The enums_t class can't be used standalone, because it receives an initializer_list that vanishes // Only the enums() function must be used: enums(variable,{accepted values, ... }) template class enums_t{ public: T &val; std::initializer_list vals; private: friend enums_t enums (T &_val, std::initializer_list elements); enums_t(T &_val, std::initializer_list elements):val(_val),vals(elements){} }; template enums_t enums(T &val, std::initializer_list elements){ return enums_t(val,elements); } template void splitline_assign(bool &ok, enums_t &a, const std::string &v) { splitline_assign(ok,a.val,v); if (ok && std::find(a.vals.begin(),a.vals.end(),a.val)==a.vals.end()) ok = false; } template void splitline_reset (enums_t &a) { splitline_reset(a.val); } // The match_t class can't be used standalone. It is used to match one word, but does not store it // The goal is to check that one word of the input match. So if it matches, no need to copy it. class match_t{ public: const char *word; match_t(const char *_word) : word(_word){} }; inline void splitline_assign(bool &ok, match_t &a, const std::string &v) { if (ok) ok = v == a.word; } inline void splitline_reset (match_t &a) { // Does nothing } inline match_t match(const char *word) { return match_t(word); } // The skip_t class can't be used standalone. It is used to skip one word, and does not store it // The goal is to ignore one part (word) of the input. class skip_t{ public: skip_t() {} }; inline void splitline_assign(bool &ok, skip_t &a, const std::string &v) { // Does nothing } inline void splitline_reset (skip_t &a) { // Does nothing } inline skip_t skip() { return skip_t(); } // splitline template void splitline_reset (T & v, TS& ... vs) { splitline_reset(v); splitline_reset(vs...); } template bool splitline (const std::vector &tb, unsigned off, T & v, TS& ... vs) { bool ok = false; if (off < tb.size()){ ok = true; splitline_assign (ok,v,tb[off]); if (ok){ ok = splitline(tb,off+1,vs...); } } return ok; } template bool splitline (PARAM_STRING line, const char split, TS&& ... vs) { auto tb = str_splitline(line,split); bool ok = splitline (tb,0,vs...); if (!ok) splitline_reset(vs...); return ok; } template bool splitline (PARAM_STRING line, TS&& ... vs) { auto tb = str_splitline(line,' '); bool ok = splitline (tb,0,vs...); if (!ok) splitline_reset(vs...); return ok; } template bool splitlineq (PARAM_STRING line, const char split, TS&& ... vs) { auto tb = str_splitlineq(line,split); bool ok = splitline (tb,0,vs...); if (!ok) splitline_reset(vs...); return ok; } template bool splitlineq (PARAM_STRING line, TS&& ... vs) { auto tb = str_splitlineq(line,' '); bool ok = splitline (tb,0,vs...); if (!ok) splitline_reset(vs...); return ok; } #endif