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