00001 /*************************************************************************** 00002 *cr 00003 *cr (C) Copyright 1995-2019 The Board of Trustees of the 00004 *cr University of Illinois 00005 *cr All Rights Reserved 00006 *cr 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * RCS INFORMATION: 00011 * 00012 * $RCSfile: JString.h,v $ 00013 * $Author: johns $ $Locker: $ $State: Exp $ 00014 * $Revision: 1.22 $ $Date: 2019年01月17日 21:20:59 $ 00015 * 00016 *************************************************************************** 00017 * DESCRIPTION: 00018 * A minimalistic string class we use instead of similar classes from the 00019 * STL or the GNU libraries for better portability and greatly reduced 00020 * code size. (only implements the functionality we actually need, doesn't 00021 * balloon the entire VMD binary as some past string class implementations 00022 * did). Implements regular expression matching methods used by VMD. 00023 ***************************************************************************/ 00024 00025 #ifndef JSTRING_H__ 00026 #define JSTRING_H__ 00027 00028 #include <string.h> 00029 00035 class JString { 00036 private: 00037 static char *defstr; 00038 char *rep; 00039 int do_free; 00040 00041 public: 00042 JString() 00043 : rep(defstr), do_free(0) {} 00044 00045 JString(const char *str) 00046 : rep(defstr), do_free(0) { 00047 if (str) { 00048 rep = new char[strlen(str)+1]; 00049 strcpy(rep, str); 00050 do_free = 1; 00051 } 00052 } 00053 JString(const JString& s) { 00054 rep = new char[strlen(s.rep)+1]; 00055 strcpy(rep, s.rep); 00056 do_free = 1; 00057 } 00058 ~JString() { if (do_free) delete [] rep; } 00059 00060 int operator==(const char *s) {return !strcmp(s,rep);} 00061 int operator!=(const char *s) {return strcmp(s,rep);} 00062 int operator<(const char *s) {return (strcmp(s,rep)<0);} 00063 int operator>(const char *s) {return (strcmp(s,rep)>0);} 00064 int operator<=(const char *s) {return (strcmp(s,rep)<=0);} 00065 int operator>=(const char *s) {return (strcmp(s,rep)>=0);} 00066 00067 JString& operator=(const char *); 00068 JString& operator=(const JString&); 00069 JString& operator=(const char); 00070 JString& operator+=(const char *); 00071 JString& operator+=(const JString&); 00072 JString& operator+=(const char); 00073 00074 friend int compare(const JString& s1, const JString& s2) { 00075 return strcmp(s1.rep, s2.rep); 00076 } 00077 00078 friend JString operator+(const char*, const JString&); 00079 JString operator+(const JString&) const; 00080 00081 int length() const { return (int) strlen(rep); } 00082 00083 operator const char *() const {return rep; } 00084 00085 // convert to uppercase 00086 void upcase(); 00087 00088 // convert to camel case (only first letter capitalized) 00089 void to_camel(); 00090 00091 // Replace all instances of pat with repl 00092 int gsub(const char *pat, const char *repl); 00093 00094 // remove the last n non-NULL characters from the end of the string 00095 void chop(int n); 00096 }; 00097 00098 #endif 00099