00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _JSTRING_H_
00023 #define _JSTRING_H_
00024
00025 #include "standard.h"
00026
00027 #if !defined(_WIN32_WCE)
00028 #if !defined(CYGWIN)
00029 #include <sys/types.h>
00030 #include <sys/timeb.h>
00031 #endif
00032 #endif
00033 #ifdef WIN32
00034 #include <windows.h>
00035 #if !defined(CYGWIN)
00036 #include <tchar.h>
00037 #endif
00038 #if !defined(_WIN32_WCE)
00039 #include <wchar.h>
00040 #endif
00041 #endif // WIN32
00042 #include <stdio.h>
00043 #include <string.h>
00044 #include <stdlib.h>
00045
00046 #include "Object.h"
00047
00048 namespace cmlabs {
00049
00050 class Collection;
00051 class ObjectCollection;
00052 class Dictionary;
00053 class ObjectDictionary;
00054 class DotString;
00055 class JTime;
00056 class Boolex;
00057
00058
00059 class JString : public Object {
00060 public:
00061 JString(const bool val);
00062 JString(const JString &value, unsigned int width, int justification);
00063 JString(unsigned int intValue, int padding = 0 );
00064 JString(int intValue, int padding = 0 );
00065 JString(long longValue, int padding = 0 );
00066 JString(unsigned long longValue, int padding = 0 );
00067 JString(long long longValue, int padding = 0 );
00068 JString(unsigned long long longValue, int padding = 0 );
00069 JString(double doubleValue , int afterdec = -1, int beforedec = -1);
00070 JString(const char *str = "");
00071 JString(char c);
00072 JString(const JString &str);
00073 JString(void* pointer);
00074
00075 virtual ~JString();
00076
00077 static JString format(char *formatstring, ...);
00078 static JString asciiMessage(const JString& title, const JString& content, unsigned int width = 0, const JString& borderchar = "*");
00079 static JString repeat(const JString& str, int count);
00080 static JString fill(const JString& str, int width);
00081
00082 Object* clone() const;
00083 JString print();
00084 unsigned long getHash() const;
00085 virtual bool unitTest();
00086
00087 virtual unsigned long getPayloadSize() const;
00088
00089 operator char*() const;
00090 operator const char*() const;
00091 operator void*();
00092 operator const void*();
00093
00094 char* getCharCopy(int &len) const;
00095 DotString toDotString() const;
00096 JTime toTime();
00097
00098 int operator ==(const char* chr) const;
00099 int operator !=(const char* chr) const;
00100 int operator ==(const JString &str) const;
00101 int operator !=(const JString &str) const;
00102 const JString& operator +=(const char*);
00103 const JString& operator +=(const char);
00104 const JString& operator +=(const unsigned char);
00105 const JString& operator =(const JString &str);
00106 const JString& operator +=(const JString &str);
00107 char operator [](unsigned int i) const;
00108 char& operator [](unsigned int i);
00109 char operator [](int i) const;
00110 char& operator [](int i);
00111 int operator <=(const JString &str) const;
00112 int operator >=(const JString &str) const;
00113 int operator < (const JString &str) const;
00114 int operator > (const JString &str) const;
00115 JString operator +(const JString& str);
00116 JString operator +(char* c);
00117 JString operator +(const JString& str) const;
00118 JString operator +(char* c) const;
00119
00120 bool startsWith( const JString &prefix ) const;
00121 bool startsWith( const JString &prefix, unsigned int toffset ) const;
00122 bool startsWithIgnoreCase( const JString &prefix ) const;
00123 bool startsWithIgnoreCase( const JString &prefix, unsigned int toffset ) const;
00124 bool startsOrEndsWithWhiteSpace() const;
00125 bool startsWithWhiteSpace() const;
00126 bool endsWithWhiteSpace() const;
00127 char charAt(unsigned int i) const;
00128 char getCharAt( unsigned int i ) const;
00129 bool setCharAt( unsigned int i, char ch );
00130 int compare(const Object* o2) const;
00131 int compareTo( const JString &anotherString ) const;
00132 const char* cstr( ) const { return Buffer; }
00133 const char* c_str( ) const { return Buffer; }
00134 const char* charpoint() const;
00135 bool endsWith( const JString &suffix ) const;
00136 bool endsWithIgnoreCase( const JString &suffix ) const;
00137 virtual bool equals(const Object* o2) const;
00138 bool equals( const JString &anObject ) const;
00139 bool equalsIgnoreCase( const JString &anotherString ) const;
00140 int indexOf( char ch ) const;
00141 int indexOf( char ch, unsigned int fromIndex ) const;
00142 int indexOf( const JString &str ) const;
00143 int indexOf( const JString &str, unsigned int fromIndex ) const;
00144 int indexOfIgnoreCase( const JString &str ) const;
00145 int indexOfIgnoreCase( const JString &str, unsigned int fromIndex ) const;
00146 int lastIndexOf( char ch ) const;
00147 int lastIndexOf( char ch, unsigned int fromIndex ) const;
00148 int lastIndexOf( const JString &str ) const;
00149 int lastIndexOf( const JString &str, unsigned int fromIndex ) const;
00150 int lastIndexOfIgnoreCase( const JString &str ) const;
00151 int lastIndexOfIgnoreCase( const JString &str, unsigned int fromIndex ) const;
00152 JString invertString() const;
00153 const unsigned int length( ) const { return Length; }
00154 JString substring( unsigned int beginIndex ) const;
00155 JString substring( unsigned int beginIndex, unsigned int endIndex ) const;
00156 JString toLowerCase( ) const;
00157 JString toUpperCase( ) const;
00158 JString trim( ) const;
00159 JString trimLeading( ) const;
00160 JString trimTrailing( ) const;
00161 JString trimCTRL() const;
00162 JString trimLines() const;
00163 JString trimLinesRemoveEmpty() const;
00164 JString concatLines() const;
00165
00166 bool contains(const JString& str) const;
00167 bool containsIgnoreCase(const JString& str) const;
00168 JString indent(int numSpaces) const;
00169 JString indentXML(int numTabs = 1) const;
00170 JString indentHTML(int numTabs = 1) const;
00171 bool looksLikeXML() const;
00172 bool isUTF() const;
00173 JString addUTFHeader() const;
00174 JString toXML();
00175 bool fromXML(const JString& xml);
00176 JString getFirst(int len);
00177 JString getLast(int len);
00178
00179
00180 Collection split(const JString& splitstr) const;
00181
00182
00183
00184
00185 Dictionary split(const JString& splitstr, const JString& equalstr) const;
00186
00187
00188 Collection splitOnWhiteSpaces() const;
00189
00190
00191 Collection splitLinesOnWidth(int maxWidth) const;
00192
00193
00194 Collection splitSingleLineOnWidth(int maxWidth) const;
00195
00196
00197 Dictionary splitCommandLine() const;
00198
00199
00200 Collection splitIntoLines() const;
00201
00202
00203
00204
00205 bool centerLines(unsigned int width = 0);
00206
00207 bool insert(JString str, int pos);
00208 bool removeChars(int count, int pos);
00209
00210 bool matchesWithRegExp(const JString& regExp) const;
00211 bool matchMyRegExpWith(const JString& text) const;
00212
00213 bool matches(const JString& match) const;
00214 bool matchesIgnoreCase(const JString& match) const;
00215
00216
00217 #ifdef WIN32
00218 LPWSTR lpwBuffer;
00219
00220 JString(wchar_t wch);
00221 JString(LPWSTR lstr);
00222
00223 operator LPWSTR();
00224 bool toUnicode();
00225 JString operator+(LPWSTR wstr);
00226 const JString& operator+=(LPWSTR wstr);
00227 JString operator+(wchar_t wch);
00228 const JString& operator+=(wchar_t wch);
00229
00230 operator const unsigned short*();
00231 bool utf2unicode();
00232 bool unicode2utf();
00233 #endif // WIN32
00234
00235
00236 void* toPointer() const;
00237 int toInt() const;
00238 long toLong() const;
00239 long long toLongLong() const;
00240 unsigned int toUnsignedInt() const;
00241 unsigned long toUnsignedLong() const;
00242 unsigned long long toUnsignedLongLong() const;
00243 double toDouble() const;
00244 bool toBool() const;
00245
00246 const JString& concat( const JString &str );
00247 void replace( char oldChar, char newChar);
00248
00249 int replace(const JString& match, const JString& replacement, bool all = true, bool recursive = false);
00250 void replace( int fromPos, int toPos, const JString& replacement);
00251
00252 int replaceIgnoreCase( const JString& match, const JString& replacement, bool all = true, bool recursive = false);
00253
00254 int scriptReplace(const JString& tagname, ObjectCollection* replacements);
00255
00256 int scriptReplaceDictionary(const JString& tagname, ObjectCollection* replacements);
00257
00258 JString scriptGetSection(const JString& tagname);
00259
00260 Dictionary scriptGetSectionParameters(const JString& tagname);
00261
00262 bool scriptReplaceSection(const JString& tagname, const JString& text);
00263
00264 int indexOfCount(const JString& match) const;
00265 int indexOfCountIgnoreCase(const JString& match) const;
00266
00267 JString removeCRs();
00268 JString removeCRTabs();
00269 JString replaceCRs();
00270 JString restoreCRs();
00271 JString toHTML() ;
00272 JString fromHTML();
00273 JString xmlStringEncode() const;
00274 JString xmlStringDecode() const;
00275 JString xml2html() const;
00276
00277 bool isCRTerminated;
00278 bool addCRLF();
00279
00280 bool setBufferSize(unsigned int size);
00281
00282
00283 static JString bytifySize(long val);
00284 static JString bytifySize(double val);
00285 static JString bytifySizes(long val1, long val2);
00286 static JString bytifySizes(double val1, double val2);
00287 static JString bytifyRate(long val);
00288 static JString bytifyRate(double val);
00289 static JString bytifyRates(long val1, long val2);
00290 static JString bytifyRates(double val1, double val2);
00291
00292 protected:
00293 char *Buffer;
00294 unsigned int BufferLen;
00295 unsigned int Length;
00296
00297 bool setBufferSizeInline(unsigned int size);
00298 bool doubleBufferInline();
00299
00300 };
00301
00302
00303 class Boolex : public Object {
00304 public:
00305 Boolex();
00306 Boolex(const JString& str);
00307 Boolex(XMLNode* node);
00308 virtual ~Boolex();
00309
00310 Object* clone() const;
00311 JString print();
00312 bool equals(const Object* o2) const;
00313 JString toText() const;
00314 bool reset();
00315
00316 JString toXML();
00317 bool fromXML(XMLNode* xml);
00318
00319 bool setExpression(const JString& str);
00320 bool parseExpression(const JString& str);
00321
00322 JString text;
00323 bool isNegated;
00324 Boolex* boolex;
00325 Collection* operators;
00326 ObjectCollection* boolexes;
00327
00328 JString strAND;
00329 JString strOR;
00330 JString strXOR;
00331 JString strNOT;
00332
00333 protected:
00334 JString enforceParentheses(JString str);
00335 bool parseParentheses(const JString& str);
00336
00337 bool isReservedWord(const JString& word);
00338 bool containsReservedWords(const JString& text);
00339 };
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 #define BIT_CAN_MATCH_NOTHING 0x00
00364 #define BIT_MORE_THAN_NULL 0x01
00365 #define BIT_OPENS_ON_STAR_OR_PLUS 0x04
00366 #define BIT_STAR_OR_PLUS_OK 0x02
00367
00368 #define CHAR_CLOSE_BRACKET ']'
00369 #define CHAR_CLOSE_PAREN ')'
00370 #define CHAR_DOLLAR '$'
00371 #define CHAR_DOUBLE_BSLASH '\\'
00372 #define CHAR_HOCH '^'
00373 #define CHAR_MINUS '-'
00374 #define CHAR_OPEN_BRACKET '['
00375 #define CHAR_OPEN_PAREN '('
00376 #define CHAR_OR '|'
00377 #define CHAR_PERIOD '.'
00378 #define CHAR_PLUS '+'
00379 #define CHAR_QUESTION '?'
00380 #define CHAR_STAR '*'
00381 #define CHAR_ZERO '\0'
00382
00383 #define MAK_AS_UNSIGNED(p) ((int)*(unsigned char *)(p))
00384 #define MAK_CHAR_2_OPCODE(p) (*(p))
00385 #define MAK_CONTROLLING_OPERAND(p) ((p)+3)
00386 #define MAK_GET_NEXT(p) (((*((p)+1)&0xFF)<<8)+(*((p)+2)&0xFF))
00387 #define MAK_IS_COMPOUND(c) ((c)==CHAR_STAR||(c)==CHAR_PLUS||(c)==CHAR_QUESTION)
00388
00389 #define MAX_STORE 1024
00390 #define MAX_SUB_EXPRESSIONS 10
00391
00392 #define OP_ANY 3
00393 #define OP_ANYBUT 5
00394 #define OP_ANYOF 4
00395 #define OP_BACK 7
00396 #define OP_BOL 1
00397 #define OP_BRANCH 6
00398 #define OP_CLOSE 30
00399 #define OP_END 0
00400 #define OP_EOL 2
00401 #define OP_EXACT 8
00402 #define OP_NMEMPTY 9
00403 #define OP_OPEN 20
00404 #define OP_PLUS 11
00405 #define OP_STAR 10
00406 #define SIGNATURE_VALUE 0x9C
00407
00408
00409 class RegExpParser {
00410 public:
00411
00412
00413 int an_error_has_occured;
00414
00415
00416 char error_string[ 255 ];
00417 public:
00418
00419
00420 RegExpParser( char *exp );
00421
00422
00423 int is_reg_exp_present( char *string );
00424
00425 private:
00426
00427
00428
00429 char *reg_exp_match_begins_here[ MAX_SUB_EXPRESSIONS ];
00430 char *reg_exp_match_ends_here[ MAX_SUB_EXPRESSIONS ];
00431 char first_char_of_regexp;
00432 char is_anchored;
00433 char *required_substring;
00434 int length_required_substring;
00435 char symbol_string[ MAX_STORE ];
00436 char *Input_scan_pointer;
00437 int parentheses_count;
00438 char reg_exp_shadow;
00439 char *code_emit_pointer;
00440 long symbol_string_size;
00441 char *input_string_pointer;
00442 char *beginning_of_input;
00443 char **pointer_to_rembh;
00444 char **pointer_to_remeh;
00445 char *Special_chars;
00446
00447
00448 void record_error( int n );
00449
00450 char *parse_parens( int paren, int *flagp );
00451
00452 char *one_side_of_or_operator( int *flagp );
00453
00454 char *trailing_wild( int *flagp );
00455
00456 char *compress_ordinary_characters( int *flagp );
00457
00458 char *emit_node( char op );
00459
00460 void emit_next_byte( char b );
00461
00462 void insert_operator( char op, char *opnd );
00463
00464 void go_to_end_of_chain( char *p, char *val );
00465
00466 void go_to_end_of_chain_on_operand( char *p, char *val );
00467
00468 int lookup_engine( char *prog );
00469
00470 int wildcard_lookup( char *p );
00471
00472 char *get_next_pointer( char *p );
00473
00474 int evaluate_next( char *string );
00475 };
00476
00477 }
00478
00479 #endif // _JSTRING_H_
00480
00481
00482
00483
00484