10017
|
1 |
/*
|
|
2 |
* Hedgewars, a free turn based strategy game
|
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU General Public License
|
|
7 |
* as published by the Free Software Foundation; either version 2
|
|
8 |
* of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
18 |
*/
|
|
19 |
|
|
20 |
#ifndef FLIB_UTIL_H_
|
|
21 |
#define FLIB_UTIL_H_
|
|
22 |
|
|
23 |
#include <stddef.h>
|
|
24 |
#include <stdarg.h>
|
|
25 |
#include <stdbool.h>
|
|
26 |
|
|
27 |
/**
|
|
28 |
* Prints a format string to a newly allocated buffer of the required size.
|
|
29 |
* Parameters are like those for printf. Returns NULL on error.
|
|
30 |
*
|
|
31 |
* Returned buffer must be free()d
|
|
32 |
*/
|
|
33 |
char *flib_asprintf(const char *fmt, ...);
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Exactly as flib_asprintf, but accepts a va_list.
|
|
37 |
*/
|
|
38 |
char *flib_vasprintf(const char *fmt, va_list args);
|
|
39 |
|
|
40 |
/**
|
|
41 |
* Creates a new string (that must be freed) containing all parts
|
|
42 |
* joined together, with the specified delimiter between each.
|
|
43 |
*/
|
|
44 |
char *flib_join(char **parts, int partCount, const char *delimiter);
|
|
45 |
|
|
46 |
/**
|
|
47 |
* Return a duplicate of the provided string, or NULL if an error
|
|
48 |
* occurs or if str is already NULL.
|
|
49 |
*
|
|
50 |
* Returned buffer must be free()d
|
|
51 |
*/
|
|
52 |
char *flib_strdupnull(const char *str);
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Return a duplicate of the provided buffer, or NULL if an error
|
|
56 |
* occurs or if buf is already NULL or if size is 0.
|
|
57 |
*
|
|
58 |
* Returned buffer must be free()d
|
|
59 |
*/
|
|
60 |
void *flib_bufdupnull(const void *buf, size_t size);
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Simple malloc wrapper that automatically logs an error if no memory
|
|
64 |
* is available. Otherwise behaves exactly like malloc.
|
|
65 |
*/
|
|
66 |
void *flib_malloc(size_t size);
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Simple calloc wrapper that automatically logs an error if no memory
|
|
70 |
* is available. Otherwise behaves exactly like calloc.
|
|
71 |
*/
|
|
72 |
void *flib_calloc(size_t count, size_t elementsize);
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Simple realloc wrapper that automatically logs an error if no memory
|
|
76 |
* is available. Otherwise behaves exactly like realloc.
|
|
77 |
*/
|
|
78 |
void *flib_realloc(void *ptr, size_t size);
|
|
79 |
|
|
80 |
/**
|
|
81 |
* Replace all non-alphanumeric and non-ascii bytes with escape
|
|
82 |
* sequences in the form %XX. Does not modify the original string,
|
|
83 |
* but returns a newly allocated one that must be free()d. Returns
|
|
84 |
* null on failure or if null was passed as argument.
|
|
85 |
*
|
|
86 |
* This should work fine with all ASCII-based charsets including UTF-8.
|
|
87 |
*/
|
|
88 |
char *flib_urlencode(const char *str);
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Replace some bytes with escape sequences in the form %XX.
|
|
92 |
* Does not modify the original string, but returns a newly allocated
|
|
93 |
* one that must be free()d.
|
|
94 |
*
|
|
95 |
* All bytes for which the predicate function returns true are escaped.
|
|
96 |
*
|
|
97 |
* Returns null on failure or if null was passed as argument.
|
|
98 |
*/
|
|
99 |
char *flib_urlencode_pred(const char *str, bool (*needsEscaping)(char c));
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Replace escape sequences of the form %XX with their byte values.
|
|
103 |
* Does not modify the original string, but returns a newly allocated
|
|
104 |
* one that must be free()d. Returns null on failure or if null was
|
|
105 |
* passed as argument.
|
|
106 |
*/
|
|
107 |
char *flib_urldecode(const char *str);
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Figure out if the string contains / or \. Useful in routines that
|
|
111 |
* construct filenames.
|
|
112 |
*/
|
|
113 |
bool flib_contains_dir_separator(const char *str);
|
|
114 |
|
|
115 |
/**
|
|
116 |
* Returns true if str is either NULL or points to a 0-length string
|
|
117 |
*/
|
|
118 |
bool flib_strempty(const char *str);
|
|
119 |
|
|
120 |
int flib_gets(char *str, size_t strlen);
|
|
121 |
|
|
122 |
#endif
|