project_files/frontlib/util/util.h
author Wuzzy <almikes@aol.com>
Thu, 04 May 2017 03:04:45 +0200
changeset 12395 c98e42601a93
parent 10017 de822cd3df3a
permissions -rw-r--r--
Add 15 main menu tips
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
#ifndef FLIB_UTIL_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    21
#define FLIB_UTIL_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 <stdbool.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    26
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    27
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    28
 * Prints a format string to a newly allocated buffer of the required size.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    29
 * Parameters are like those for printf. Returns NULL on error.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    30
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    31
 * Returned buffer must be free()d
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    32
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    33
char *flib_asprintf(const char *fmt, ...);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    34
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    35
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    36
 * Exactly as flib_asprintf, but accepts a va_list.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    37
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    38
char *flib_vasprintf(const char *fmt, va_list args);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    39
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    40
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    41
 * Creates a new string (that must be freed) containing all parts
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    42
 * joined together, with the specified delimiter between each.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    43
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    44
char *flib_join(char **parts, int partCount, const char *delimiter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    45
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    46
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    47
 * Return a duplicate of the provided string, or NULL if an error
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    48
 * occurs or if str is already NULL.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    49
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    50
 * Returned buffer must be free()d
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    51
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    52
char *flib_strdupnull(const char *str);
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
 * Return a duplicate of the provided buffer, or NULL if an error
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    56
 * occurs or if buf is already NULL or if size is 0.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    57
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    58
 * Returned buffer must be free()d
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    59
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    60
void *flib_bufdupnull(const void *buf, size_t size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    61
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    62
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    63
 * Simple malloc wrapper that automatically logs an error if no memory
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    64
 * is available. Otherwise behaves exactly like malloc.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    65
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    66
void *flib_malloc(size_t size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    67
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    68
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    69
 * Simple calloc wrapper that automatically logs an error if no memory
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    70
 * is available. Otherwise behaves exactly like calloc.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    71
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    72
void *flib_calloc(size_t count, size_t elementsize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    73
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    74
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    75
 * Simple realloc wrapper that automatically logs an error if no memory
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    76
 * is available. Otherwise behaves exactly like realloc.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    77
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    78
void *flib_realloc(void *ptr, size_t size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    79
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    80
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    81
 * Replace all non-alphanumeric and non-ascii bytes with escape
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    82
 * sequences in the form %XX. Does not modify the original string,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    83
 * but returns a newly allocated one that must be free()d. Returns
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    84
 * null on failure or if null was passed as argument.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    85
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    86
 * This should work fine with all ASCII-based charsets including UTF-8.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    87
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    88
char *flib_urlencode(const char *str);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    89
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    90
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    91
 * Replace some bytes with escape sequences in the form %XX.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    92
 * Does not modify the original string, but returns a newly allocated
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    93
 * one that must be free()d.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    94
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    95
 * All bytes for which the predicate function returns true are escaped.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    96
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    97
 * Returns null on failure or if null was passed as argument.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    98
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    99
char *flib_urlencode_pred(const char *str, bool (*needsEscaping)(char c));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   100
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   101
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   102
 * Replace escape sequences of the form %XX with their byte values.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   103
 * Does not modify the original string, but returns a newly allocated
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   104
 * one that must be free()d. Returns null on failure or if null was
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   105
 * passed as argument.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   106
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   107
char *flib_urldecode(const char *str);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   108
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   109
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   110
 * Figure out if the string contains / or \. Useful in routines that
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   111
 * construct filenames.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   112
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   113
bool flib_contains_dir_separator(const char *str);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   114
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   115
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   116
 * Returns true if str is either NULL or points to a 0-length string
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   117
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   118
bool flib_strempty(const char *str);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   119
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   120
int flib_gets(char *str, size_t strlen);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   121
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
   122
#endif