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.C,v $ 00013 * $Author: johns $ $Locker: $ $State: Exp $ 00014 * $Revision: 1.21 $ $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 #include "JString.h" 00026 #include <ctype.h> 00027 #include <stdio.h> 00028 00029 char *JString::defstr = (char *)""; 00030 00031 JString& JString::operator+=(const JString& s) { 00032 char *newrep = new char[length() + s.length() + 1]; 00033 ::strcpy(newrep, rep); 00034 ::strcat(newrep, s.rep); 00035 if (do_free) delete [] rep; 00036 rep = newrep; 00037 do_free = 1; 00038 return *this; 00039 } 00040 00041 00042 JString& JString::operator=(const JString& s) { 00043 if (rep != s.rep) { 00044 if (do_free) delete [] rep; 00045 rep = new char[s.length()+1]; 00046 ::strcpy(rep, s.rep); 00047 do_free = 1; 00048 } 00049 return *this; 00050 } 00051 00052 JString& JString::operator=(const char *s) { 00053 if (s == NULL) return *this; 00054 if (do_free) delete [] rep; 00055 rep = new char[strlen(s)+1]; 00056 ::strcpy(rep, s); 00057 do_free = 1; 00058 return *this; 00059 } 00060 00061 JString& JString::operator=(const char c) { 00062 if (do_free) delete [] rep; 00063 rep=new char[2]; 00064 rep[0]=c; 00065 rep[1]='0円'; 00066 do_free =1 ; 00067 return *this; 00068 } 00069 00070 JString& JString::operator+=(const char *s) { 00071 if (s==NULL) return *this; 00072 char *newrep = new char[length() + strlen(s) + 1]; 00073 ::strcpy(newrep, rep); 00074 ::strcat(newrep, s); 00075 if (do_free) delete [] rep; 00076 rep = newrep; 00077 do_free =1; 00078 return *this; 00079 } 00080 00081 JString& JString::operator+=(const char c) { 00082 char *newrep = new char[length() + 2]; 00083 ::strcpy(newrep,rep); 00084 newrep[length()]=c; 00085 newrep[length()+1]='0円'; 00086 if (do_free) delete [] rep; 00087 rep=newrep; 00088 do_free = 1; 00089 return *this; 00090 } 00091 00092 JString operator+(const char* s, const JString& S) { 00093 JString retval; 00094 retval.rep = new char[::strlen(s) + S.length() + 1]; 00095 ::strcpy(retval.rep, s); 00096 ::strcat(retval.rep, S.rep); 00097 retval.do_free = 1; 00098 return retval; 00099 } 00100 00101 JString JString::operator+(const JString& s) const { 00102 JString retval; 00103 retval.rep = new char[length() + s.length() + 1]; 00104 ::strcpy(retval.rep, rep); 00105 ::strcat(retval.rep, s.rep); 00106 retval.do_free = 1; 00107 return retval; 00108 } 00109 00110 void JString::upcase() { 00111 char *s = rep; 00112 while (*s != '0円') { 00113 *s=toupper(*s); 00114 s++; 00115 } 00116 } 00117 00118 void JString::to_camel() { 00119 char *s = rep; 00120 int have_first = 0; 00121 while (*s) { 00122 if (have_first) { 00123 *s = tolower(*s); 00124 } else { 00125 if (isalpha(*s)) { 00126 *s = toupper(*s); 00127 have_first = 1; 00128 } 00129 } 00130 s++; 00131 } 00132 } 00133 00134 int JString::gsub(const char *pat, const char *repl) { 00135 char *found; 00136 int patn = strlen(pat); 00137 int repln = strlen(repl); 00138 int ind = 0; 00139 int nreplace = 0; 00140 while ((found = strstr(rep+ind, pat)) != NULL) { 00141 int loc = found - rep; 00142 if (repln > patn) { 00143 char *tmp = new char[length() + repln + 1]; 00144 strcpy(tmp, rep); 00145 if (do_free) delete [] rep; 00146 rep = tmp; 00147 found = rep+loc; 00148 do_free = 1; 00149 } 00150 memmove(found+repln, found+patn, strlen(found+patn)+1); 00151 memcpy(found, repl, repln); 00152 ind = loc + repln; 00153 nreplace++; 00154 } 00155 return nreplace; 00156 } 00157 00158 void JString::chop(int n) { 00159 for (int i=length()-1; n > 0 && i >= 0; --n) 00160 rep[i] = '0円'; 00161 } 00162