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
33 /*---------------------------------------------------------------------------*/
35 int read_line(char **dst, fs_file fin)
44 while (fs_gets(buff, sizeof (buff), fin))
46 /* Append to data read so far. */
50 new = concat_string(line, buff, NULL);
59 /* Strip newline, if any. */
67 /* We hit a newline, clean up and break. */
68 line = realloc(line, len1 + 1);
73 return (*dst = line) ? 1 : 0;
76 char *strip_newline(char *str)
78 char *c = str + strlen(str) - 1;
80 while (c >= str && (*c == '\n' || *c =='\r'))
86 char *dupe_string(const char *src)
90 if (src && (dst = malloc(strlen(src) + 1)))
96 char *concat_string(const char *first, ...)
100 if ((full = strdup(first)))
107 while ((part = va_arg(ap, const char *)))
111 if ((new = realloc(full, strlen(full) + strlen(part) + 1)))
130 char *trunc_string(const char *src, char *dst, int len)
132 static const char ell[] = "...";
134 assert(len > sizeof (ell));
136 if (dst[len - 1] = '\0', strncpy(dst, src, len), dst[len - 1] != '\0')
137 strcpy(dst + len - sizeof (ell), ell);
142 time_t make_time_from_utc(struct tm *tm)
144 struct tm local, *utc;
149 local = *localtime(&t);
152 local.tm_year += local.tm_year - utc->tm_year;
153 local.tm_mon += local.tm_mon - utc->tm_mon ;
154 local.tm_mday += local.tm_mday - utc->tm_mday;
155 local.tm_hour += local.tm_hour - utc->tm_hour;
156 local.tm_min += local.tm_min - utc->tm_min ;
157 local.tm_sec += local.tm_sec - utc->tm_sec ;
159 return mktime(&local);
162 const char *date_to_str(time_t i)
164 static char str[sizeof ("YYYY-mm-dd HH:MM:SS")];
165 strftime(str, sizeof (str), "%Y-%m-%d %H:%M:%S", localtime(&i));
169 int file_exists(const char *name)
173 if ((fp = fopen(name, "r")))
181 int file_rename(const char *src, const char *dst)
184 if (file_exists(dst))
187 return rename(src, dst);
190 void file_copy(FILE *fin, FILE *fout)
195 while ((size = fread(buff, 1, sizeof (buff), fin)) > 0)
196 fwrite(buff, 1, size, fout);
199 /*---------------------------------------------------------------------------*/
201 int path_is_sep(int c)
204 return c == '/' || c == '\\';
210 int path_is_abs(const char *path)
212 if (path_is_sep(path[0]))
216 if (isalpha(path[0]) && path[1] == ':' && path_is_sep(path[2]))
223 static char *path_last_sep(const char *path)
227 sep = strrchr(path, '/');
232 sep = strrchr(path, '\\');
238 if ((tmp = strrchr(sep, '\\')))
246 char *base_name(const char *name, const char *suffix)
248 static char buf[MAXSTR];
254 /* Remove the directory part. */
256 base = path_last_sep(name);
258 strncpy(buf, base ? base + 1 : name, sizeof (buf) - 1);
260 /* Remove the suffix. */
264 int l = strlen(buf) - strlen(suffix);
266 if (l >= 0 && strcmp(buf + l, suffix) == 0)
273 const char *dir_name(const char *name)
275 static char buff[MAXSTR];
279 strncpy(buff, name, sizeof (buff) - 1);
281 if ((sep = path_last_sep(buff)))
295 * Given a path to a file REF and another path REL relative to REF,
296 * construct and return a new path that can be used to refer to REL
299 char *path_resolve(const char *ref, const char *rel)
301 static char new[MAXSTR * 2];
303 if (path_is_abs(rel))
305 strncpy(new, rel, sizeof (new) - 1);
309 strncpy(new, dir_name(ref), sizeof (new) - 1);
310 strncat(new, "/", sizeof (new) - strlen(new) - 1);
311 strncat(new, rel, sizeof (new) - strlen(new) - 1);
316 /*---------------------------------------------------------------------------*/
318 int rand_between(int low, int high)
320 return low + rand() / (RAND_MAX / (high - low + 1) + 1);
323 /*---------------------------------------------------------------------------*/