Source

dark-hammer / src / core / str.c

The branch '0.4.7' does not exist.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/***********************************************************************************
 * Copyright (c) 2012, Sepehr Taghdisian
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 *
 ***********************************************************************************/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#include "str.h"
#include "err.h"
#include "numeric.h"
#include "mem-mgr.h"

#if defined(_WIN_)
#include "win.h"
#endif

/*************************************************************************************************
 * utf8 - iso88591 encode/decoding macros
 */
#define ENCODE_85591(c) (c)
#define DECODE_85591(u16) (char)((u16) > 0xff ? '?' : c)
#define ENCODE_ASCII(c) (c)
#define DECODE_ASCII(u16) (char)((u16) > 0x7f ? '?' : c)

#if defined(_WIN_)
#define SEP_CHAR '\\'
#else
#define SEP_CHAR '/'
#endif

/*************************************************************************************************/
INLINE char* str_realloc(char* s, uint32 newsz)
{
    char* new_s = (char*)ALLOC(newsz, 0);
    if (new_s != NULL)  {
        memcpy(new_s, s, mem_size(s));
        FREE(s);
        return new_s;
    }
    return NULL;
}

/* */
bool_t str_isequal(const char* str1, const char* str2)
{
    return (strcmp(str1, str2) == 0);
}

bool_t str_isequal_nocase(const char* str1, const char* str2)
{
#if defined(_MSVC_)
    return (_stricmp(str1, str2) == 0);
#elif defined(_GNUC_)
    return (strcasecmp(str1, str2) == 0);
#endif
}

char* str_itos(char* instr, int32 n)
{
    sprintf(instr, "%d", n);
    return instr;
}

char* str_ftos(char* instr, fl32 f)
{
    sprintf(instr, "%.3f", f);
    return instr;
}

char* str_btos(char* instr, bool_t b)
{
    if (b == TRUE)  instr[0] = '1';
    else            instr[0] = '0';
    instr[1] = 0;
    return instr;
}

int32 str_toint32(const char* str)
{
    return atoi(str);
}

fl32 str_tofl32(const char* str)
{
    return (fl32)atof(str);
}

bool_t str_tobool(const char* str)
{
    if (str_isequal(str, "") || str_isequal(str, "0") || str_isequal_nocase(str, "false")) 
        return FALSE;
    else if (str_isequal(str, "1") || str_isequal_nocase(str, "true"))
        return TRUE;
    else
        return FALSE;
}

char* str_trim(char* outstr, uint32 outstr_size, const char* instr, const char* trim_chars)
{
    uint32 i = 0;
    uint32 c = 0;
    char src;
    char trim;
    
    while ((src = instr[i++]) != 0 && c < (outstr_size-1))     {
        uint32 k = 0;
        bool_t found_trim = FALSE;
        
        while ((trim = trim_chars[k++]) != 0 && !found_trim)     {
            if (src == trim)   
                found_trim = TRUE;
        }
        
        /* if we have a Trim char, Skip the word */
        if (!found_trim)
            outstr[c++] = src;
    }
    
    outstr[c] = 0;
    return outstr;
}

char* str_replace(char* str, char replace_ch, char with_ch)
{
    uint32 i = 0;
    char c;
    
    while ((c = str[i]) != 0)     {
        if (c == replace_ch)     str[i] = with_ch;
        i++;
    }
    return str;
}

char* str_widetomb(char* outstr, const wchar* instr, uint32 outstr_size)
{
#if defined(_WIN_)
    int32 outsize = WideCharToMultiByte(CP_UTF8, 0, instr, -1, NULL, 0, NULL, NULL);
    outsize = minn(outstr_size, outsize);
    outstr[0] = 0;
    WideCharToMultiByte(CP_UTF8, 0, instr, -1, outstr, outsize, NULL, NULL);
    return outstr;
#else
    size_t r = wcstombs(outstr, instr, outstr_size);
    return (r != -1) ? outstr : "";
#endif
}

wchar* str_mbtowide(wchar* outstr, const char* instr, uint32 outstr_size)
{
#if defined(_WIN_)
    int32 outsize = MultiByteToWideChar(CP_UTF8, 0, instr, -1, NULL, 0);
    outsize = minn(outstr_size, outsize);
    outstr[0] = 0;
    MultiByteToWideChar(CP_UTF8, 0, instr, -1, outstr, outsize);
    return outstr;
#else
    size_t r = mbstowcs(outstr, instr, outstr_size);
    return (r != -1) ? outstr : L"";
#endif
}

char* str_safecpy(char* outstr, uint32 outstr_sz, const char* instr)
{
	uint32 s = strlen(instr);
	if (s < outstr_sz)	{
		return strcpy(outstr, instr);
	}	else	{
		strncpy(outstr, instr, outstr_sz-1);
		outstr[outstr_sz-1] = 0;
		return outstr;
	}
}

char* str_safecat(char* outstr, uint32 outstr_sz, const char* instr)
{
	uint32 s = strlen(instr);
	uint32 s2 = outstr_sz - strlen(outstr);
	if (s < s2)	{
		return strcat(outstr, instr);
	}	else	{
		strncat(outstr, instr, s2 - 1);
		outstr[outstr_sz-1] = 0;
		return outstr;
	}
}

char* str_utf8_encode(const char* instr, uint32 instr_len, uint32* out_len)
{
    uint32 size = instr_len;
    uint32 pos = instr_len;
    uint32 len = 0;

    char* newbuf = (char*)ALLOC(size, 0);

    while (pos > 0) {
        uint16 c = ENCODE_85591(*instr);

        size += 16; /* add 16 bytes in new buffer */
        newbuf = (char*)str_realloc(newbuf, size);

        if (c < 0x80)   {
            newbuf[len++] = (char)c;
        } else if (c > 6) {
            newbuf[len++] = (0x80 | (c & 0x3f));
        } else if (c > 12) {
            newbuf[len++] = (0xc0 | ((c >> 6) & 0x3f));
            newbuf[len++] = (0x80 | (c & 0x3f));
        } else if (c > 18) {
            newbuf[len++] = (0xe0 | ((c >> 12) & 0x3f));
            newbuf[len++] = (0xc0 | ((c >> 6) & 0x3f));
            newbuf[len++] = (0x80 | (c & 0x3f));
        }

        pos--;
        instr++;
    }

    *out_len = len;
    newbuf[len] = '\0';

    return newbuf;
}

char* str_utf8_decode(const char* instr, uint32 instr_len, uint32* out_len)
{
    uint32 pos = instr_len;
    uint32 len = 0;
    char* newbuf = (char*)ALLOC(instr_len + 1, 0);

    while (pos > 0) {
        uint16 c = (uint8)(*instr);
        if (c >= 0xf0) { /* four bytes encoded, 21 bits */
            if(pos-4 >= 0)
                c = ((instr[0]&7)<<18) | ((instr[1]&63)<<12) | ((instr[2]&63)<<6) | (instr[3]&63);
            else
                c = '?';

            instr += 4;
            pos -= 4;
        } else if (c >= 0xe0) { /* three bytes encoded, 16 bits */
            if (pos-3 >= 0)
                c = ((instr[0]&63)<<12) | ((instr[1]&63)<<6) | (instr[2]&63);
            else
                c = '?';

            instr += 3;
            pos -= 3;
        } else if (c >= 0xc0) { /* two bytes encoded, 11 bits */
            if (pos-2 >= 0)
                c = ((instr[0]&63)<<6) | (instr[1]&63);
            else
                c = '?';

            instr += 2;
            pos -= 2;
        } else {
            instr++;
            pos--;
        }

        newbuf[len++] = DECODE_85591(c);
    }

    if (len < instr_len)
        newbuf = str_realloc(newbuf, len + 1);

    newbuf[len] = '\0';
    return newbuf;
}

void str_utf8_free(char* s)
{
    ASSERT(s);
    FREE(s);
}

void* str_toptr(const char* s)
{
    uptr_t p;
    sscanf(s, "%x", &p);
    return (void*)p;
}

/*************************************************************************************************/
/* path/filename helper functions */
char* path_norm(char* outpath, const char* inpath)
{
#if defined(_WIN_)
    char tmp[DH_PATH_MAX];
    GetFullPathName(inpath, DH_PATH_MAX, tmp, NULL);
    return path_towin(outpath, tmp);
#else
    char* tmp = realpath(inpath, NULL);
    path_tounix(outpath, tmp);
    free(tmp);
    return outpath;
#endif
}

char* path_tounix(char* outpath, const char* inpath)
{
    char tmp[DH_PATH_MAX];
    strcpy(tmp, inpath);
    str_replace(tmp, '\\', '/');
    strcpy(outpath, tmp);
    return outpath;
}

char* path_towin(char* outpath, const char* inpath)
{
    char tmp[DH_PATH_MAX];
    strcpy(tmp, inpath);
    str_replace(tmp, '/', '\\');
    strcpy(outpath, tmp);
    return outpath;
}

char* path_getdir(char* outpath, const char* inpath)
{
    /* to prevent aliasing */
    char tmp[DH_PATH_MAX];
    strcpy(tmp, inpath);

    /* Path with '/' or '\\' at the End */
    char* r = strrchr(tmp, '/');
    if (r == NULL)     r = strrchr(tmp, '\\');
    if (r != NULL)     {    strncpy(tmp, inpath, (r - tmp)); tmp[r - tmp] = 0;    }

    strcpy(outpath, tmp);
    return outpath;
}

char* path_getfilename(char* outpath, const char* inpath)
{
    char* r;
    char tmp[DH_PATH_MAX];     
    strcpy(tmp, inpath);

    r = strrchr(tmp, '/');
    if (r == NULL)     r = strrchr(tmp, '\\');
    if (r != NULL)     strcpy(tmp, r + 1);
    
    /* Name only */
    r = strrchr(tmp, '.');
    if (r != NULL)     *r = 0;
    
    strcpy(outpath, tmp);
    return outpath;
}

char* path_getfileext(char* outpath, const char* inpath)
{
    char tmp[DH_PATH_MAX];     /* Prevent Aliasing */
    
    strcpy(tmp, inpath);
    char* r = strrchr(tmp, '.');
    if (r != NULL)     strcpy(tmp, r + 1);
    else               tmp[0] = 0;
    
    r = strchr(tmp, '/');

    strcpy(outpath, (r != NULL) ? (r + 1) : tmp);
    return outpath;
}

char* path_getfullfilename(char* outpath, const char* inpath)
{
    char* r;
    char tmp[DH_PATH_MAX];     /* Prevent Aliasing */
    strcpy(tmp, inpath);
    r = strrchr(tmp, '/');
    if (r == NULL)     r = strrchr(tmp, '\\');
    if (r != NULL)     strcpy(tmp, r + 1);
    else               strcpy(tmp, inpath);

    strcpy(outpath, tmp);
    return outpath;
}

char* path_goup(char* outpath, const char* inpath)
{
    char tmp[DH_PATH_MAX];
    strcpy(tmp, inpath);
    size_t s = strlen(tmp);
    
    if (tmp[s-1] == '/' || tmp[s-1] == '\\')    
        tmp[s-1] = 0;
    
    /* handle case when the path is like 'my/path/./' */
    if (s>3 && tmp[s-2] == '.' && (tmp[s-3] == '/' || tmp[s-3] == '\\'))
        tmp[s-3] = 0;

    char* up = strrchr(tmp, '/');
    if (up == NULL)
        up = strrchr(tmp, '\\');

    if (up != NULL)     
        *up = 0;

    strcpy(outpath, tmp);
    return outpath;
}

bool_t path_isfilevalid(const char* inpath)
{
    FILE* f = fopen(inpath, "rb");
    if (f != NULL)     
        fclose(f);
    return (f != NULL);
}

char* path_join(char* outpath, const char* join0, const char* join1, ...)
{
    char tmp[DH_PATH_MAX];
    char sep[] = {SEP_CHAR, 0};

    if (join0[0] != 0)   {
        strcpy(tmp, join0);
        strcat(tmp, sep);
        strcat(tmp, join1);
    }   else    {
        strcpy(tmp, join1);
    }

    va_list args;
    va_start(args, join1);
    const char* join2;
    while ((join2 = va_arg(args, const char*)) != NULL) {
        strcat(tmp, sep);
        strcat(tmp, join2);
    }
    va_end(args);

    return strcpy(outpath, tmp);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.