Annotation of parser3/src/include/pa_string.h, revision 1.71

1.41      paf         1: /** @file
1.43      paf         2:        Parser: string class decl.
                      3: 
1.29      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.43      paf         5: 
1.30      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.29      paf         7: 
1.71    ! paf         8:        $Id: pa_string.h,v 1.70 2001/04/04 10:50:34 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #ifndef PA_STRING_H
                     12: #define PA_STRING_H
                     13: 
1.49      paf        14: #include "pa_config_includes.h"
1.9       paf        15: 
1.1       paf        16: #include <stddef.h>
                     17: 
1.15      paf        18: #include "pa_pool.h"
1.4       paf        19: #include "pa_types.h"
                     20: 
1.63      paf        21: class Table;
                     22: 
1.50      paf        23: /**
                     24:        $MAIN:html-typo table elements must enlarge string not more that that
                     25:        that's a tradeoff - otherwise we'd have to scan string twice:
                     26:        - first for buffer length
                     27:        - second for replacements themselves
                     28: */
1.31      paf        29: #define UNTAINT_TIMES_BIGGER 10
                     30: 
1.9       paf        31: #ifndef NO_STRING_ORIGIN
1.33      paf        32: #      define STRING_APPEND_PARAMS \
                     33:                const char *src, size_t size,  \
1.52      paf        34:                String::Untaint_lang lang, \
1.33      paf        35:                const char *file, uint line
1.54      paf        36: /// appends piece to String  @see String::real_append
                     37: #      define APPEND(src, size, lang, file, line) \
                     38:                real_append(src, size, lang, file, line)
1.9       paf        39: #else
1.33      paf        40: #      define STRING_APPEND_PARAMS \
                     41:                const char *src, \
                     42:                size_t size, \
1.52      paf        43:                String::Untaint_lang lang
1.54      paf        44: /// appends piece to String  @see String::real_append
                     45: #      define APPEND(src, size, lang, file, line) \
                     46:                real_append(src, size, lang)
                     47: #endif
1.48      paf        48: /// appends clean piece to String  @see String::real_append
1.54      paf        49: #define APPEND_CLEAN(src, size, file, line) \
1.62      paf        50:        APPEND(src, size, String::UL_CLEAN, file, line)
1.48      paf        51: /// appends tainted piece to String  @see String::real_append
1.54      paf        52: #define APPEND_TAINTED(src, size, file, line) \
1.62      paf        53:        APPEND(src, size, String::UL_TAINTED, file, line)
1.48      paf        54: /// handy: appends const char* piece to String  @see String::real_append
1.54      paf        55: #define        APPEND_CONST(src) APPEND_CLEAN(src, 0, 0, 0)
1.9       paf        56: 
1.62      paf        57: class Array;
1.71    ! paf        58: class SQL_Connection;
1.62      paf        59: 
1.42      paf        60: /** 
1.41      paf        61:        Pooled string.
                     62: 
1.47      paf        63:        Internal structure:
                     64:        @verbatim
                     65:                String                          Chunk0
                     66:                ======                          ========
                     67:                head--------------->[ptr, size, ...]
                     68:                append_here-------->[ptr, size, ...]
                     69:                                                        .
                     70:                                                        .
                     71:                                                        [ptr, size, ...]
                     72:                link_row----------->[link to the next chunk]
1.41      paf        73:        @endverbatim
                     74: 
                     75:        All pieces remember 
                     76:        - the file and its line they are from [can be turned off by NO_STRING_ORIGIN]
                     77:        - whether they are tainted or not, 
                     78:          and the language which should be used to detaint them
                     79: */
1.15      paf        80: class String : public Pooled {
1.1       paf        81: public:
1.48      paf        82: 
1.1       paf        83:        enum {
1.41      paf        84:                CR_PREALLOCATED_COUNT=5, ///< default preallocated item count
                     85:                CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded()
1.1       paf        86:        };
                     87: 
1.41      paf        88:        /// piece is tainted or not. the language to use when detaint 
1.27      paf        89:        enum Untaint_lang {
1.60      paf        90:                UL_UNKNOWN=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum
1.62      paf        91:                UL_CLEAN, ///< clean
                     92:                UL_TAINTED,  ///< tainted, untaint language as assigned later 
1.27      paf        93:                // untaint languages. assigned by ^untaint[lang]{...}
1.48      paf        94:                UL_PASS_APPENDED,
1.41      paf        95:                        /**<
                     96:                                leave language built into string being appended.
                     97:                                just a flag, that value not stored
                     98:                        */
1.48      paf        99:                UL_AS_IS,     ///< leave all characters intact
                    100:                UL_FILE_NAME, ///< filename
                    101:                UL_HEADER,    ///< text in response header
                    102:                UL_URI,       ///< text in uri
                    103:                UL_TABLE,     ///< ^table:set body
                    104:                UL_SQL,       ///< ^table:sql body
                    105:                UL_JS,        ///< JavaScript code
                    106:                UL_HTML,      ///< HTML code (for editing)
                    107:                UL_HTML_TYPO  ///< HTML code with TYPOgraphic replacements (for showing)
1.27      paf       108:        };
                    109: 
1.8       paf       110: public:
                    111: 
1.37      paf       112:        String(Pool& apool, const char *src=0, bool tainted=false);
1.14      paf       113:        String(const String& src);
                    114:        size_t size() const { return fsize; }
1.69      paf       115:        /// convert to C string. if 'lang' known, forcing 'lang' to it
1.71    ! paf       116:        char *cstr(Untaint_lang lang=UL_UNKNOWN, SQL_Connection *connection=0) const {
1.50      paf       117:                char *result=(char *)malloc(size()*UNTAINT_TIMES_BIGGER+1);
1.71    ! paf       118:                char *eol=store_to(result, lang, connection);
1.50      paf       119:                *eol=0;
                    120:                return result;
                    121:        }
1.46      paf       122:        /** append fragment
1.54      paf       123:                @see APPEND_CLEAN, APPEND_TAINTED, APPEND_CONST
1.46      paf       124:        */
1.9       paf       125:        String& real_append(STRING_APPEND_PARAMS);
1.44      paf       126:        /// @return <0 ==0 or >0 depending on comparison result
1.62      paf       127:        int cmp (int& partial, const String& src, 
                    128:                size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const;
1.56      paf       129:        bool operator < (const String& src) const {     int p; return cmp(p, src)<0; }
                    130:        bool operator > (const String& src) const {     int p; return cmp(p, src)>0; }
                    131:        bool operator <= (const String& src) const { int p; return cmp(p, src)<=0; }
                    132:        bool operator >= (const String& src) const { int p; return cmp(p, src)>=0; }
1.26      paf       133:        bool operator == (const String& src) const {
                    134:                if(size()!=src.size()) // can speed up in trivial case
                    135:                        return false;
1.56      paf       136:                int p; return cmp(p, src)==0;
1.26      paf       137:        }
1.56      paf       138:        bool operator != (const String& src) const { int p; return cmp(p, src)!=0; }
1.26      paf       139: 
1.50      paf       140:        /**
                    141:                 @param partial 
                    142:                        returns partial match status. 
1.51      paf       143:                        - -1: strings too different
                    144:                        -  0: full match
                    145:                        -  1: means @c this starts @c src
                    146:                        -  2: means @src starts @this
1.50      paf       147:        */
1.61      paf       148:        int cmp(int& partial, const char* src_ptr, size_t src_size=0, 
1.62      paf       149:                size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const;
1.50      paf       150:        bool operator == (const char* src_ptr) const { 
                    151:                size_t src_size=src_ptr?strlen(src_ptr):0;
                    152:                if(size() != src_size)
                    153:                        return false;
                    154:                int partial; // unused
1.56      paf       155:                return cmp(partial, src_ptr, src_size)==0; 
1.50      paf       156:        }
                    157: 
1.42      paf       158:        /** 
                    159:                appends other String.
1.41      paf       160: 
1.47      paf       161:                marking all tainted pieces of it with @a lang.
                    162:                or marking ALL pieces of it with a @a lang when @a forced to.
1.41      paf       163:        */
1.39      paf       164:        String& append(const String& src, Untaint_lang lang, bool forced=false);
1.8       paf       165: 
1.41      paf       166:        /// simple hash code of string. used by Hash
1.14      paf       167:        uint hash_code() const;
1.54      paf       168: 
                    169:        /// extracts [start, finish) piece of string
1.70      paf       170:        String& mid(size_t start, size_t finish) const;
1.55      paf       171: 
1.59      paf       172:        /// @return position of substr in string, -1 means "not found" [String version]
1.62      paf       173:        int pos(const String& substr, 
                    174:                size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const;
1.59      paf       175:        /// @return position of substr in string, -1 means "not found" [const char* version]
1.62      paf       176:        int pos(const char *substr, size_t substr_size, 
                    177:                size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const;
                    178: 
                    179:        void split(Array& result, 
                    180:                size_t *pos_after_ref, 
                    181:                const char *delim, size_t delim_size, 
                    182:                Untaint_lang lang, int limit=-1) const;
                    183:        void split(Array& result, 
                    184:                size_t *pos_after_ref, 
                    185:                const String& delim, 
                    186:                Untaint_lang lang, int limit=-1) const;
                    187: 
1.68      paf       188:        typedef void (*Row_action)(Table& table, Array *row, int start, int finish, 
                    189:                void *info);
1.64      paf       190:        bool match(const String *aorigin,
                    191:                const String& regexp, 
1.65      paf       192:                const String *options,
1.66      paf       193:                Table **table,
                    194:                Row_action row_action, void *info) const;
1.13      paf       195: 
1.41      paf       196: #ifndef NO_STRING_ORIGIN
                    197:        /// origin of string. calculated by first row
1.43      paf       198:        const Origin& origin() const;
1.41      paf       199: #endif
1.8       paf       200: 
1.10      paf       201: private:
                    202: 
1.1       paf       203:        struct Chunk {
1.6       paf       204:                // the number of rows in chunk
1.56      paf       205:                size_t count;
1.1       paf       206:                union Row {
1.27      paf       207:                        // fragment
                    208:                        struct { 
                    209:                                const char *ptr;  // pointer to the start
                    210:                                size_t size;  // length
                    211:                                Untaint_lang lang; // untaint flag, later untaint language
                    212: #ifndef NO_STRING_ORIGIN
                    213:                                Origin origin;  // origin
                    214: #endif
1.1       paf       215:                        } item;
                    216:                        Chunk *link;  // link to the next chunk in chain
1.2       paf       217:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf       218:                // next rows are here
                    219:                Chunk *preallocated_link;
                    220:        }
                    221:                head;  // the head chunk of the chunk chain
                    222: 
                    223:        // next append would write to this record
                    224:        Chunk::Row *append_here;
                    225:        
                    226:        // the address of place where lies address 
                    227:        // of the link to the next chunk to allocate
                    228:        Chunk::Row *link_row;
                    229: 
1.5       paf       230: private:
1.25      paf       231:        // last chunk
                    232:        Chunk *last_chunk;
1.5       paf       233: 
                    234:        // string size
                    235:        size_t fsize;
                    236: 
                    237:        // used rows in all chunks
                    238:        int fused_rows;
                    239: 
                    240: private:
1.1       paf       241: 
                    242:        bool chunk_is_full() {
                    243:                return append_here == link_row;
                    244:        }
                    245:        void expand();
1.71    ! paf       246:        char *String::store_to(char *dest, 
        !           247:                Untaint_lang lang=UL_UNKNOWN, SQL_Connection *connection=0) const;
1.7       paf       248: 
                    249: private: //disabled
                    250: 
1.12      paf       251:        String& operator = (const String&) { return *this; }
1.7       paf       252: 
1.1       paf       253: };
                    254: 
                    255: #endif

E-mail: