Add missing credit for Esperanto translation
[neverball.git] / share / common.h
blob7c96f24cadd6f35ca077f0347c49f0bd08225014
1 /*
2 * Copyright (C) 2007 Neverball authors
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 * 02111-1307 USA
20 #ifndef COMMON_H
21 #define COMMON_H
23 #include <time.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include "fs.h"
30 /* Random stuff. */
32 #ifndef MAXSTR
33 #define MAXSTR 256
34 #endif
36 #ifdef __GNUC__
37 #define NULL_TERMINATED __attribute__ ((__sentinel__))
38 #else
39 #define NULL_TERMINATED
40 #endif
42 /* Math. */
44 #define MIN(a, b) ((a) < (b) ? (a) : (b))
45 #define MAX(a, b) ((a) > (b) ? (a) : (b))
47 #define CLAMP(a, b, c) MIN(MAX(a, b), c)
49 #define SIGN(n) ((n) < 0 ? -1 : ((n) ? +1 : 0))
50 #define ROUND(f) ((int) ((f) + 0.5f * SIGN(f)))
52 #define TIME_TO_MS(t) ROUND((t) * 1000.0f)
53 #define MS_TO_TIME(m) ((m) * 0.001f)
55 int rand_between(int low, int high);
57 /* Arrays and strings. */
59 #ifndef ARRAYSIZE
60 #define ARRAYSIZE(a) (sizeof (a) / sizeof ((a)[0]))
61 #endif
63 #define MAXSTRLEN(a) (sizeof (a) - 1)
65 #define SAFECPY(dst, src) \
66 (strncpy((dst), (src), MAXSTRLEN(dst)))
67 #define SAFECAT(dst, src) \
68 (strncat((dst), (src), MAXSTRLEN(dst) - MIN(strlen(dst), MAXSTRLEN(dst))))
70 int read_line(char **, fs_file);
71 char *strip_newline(char *);
73 char *dupe_string(const char *);
74 char *concat_string(const char *first, ...) NULL_TERMINATED;
76 #ifdef strdup
77 #undef strdup
78 #endif
79 #define strdup dupe_string
81 #define str_starts_with(s, h) (strncmp((s), (h), strlen(h)) == 0)
82 #define str_ends_with(s, t) ((strlen(s) >= strlen(t)) && strcmp((s) + strlen(s) - strlen(t), (t)) == 0)
85 * Declaring vsnprintf with the C99 signature, even though we're
86 * claiming to be ANSI C. This is probably bad but is not known to not
87 * work.
89 #ifndef __APPLE__
90 extern int vsnprintf(char *, size_t, const char *, va_list);
91 #endif
93 /* Time. */
95 time_t make_time_from_utc(struct tm *);
96 const char *date_to_str(time_t);
98 /* Files. */
100 int file_exists(const char *);
101 int file_rename(const char *, const char *);
102 void file_copy(FILE *fin, FILE *fout);
104 /* Paths. */
106 int path_is_sep(int);
107 int path_is_abs(const char *);
109 char *path_join(const char *, const char *);
110 char *path_normalize(char *);
112 const char *path_last_sep(const char *);
113 const char *path_next_sep(const char *);
115 const char *base_name(const char *name);
116 const char *base_name_sans(const char *name, const char *suffix);
117 const char *dir_name(const char *name);
119 /* Environment */
121 int set_env_var(const char *, const char *);
123 #endif