00001
00009 #include <string.h>
00010 #include <stdlib.h>
00011 #include "rstring.h"
00012
00013
00014 int delISpaces(char* str)
00015 {
00016 int i,j;
00017
00018 if(!str) return -1;
00019 else
00020 {
00021 for(j=0;str[j]==' ';j++);
00022
00023 if(j)
00024 {
00025 for(i=0;str[j];i++,j++)
00026 str[i]=str[j];
00027
00028 str[i]=str[j];
00029
00030 return i;
00031 }
00032 else return strlen(str);
00033 }
00034 }
00035
00036
00037
00038 int delCSpaces(char* str)
00039 {
00040 int i,j;
00041
00042 if(!str) return -1;
00043 else
00044 {
00045 for(i=0,j=1;str[i+1];i++)
00046 if(str[i]!=' '||str[i+1]!=' ')
00047 {
00048 str[j]=str[i+1];
00049 j++;
00050 }
00051
00052 str[j]=str[i+1];
00053
00054 return j;
00055 }
00056 }
00057
00058
00059
00060 int delESpaces(char* str)
00061 {
00062 int i;
00063
00064 if(!str) return -1;
00065 else
00066 {
00067 for(i=strlen(str);str[i-1]==' ';i--)
00068 str[i-1]='\0';
00069
00070 return i;
00071 }
00072 }
00073
00074
00075
00076 int delSpaces(char* str)
00077 {
00078 int i,j;
00079
00080 if(!str) return -1;
00081 else
00082 {
00083 for(i=0;str[i]==' ';i++);
00084
00085 for(j=0;str[i];i++)
00086 if(str[i]!=' '||str[i+1]!=' ')
00087 {
00088 str[j]=str[i];
00089 j++;
00090 }
00091
00092 if(str[j-1]==' ')
00093 {
00094 str[j-1]='\0';
00095 return(j-1);
00096 }
00097 else
00098 {
00099 str[j]='\0';
00100 return j;
00101 }
00102 }
00103 }
00104
00105
00106
00107 int charElem(char c,const char* str)
00108 {
00109 int i,res;
00110
00111 if(!str) return 0;
00112 else
00113 {
00114 for(i=0,res=0;str[i]&&!res;i++)
00115 res=(c==str[i]);
00116
00117 return res;
00118 }
00119 }
00120
00121
00122
00123 List words(const char* str)
00124 {
00125 List l;
00126 char *aux,*sub,*tmp;
00127 int i,err;
00128
00129 if(!str) return NULL;
00130 else if(!str[0]) return newList();
00131 else
00132 {
00133 aux=strdup(str);
00134 delSpaces(aux);
00135 sub=&aux[0];
00136 l=newList();
00137
00138 for(i=0,err=0;aux[i]&&!err;i++)
00139 if(aux[i]==' ')
00140 {
00141 aux[i]='\0';
00142 tmp=strdup(sub);
00143 err=listInsertLst(l,tmp);
00144 sub=&aux[i+1];
00145 }
00146 tmp=strdup(sub);
00147 err=listInsertLst(l,tmp)&&err;
00148
00149 if(err)
00150 {
00151 listMap(l,free);
00152 listDelete(l);
00153 return NULL;
00154 }
00155 else
00156 {
00157 free(aux);
00158 return l;
00159 }
00160 }
00161 }
00162
00163
00164
00165 List strSep(const char* str,const char* delim)
00166 {
00167 List l;
00168 char *aux,*tmp,*sub;
00169 int i,err;
00170
00171 if(!str) return NULL;
00172 else if(!str[0]) return newList();
00173 else
00174 {
00175 aux=strdup(str);
00176 sub=&aux[0];
00177 l=newList();
00178
00179 for(i=0,err=0;aux[i]&&!err;i++)
00180 if(1==charElem(aux[i],delim))
00181 {
00182 aux[i]='\0';
00183 if(sub[0]!=aux[i])
00184 {
00185 tmp=strdup(sub);
00186 err=listInsertLst(l,tmp);
00187 }
00188 sub=&aux[i+1];
00189 }
00190 if(sub[0]!=aux[i])
00191 {
00192 tmp=strdup(sub);
00193 err=listInsertLst(l,tmp)&&err;
00194 }
00195
00196 if(err)
00197 {
00198 listMap(l,free);
00199 listDelete(l);
00200 return NULL;
00201 }
00202 else
00203 {
00204 free(aux);
00205 return l;
00206 }
00207 }
00208 }