project_files/frontlib/util/util.c
author Wuzzy <Wuzzy2@mail.ru>
Sun, 16 Jun 2019 13:55:32 +0200
changeset 15170 a251d8757e8c
parent 10017 de822cd3df3a
permissions -rw-r--r--
ACF5: Disable inappropriate "boring" taunt at the end
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    20
#include "util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    21
#include "logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    22
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    23
#include <stddef.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    24
#include <stdarg.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    25
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    26
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    27
#include <stdio.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    28
#include <ctype.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    29
#include <limits.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    30
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    31
char *flib_asprintf(const char *fmt, ...) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    32
    va_list argp;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    33
    va_start(argp, fmt);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    34
    char *result = flib_vasprintf(fmt, argp);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    35
    va_end(argp);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    36
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    37
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    38
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    39
char *flib_vasprintf(const char *fmt, va_list args) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    40
    char *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    41
    if(!log_badargs_if(fmt==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    42
        int requiredSize = vsnprintf(NULL, 0, fmt, args)+1;                 // Figure out how much memory we need,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    43
        if(!log_e_if(requiredSize<0, "Error formatting string with template \"%s\"", fmt)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    44
            char *tmpbuf = flib_malloc(requiredSize);                       // allocate it
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    45
            if(tmpbuf && vsnprintf(tmpbuf, requiredSize, fmt, args)>=0) {   // and then do the actual formatting.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    46
                result = tmpbuf;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    47
                tmpbuf = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    48
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    49
            free(tmpbuf);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    50
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    51
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    52
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    53
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    54
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    55
char *flib_join(char **parts, int partCount, const char *delimiter) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    56
    char *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    57
    if(!log_badargs_if2(parts==NULL, delimiter==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    58
        size_t totalSize = 1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    59
        size_t delimLen = strlen(delimiter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    60
        for(int i=0; i<partCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    61
            totalSize += strlen(parts[i]) + delimLen;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    62
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    63
        result = flib_malloc(totalSize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    64
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    65
        if(result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    66
            size_t outpos = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    67
            for(int i=0; i<partCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    68
                if(i>0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    69
                    strcpy(result+outpos, delimiter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    70
                    outpos += delimLen;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    71
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    72
                strcpy(result+outpos, parts[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    73
                outpos += strlen(parts[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    74
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    75
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    76
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    77
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    78
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    79
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    80
char *flib_strdupnull(const char *str) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    81
    return str==NULL ? NULL : flib_asprintf("%s", str);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    82
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    83
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    84
void *flib_bufdupnull(const void *buf, size_t size) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    85
    void *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    86
    if(!log_badargs_if(buf==NULL && size>0)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    87
        result = flib_malloc(size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    88
        if(result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    89
            memcpy(result, buf, size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    90
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    91
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    92
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    93
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    94
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    95
void *flib_malloc(size_t size) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    96
    void *result = malloc(size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    97
    if(!result && size>0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    98
        flib_log_e("Out of memory trying to malloc %zu bytes.", size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    99
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   100
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   101
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   102
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   103
void *flib_calloc(size_t count, size_t elementsize) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   104
    void *result = calloc(count, elementsize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   105
    if(!result && count>0 && elementsize>0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   106
        flib_log_e("Out of memory trying to calloc %zu objects of %zu bytes each.", count, elementsize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   107
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   108
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   109
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   110
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   111
void *flib_realloc(void *ptr, size_t size) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   112
    void *result = realloc(ptr, size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   113
    if(!result && size>0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   114
        flib_log_e("Out of memory trying to realloc %zu bytes.", size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   115
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   116
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   117
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   118
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   119
static bool isAsciiAlnum(char c) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   120
    return (c>='0' && c<='9') || (c>='a' && c <='z') || (c>='A' && c <='Z');
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   121
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   122
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   123
char *flib_urlencode(const char *inbuf) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   124
    return flib_urlencode_pred(inbuf, isAsciiAlnum);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   125
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   126
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   127
static size_t countCharsToEscape(const char *inbuf, bool (*needsEscaping)(char c)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   128
    size_t result = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   129
    for(const char *c=inbuf; *c; c++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   130
        if(needsEscaping(*c)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   131
            result++;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   132
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   133
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   134
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   135
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   136
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   137
char *flib_urlencode_pred(const char *inbuf, bool (*needsEscaping)(char c)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   138
    char *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   139
    if(inbuf && !log_badargs_if(needsEscaping == NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   140
        size_t insize = strlen(inbuf);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   141
        if(!log_e_if(insize > SIZE_MAX/4, "String too long: %zu bytes.", insize)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   142
            size_t escapeCount = countCharsToEscape(inbuf, needsEscaping);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   143
            result = flib_malloc(insize + escapeCount*2 + 1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   144
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   145
        if(result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   146
            char *out = result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   147
            for(const char *in = inbuf; *in; in++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   148
                if(!needsEscaping(*in)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   149
                    *out = *in;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   150
                    out++;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   151
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   152
                    snprintf(out, 4, "%%%02x", (unsigned)(*(uint8_t*)in));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   153
                    out += 3;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   154
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   155
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   156
            *out = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   157
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   158
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   159
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   160
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   161
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   162
char *flib_urldecode(const char *inbuf) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   163
    if(!inbuf) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   164
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   165
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   166
    char *outbuf = flib_malloc(strlen(inbuf)+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   167
    if(!outbuf) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   168
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   169
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   170
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   171
    size_t inpos = 0, outpos = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   172
    while(inbuf[inpos]) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   173
        if(inbuf[inpos] == '%' && isxdigit(inbuf[inpos+1]) && isxdigit(inbuf[inpos+2])) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   174
            char temp[3] = {inbuf[inpos+1],inbuf[inpos+2],0};
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   175
            outbuf[outpos++] = strtol(temp, NULL, 16);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   176
            inpos += 3;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   177
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   178
            outbuf[outpos++] = inbuf[inpos++];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   179
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   180
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   181
    outbuf[outpos] = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   182
    char *shrunk = realloc(outbuf, outpos+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   183
    return shrunk ? shrunk : outbuf;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   184
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   185
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   186
bool flib_contains_dir_separator(const char *str) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   187
    if(!log_badargs_if(!str)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   188
        for(;*str;str++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   189
            if(*str=='\\' || *str=='/') {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   190
                return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   191
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   192
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   193
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   194
    return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   195
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   196
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   197
bool flib_strempty(const char *str) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   198
    return !str || !*str;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   199
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   200
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   201
int flib_gets(char *str, size_t strlen) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   202
    if(fgets(str, strlen, stdin)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   203
        for(char *s=str; *s; s++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   204
            if(*s=='\r' || *s=='\n') {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   205
                *s = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   206
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   207
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   208
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   209
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   210
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   211
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   212
}