00001
00016
00017
00018 #include "arr_utils.h"
00019
00020 #include "globals.h"
00021 #include <stdarg.h>
00022
00028 void strip_crlf(char *string_in)
00029 {
00030 int i;
00031 unsigned char *string = (unsigned char *) string_in;
00032
00033 for(i=strlen((char *)string)-1;i>=0;i--)
00034 {
00035 if(string[i]<32)
00036 string[i]=0;
00037 else
00038 break;
00039 }
00040 }
00041
00048 char *trim_right(char *string_in)
00049 {
00050 int i;
00051 unsigned char *string = (unsigned char *) string_in;
00052
00053 for(i=strlen((char *)string)-1;i>=0;i--)
00054 {
00055 if(string[i]<=32)
00056 string[i]='\0';
00057 else
00058 break;
00059 }
00060 return (char *)string;
00061 }
00062
00069 char *trim_left(char *string_in)
00070 {
00071 int i;
00072 unsigned char *string = (unsigned char *) string_in;
00073
00074 while (string[0]!='\0')
00075 {
00076 if(string[0]<=32)
00077 string++;
00078 else
00079 break;
00080 }
00081 return (char *)string;
00082 }
00083
00093 char *strdup_noquot(char *src)
00094 {
00095 char *trimsrc;
00096 trimsrc=src;
00097 while ((trimsrc[0]>0)&&(trimsrc[0]<=32)) trimsrc++;
00098 if ((trimsrc[0]=='\"')||(trimsrc[0]=='\''))
00099 trimsrc++;
00100 else
00101 trimsrc=src;
00102 int len=strlen(trimsrc);
00103 int trimlen;
00104 trimlen=len;
00105 while (((unsigned char)(trimsrc[trimlen-1])<=32)) trimlen--;
00106 if ((trimsrc[trimlen-1]=='\"')||(trimsrc[trimlen-1]=='\''))
00107 trimlen--;
00108 else
00109 trimlen=len;
00110 char *dest=malloc(trimlen+1);
00111 if (dest==NULL) return NULL;
00112 strncpy(dest,trimsrc,trimlen);
00113 dest[trimlen]='\0';
00114 return dest;
00115 }
00116
00124 char *strdup_trim(char *src)
00125 {
00126 char *trimsrc;
00127 trimsrc=src;
00128 while ((trimsrc[0]>0)&&(trimsrc[0]<=32)) trimsrc++;
00129 int len=strlen(trimsrc);
00130 int trimlen;
00131 trimlen=len;
00132 while ((((unsigned char)(trimsrc[trimlen-1]))<=32)) trimlen--;
00133 char *dest=malloc(trimlen+1);
00134 if (dest==NULL) return NULL;
00135 strncpy(dest,trimsrc,trimlen);
00136 dest[trimlen]='\0';
00137 return dest;
00138 }
00139
00146 char *filename_from_path(char *pathname)
00147 {
00148 char *fname = NULL;
00149 if (pathname)
00150 {
00151 fname = strrchr (pathname, '/') + 1;
00152 if (!fname)
00153 fname = strrchr (pathname, '\\') + 1;
00154 }
00155 if (!fname)
00156 fname=pathname;
00157 return fname;
00158 }
00159
00168 short format_map_fname(char *fname, const char *usrinput,const char *levels_path)
00169 {
00170
00171 short has_path=(strchr(usrinput,'\\')!=NULL);
00172 if (!has_path)
00173 has_path=(strchr(usrinput,'/')!=NULL);
00174 if (!has_path)
00175 {
00176 const char *usedpath;
00177 if ((levels_path==NULL)||(levels_path[0]=='\0'))
00178 usedpath=".";
00179 else
00180 usedpath=levels_path;
00181
00182 char *dotpos=strrchr(usrinput,'.');
00183 if (dotpos!=NULL)
00184 {
00185 int len=dotpos-usrinput;
00186 strncpy(fname,usrinput,len);
00187 fname[len]='\0';
00188 } else
00189
00190 {
00191 if (atoi(usrinput))
00192 sprintf (fname, "%s"SEPARATOR"map%.5d", usedpath, atoi(usrinput));
00193 else
00194 sprintf(fname, "%s"SEPARATOR"%s", usedpath, usrinput);
00195 }
00196 } else
00197
00198 {
00199 sprintf(fname, "%s", usrinput);
00200 }
00201 if (strlen(fname)>0) return true;
00202 return false;
00203 }
00204
00214 char *prepare_short_fname(const char *fname, unsigned int maxlen)
00215 {
00216 const char *start;
00217 start=strrchr(fname,SEPARATOR[0]);
00218 if (start==NULL)
00219 start=fname;
00220 else
00221 start++;
00222 if (start[0]=='\0')
00223 start=fname;
00224 unsigned int len=strlen(start);
00225 if (len > maxlen)
00226 len=maxlen;
00227 char *retname;
00228 if (len>0)
00229 retname=(char *)malloc(len+1);
00230 else
00231 retname=NULL;
00232 if (retname==NULL)
00233 {
00234 message_error("prepare_short_fname: Cannot allocate memory.");
00235 return NULL;
00236 }
00237 strncpy(retname,start,len);
00238 retname[len]='\0';
00239 return retname;
00240 }
00241
00251 short format_data_fname(char **fullname, const char *data_path, const char *format, ...)
00252 {
00253 va_list val;
00254 va_start(val, format);
00255 char *file = (char *)malloc(DISKPATH_SIZE*sizeof(char));
00256 if (file!=NULL)
00257 vsprintf(file, format, val);
00258 va_end(val);
00259 if (file==NULL)
00260 return false;
00261 *fullname = (char *)malloc(strlen(file)+strlen(data_path)+4);
00262 if (*fullname==NULL)
00263 {
00264 message_error("format_data_fname: Out of memory.");
00265 return false;
00266 }
00267 sprintf(*fullname, "%s"SEPARATOR"%s", data_path, file);
00268 free(file);
00269 if (strlen(*fullname)>strlen(data_path)) return true;
00270 return false;
00271 }
00272
00280 int arr_ushort_pos(const unsigned short *arr,unsigned short arr_item,int array_count)
00281 {
00282 int i;
00283 for (i=0;i<array_count;i++)
00284 if (arr[i]==arr_item) return i;
00285 return -1;
00286 }