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
00043
00044 #include <sysdep.h>
00045 #include "libjwgc.h"
00046 #include "libxode.h"
00047
00048 #ifdef HAVE_ICONV_H
00049 #include <iconv.h>
00050 #else
00051 #undef iconv_t
00052 #define iconv_t libiconv_t
00053 typedef void* iconv_t;
00054 extern iconv_t iconv_open (const char* tocode, const char* fromcode);
00055 extern size_t iconv (iconv_t cd, const char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft);
00056 extern int iconv_close (iconv_t cd);
00057 #endif
00058
00059 #ifdef HAVE_LIBCHARSET_H
00060 #include <libcharset.h>
00061 #else
00062 extern const char * locale_charset (void);
00063 #endif
00064
00065 char *
00066 j_strdup(const char *str)
00067 {
00068 if (str == NULL)
00069 return NULL;
00070 else
00071 return strdup(str);
00072 }
00073
00074 char *
00075 j_strcat(char *dest, char *txt)
00076 {
00077 if (!txt)
00078 return (dest);
00079
00080 while (*txt)
00081 *dest++ = *txt++;
00082 *dest = '\0';
00083
00084 return (dest);
00085 }
00086
00087 int
00088 j_strcmp(const char *a, const char *b)
00089 {
00090 if (a == NULL || b == NULL)
00091 return -1;
00092
00093 while (*a == *b && *a != '\0' && *b != '\0') {
00094 a++;
00095 b++;
00096 }
00097
00098 if (*a == *b)
00099 return 0;
00100
00101 return -1;
00102 }
00103
00104 int
00105 j_strcasecmp(const char *a, const char *b)
00106 {
00107 if (a == NULL || b == NULL)
00108 return -1;
00109 else
00110 return strcasecmp(a, b);
00111 }
00112
00113 int
00114 j_strncmp(const char *a, const char *b, int i)
00115 {
00116 if (a == NULL || b == NULL)
00117 return -1;
00118 else
00119 return strncmp(a, b, i);
00120 }
00121
00122 int
00123 j_strncasecmp(const char *a, const char *b, int i)
00124 {
00125 if (a == NULL || b == NULL)
00126 return -1;
00127 else
00128 return strncasecmp(a, b, i);
00129 }
00130
00131 int
00132 j_strlen(const char *a)
00133 {
00134 if (a == NULL)
00135 return 0;
00136 else
00137 return strlen(a);
00138 }
00139
00140 int
00141 j_atoi(const char *a, int def)
00142 {
00143 if (a == NULL)
00144 return def;
00145 else
00146 return atoi(a);
00147 }
00148
00149 void
00150 trim_message(str)
00151 char *str;
00152 {
00153 int pos = strlen(str);
00154
00155 dprintf(dExecution, "Trim start position at %d.\n", pos);
00156 while (pos >= 0 && (isspace(str[pos]) || iscntrl(str[pos]))) {
00157 dprintf(dExecution, "Trimming position %d.\n", pos);
00158 str[pos] = '\0';
00159 pos--;
00160 }
00161 }
00162
00163 char *
00164 spool_print(xode_spool s)
00165 {
00166 char *ret, *tmp;
00167 struct xode_spool_node *next;
00168
00169 if (s == NULL || s->len == 0 || s->first == NULL)
00170 return NULL;
00171
00172 ret = xode_pool_malloc(s->p, s->len + 1);
00173 *ret = '\0';
00174
00175 next = s->first;
00176 tmp = ret;
00177 while (next != NULL) {
00178 tmp = j_strcat(tmp, next->c);
00179 next = next->next;
00180 }
00181
00182 return ret;
00183 }
00184
00185
00186 char *
00187 spools(xode_pool p,...)
00188 {
00189 va_list ap;
00190 xode_spool s;
00191 char *arg = NULL;
00192
00193 if (p == NULL)
00194 return NULL;
00195
00196 s = xode_spool_newfrompool(p);
00197
00198 va_start(ap, p);
00199
00200
00201 while (1) {
00202 arg = va_arg(ap, char *);
00203 if ((xode_pool) arg == p)
00204 break;
00205 else
00206 xode_spool_add(s, arg);
00207 }
00208
00209 va_end(ap);
00210
00211 return spool_print(s);
00212 }
00213
00214 int
00215 str_clear_unprintable(in, out)
00216 const char *in;
00217 char **out;
00218 {
00219 int i;
00220
00221 dprintf(dExecution, "Clearing unprintable characters...\n");
00222 *out = (char *)malloc(sizeof(char) * (strlen(in) + 1));
00223 for (i = 0; i < strlen(in); i++) {
00224 if (in[i] != '\n' && !isprint(in[i])) {
00225 (*out)[i] = ' ';
00226 }
00227 else {
00228 (*out)[i] = in[i];
00229 }
00230 }
00231
00232 return 1;
00233 }
00234
00235 int
00236 str_to_unicode(in, out)
00237 const char *in;
00238 char **out;
00239 {
00240 #ifdef HAVE_LIBICONV
00241 iconv_t ic;
00242 size_t ret;
00243 int inlen, outlen;
00244 char *inptr, *outbuf, *outptr;
00245 extern int errno;
00246
00247 dprintf(dExecution, "Converting localized string to unicode...\n");
00248 ic = iconv_open("UTF-8", (char *)locale_charset());
00249 if (ic == (iconv_t)-1) {
00250 return str_clear_unprintable(in, out);
00251 }
00252
00253 inptr = (char *)in;
00254 inlen = strlen(in);
00255
00256 outbuf = (char *)malloc(sizeof(char) * ((inlen * 4) + 1));
00257 outbuf[0] = '\0';
00258 outptr = outbuf;
00259 outlen = inlen;
00260 do {
00261 ret = iconv(ic,
00262 (const char **)&inptr, (size_t *)&inlen,
00263 (char **)&outptr, (size_t *)&outlen);
00264 if (ret == (size_t) -1) {
00265 if (errno == EINVAL) {
00266 continue;
00267 }
00268 }
00269 outptr[inlen] = '\0';
00270 } while(inlen > 0);
00271 iconv_close(ic);
00272
00273 *out = outbuf;
00274 return 1;
00275 #else
00276 return str_clear_unprintable(in, out);
00277 #endif
00278 }
00279
00280 int
00281 unicode_to_str(in, out)
00282 const char *in;
00283 char **out;
00284 {
00285 #ifdef HAVE_LIBICONV
00286 iconv_t ic;
00287 size_t ret;
00288 int inlen, outlen;
00289 char *inptr, *outbuf, *outptr;
00290 extern int errno;
00291
00292 dprintf(dExecution, "Converting unicode to localized string...\n");
00293 ic = iconv_open((char *)locale_charset(), "UTF-8");
00294 if (ic == (iconv_t)-1) {
00295 *out = (char *)strdup(in);
00296 return 1;
00297 }
00298
00299 inptr = (char *)in;
00300 inlen = strlen(in);
00301
00302 outbuf = (char *)malloc(sizeof(char) * (inlen + 1));
00303 outbuf[0] = '\0';
00304 outptr = outbuf;
00305 outlen = inlen;
00306 do {
00307 ret = iconv(ic,
00308 (const char **)&inptr, (size_t *)&inlen,
00309 (char **)&outptr, (size_t *)&outlen);
00310 if (ret == (size_t) -1) {
00311 if (errno == EINVAL) {
00312 continue;
00313 }
00314 }
00315 outptr[inlen] = '\0';
00316 } while(inlen > 0);
00317 iconv_close(ic);
00318
00319 *out = outbuf;
00320 return 1;
00321 #else
00322 return in;
00323 #endif
00324 }