2 * Copyright (C) 2007 Neverball contributors
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
34 /*---------------------------------------------------------------------------*/
36 int read_line(char **dst, fs_file fin)
38 char buffer[MAXSTR] = "";
42 char *store_new = NULL;
49 if (fs_gets(buffer, sizeof (buffer), fin) == NULL)
60 buffer_size = strlen(buffer) + 1;
62 /* Erase trailing newline. */
64 if (buffer[buffer_size - 2] == '\n')
67 buffer[buffer_size - 2] = '\0';
71 /* Allocate or reallocate space for the buffer. */
73 if ((store_new = (char *) realloc(store, store_size + buffer_size)))
75 /* Avoid passing garbage to string functions. */
81 store_size += buffer_size;
87 fprintf(stderr, "Failed to allocate memory.\n");
94 strncat(store, buffer, buffer_size);
102 char *strip_newline(char *str)
104 char *c = str + strlen(str) - 1;
106 while (c >= str && (*c == '\n' || *c =='\r'))
112 char *dupe_string(const char *src)
116 if (src && (dst = malloc(strlen(src) + 1)))
122 char *concat_string(const char *first, ...)
126 if ((full = strdup(first)))
133 while ((part = va_arg(ap, const char *)))
137 if ((new = realloc(full, strlen(full) + strlen(part) + 1)))
156 char *trunc_string(const char *src, char *dst, int len)
158 static const char ell[] = "...";
160 assert(len > sizeof (ell));
162 if (dst[len - 1] = '\0', strncpy(dst, src, len), dst[len - 1] != '\0')
163 strcpy(dst + len - sizeof (ell), ell);
168 time_t make_time_from_utc(struct tm *tm)
170 struct tm local, *utc;
175 local = *localtime(&t);
178 local.tm_year += local.tm_year - utc->tm_year;
179 local.tm_mon += local.tm_mon - utc->tm_mon ;
180 local.tm_mday += local.tm_mday - utc->tm_mday;
181 local.tm_hour += local.tm_hour - utc->tm_hour;
182 local.tm_min += local.tm_min - utc->tm_min ;
183 local.tm_sec += local.tm_sec - utc->tm_sec ;
185 return mktime(&local);
188 const char *date_to_str(time_t i)
190 static char str[sizeof ("YYYY-mm-dd HH:MM:SS")];
191 strftime(str, sizeof (str), "%Y-%m-%d %H:%M:%S", localtime(&i));
195 int file_exists(const char *name)
199 if ((fp = fopen(name, "r")))
207 int file_rename(const char *src, const char *dst)
210 if (file_exists(dst))
213 return rename(src, dst);
216 void file_copy(FILE *fin, FILE *fout)
221 while ((size = fread(buff, 1, sizeof (buff), fin)) > 0)
222 fwrite(buff, 1, size, fout);
225 /*---------------------------------------------------------------------------*/
227 int path_is_sep(int c)
230 return c == '/' || c == '\\';
236 int path_is_abs(const char *path)
238 if (path_is_sep(path[0]))
242 if (isalpha(path[0]) && path[1] == ':' && path_is_sep(path[2]))
249 static char *path_last_sep(const char *path)
253 sep = strrchr(path, '/');
258 sep = strrchr(path, '\\');
264 if ((tmp = strrchr(sep, '\\')))
272 char *base_name(const char *name, const char *suffix)
274 static char buf[MAXSTR];
280 /* Remove the directory part. */
282 base = path_last_sep(name);
284 strncpy(buf, base ? base + 1 : name, sizeof (buf));
286 /* Remove the suffix. */
290 int l = strlen(buf) - strlen(suffix);
292 if (l >= 0 && strcmp(buf + l, suffix) == 0)
299 const char *dir_name(const char *name)
301 static char buff[MAXSTR];
305 strncpy(buff, name, sizeof (buff) - 1);
307 if ((sep = path_last_sep(buff)))
321 * Given a path to a file REF and another path REL relative to REF,
322 * construct and return a new path that can be used to refer to REL
325 char *path_resolve(const char *ref, const char *rel)
327 static char new[MAXSTR * 2];
329 if (path_is_abs(rel))
331 strncpy(new, rel, sizeof (new) - 1);
335 strncpy(new, dir_name(ref), sizeof (new) - 1);
336 strncat(new, "/", sizeof (new) - strlen(new) - 1);
337 strncat(new, rel, sizeof (new) - strlen(new) - 1);
342 /*---------------------------------------------------------------------------*/
344 int rand_between(int low, int high)
346 return low + rand() / (RAND_MAX / (high - low + 1) + 1);
349 /*---------------------------------------------------------------------------*/