00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045 #include <ctype.h>
00046 #include <assert.h>
00047 #include <stdarg.h>
00048
00049 #include "ckd_alloc.h"
00050 #include "strfuncs.h"
00051
00052
00053 double sb_strtod(const char *s00, char **se);
00054
00055 double
00056 atof_c(char const *str)
00057 {
00058 return sb_strtod(str, NULL);
00059 }
00060
00061 char *
00062 string_join(const char *base, ...)
00063 {
00064 va_list args;
00065 size_t len;
00066 const char *c;
00067 char *out;
00068
00069 va_start(args, base);
00070 len = strlen(base);
00071 while ((c = va_arg(args, const char *)) != NULL) {
00072 len += strlen(c);
00073 }
00074 len++;
00075 va_end(args);
00076
00077 out = ckd_calloc(len, 1);
00078 va_start(args, base);
00079 strcpy(out, base);
00080 while ((c = va_arg(args, const char *)) != NULL) {
00081 strcat(out, c);
00082 }
00083 va_end(args);
00084
00085 return out;
00086 }
00087
00088 char *
00089 string_trim(char *string, enum string_edge_e which)
00090 {
00091 size_t len;
00092
00093 len = strlen(string);
00094 if (which == STRING_START || which == STRING_BOTH) {
00095 size_t sub = strspn(string, " \t\n\r\f");
00096 if (sub > 0) {
00097 memmove(string, string + sub, len + 1 - sub);
00098 len -= sub;
00099 }
00100 }
00101 if (which == STRING_END || which == STRING_BOTH) {
00102 long sub = len;
00103 while (--sub >= 0)
00104 if (strchr(" \t\n\r\f", string[sub]) == NULL)
00105 break;
00106 if (sub == -1)
00107 string[0] = '\0';
00108 else
00109 string[sub+1] = '\0';
00110 }
00111 return string;
00112 }
00113
00114 int32
00115 str2words(char *line, char **ptr, int32 max_ptr)
00116 {
00117 int32 i, n;
00118
00119 n = 0;
00120 i = 0;
00121 while (1) {
00122
00123 while (line[i] && isspace((unsigned char)line[i]))
00124 ++i;
00125 if (!line[i])
00126 break;
00127
00128 if (ptr != NULL && n >= max_ptr) {
00129
00130
00131
00132
00133 for (; i >= 0; --i)
00134 if (line[i] == '\0')
00135 line[i] = ' ';
00136
00137 return -1;
00138 }
00139
00140
00141 if (ptr != NULL)
00142 ptr[n] = line + i;
00143 ++n;
00144 while (line[i] && !isspace((unsigned char)line[i]))
00145 ++i;
00146 if (!line[i])
00147 break;
00148 if (ptr != NULL)
00149 line[i] = '\0';
00150 ++i;
00151 }
00152
00153 return n;
00154 }
00155
00156
00157 int32
00158 nextword(char *line, const char *delim, char **word, char *delimfound)
00159 {
00160 const char *d;
00161 char *w;
00162
00163
00164 for (w = line; *w; w++) {
00165 for (d = delim; *d && (*d != *w); d++);
00166 if (!*d)
00167 break;
00168 }
00169 if (!*w)
00170 return -1;
00171
00172 *word = w;
00173
00174
00175 for (w++; *w; w++) {
00176 for (d = delim; *d && (*d != *w); d++);
00177 if (*d)
00178 break;
00179 }
00180
00181
00182 *delimfound = *w;
00183 *w = '\0';
00184
00185 return (w - *word);
00186 }