Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

utilstr.cpp

00001 #include <string.h>
00002 #include <utilstr.h>
00003 #include <ctype.h>
00004 
00005 /******************************************************************************
00006  * stdstr - Sets/gets a string
00007  *
00008  * ENT: ipstr   - pointer to a string pointer to set if necessary
00009  *      istr    - string to set to *ipstr
00010  *                      0 - only get
00011  *
00012  * RET: *ipstr
00013  */
00014 
00015 char *stdstr(char **ipstr, const char *istr) {
00016         if (istr) {
00017                 if (*ipstr)
00018                         delete [] *ipstr;
00019                 int len = strlen(istr) + 1;
00020                 *ipstr = new char [ len ];
00021                 memcpy(*ipstr, istr, len);
00022         }
00023         return *ipstr;
00024 }
00025 
00026 
00027 /******************************************************************************
00028  * strstrip - Removes leading and trailing spaces from a string
00029  *
00030  * ENT: istr    - string pointer to strip
00031  *
00032  * RET: *istr
00033  */
00034 
00035 char *strstrip(char *istr) {
00036         char *tmp = istr;
00037         char *rtmp;
00038 
00039         int len = strlen(istr);
00040         if (len < 1)
00041                 return istr;
00042         rtmp = istr + (len - 1);
00043         
00044         while ((*rtmp == ' ')||(*rtmp == '\t')||(*rtmp == 10)||(*rtmp == 13)) *(rtmp--) = 0;
00045         while ((*tmp == ' ')||(*tmp == '\t')||(*tmp == 10)||(*tmp == 13)) tmp++;
00046         memmove(istr, tmp, (rtmp - tmp) + 1);
00047         istr[(rtmp - tmp) + 1] = 0;
00048 
00049         return istr;
00050 }
00051 
00052 
00053 /******************************************************************************
00054  * stristr - Scans a string for the occurrence of a given substring, no case
00055  *
00056  * ENT: scans s1 for the first occurrence of the substring s2, ingnoring case
00057  *
00058  * RET: a pointer to the element in s1, where s2 begins (points to s2 in s1).
00059  *                      If s2 does not occur in s1, returns null.
00060  */
00061 
00062 const char *stristr(const char *s1, const char *s2) {
00063         int tLen = strlen(s2);
00064         int cLen = strlen(s1);
00065         char *target = new char [ tLen + 1 ];
00066         int i, j;
00067         const char *retVal = 0;
00068 
00069         strcpy(target, s2);
00070         for (i = 0; i < tLen; i++)
00071                 target[i] = SW_toupper(target[i]);
00072 
00073         for (i = 0; i < (cLen - tLen)+1; i++) {
00074                 if (SW_toupper(s1[i]) == (unsigned char)*target) {
00075                         for (j = 1; j < tLen; j++) {
00076                                 if (SW_toupper(s1[i+j]) != (unsigned char)target[j])
00077                                         break;
00078                         }
00079                         if (j == tLen) {
00080                                 retVal = s1+i;
00081                                 break;
00082                         }
00083                 }
00084         }
00085         delete [] target;
00086         return retVal;
00087 }
00088 
00089 /******************************************************************************
00090  * strnicmp - compares the first n bytes of 2 string ignoring case
00091  *
00092  * ENT: compares s1 to s2 comparing the first n byte ingnoring case
00093  *
00094  * RET: same as strcmp
00095  */
00096 
00097 const char strnicmp(const char *s1, const char *s2, int len) {
00098 
00099      int tLen = strlen(s2);
00100      int cLen = strlen(s1);
00101      char diff;
00102      int i;
00103      for (i = 0; ((i < len) && (i < tLen) && (i < cLen)); i++) {
00104         if ((diff = SW_toupper(*s1) - SW_toupper(*s2)))
00105                 return diff;
00106         s1++;
00107         s2++;
00108      }
00109      return (i < len) ? cLen - tLen : 0;
00110 }
00111 
00112 /******************************************************************************
00113  * strlenw - Scans a string for trailing 0x0000 and return size in BYTES
00114  *
00115  * ENT: target - string for which to determine size
00116  *
00117  * RET: length in BYTES 
00118  *                      If s2 does not occur in s1, returns null.
00119  */
00120 
00121 unsigned int strlenw(const char *s1) {
00122         return strlen(s1);
00123 // utf8 says no null in string except terminator, so below code is overkill
00124 /*
00125         const char *ch = s1;
00126         if (!*ch)
00127                 ch++;
00128         while (*ch) {
00129                 ch++;
00130                 if (!*ch)
00131                         ch++;
00132         }
00133         return (unsigned int)(ch - s1) - 1;
00134 */
00135 }
00136 
00137 
00138 /******************************************************************************
00139  * toupperstr - converts a string to uppercase string
00140  *
00141  * ENT: target - string to convert
00142  *
00143  * RET: target
00144  */
00145 
00146 char *toupperstr(char *buf) {
00147         char *ret = buf;
00148         while (*buf)
00149                 *buf = SW_toupper(*buf++);
00150 
00151         return ret;
00152 }

Generated on Thu Jun 20 22:13:01 2002 for The Sword Project by doxygen1.2.15