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
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 time_t make_time_from_utc(struct tm *tm)
132 struct tm local, *utc;
137 local = *localtime(&t);
140 local.tm_year += local.tm_year - utc->tm_year;
141 local.tm_mon += local.tm_mon - utc->tm_mon ;
142 local.tm_mday += local.tm_mday - utc->tm_mday;
143 local.tm_hour += local.tm_hour - utc->tm_hour;
144 local.tm_min += local.tm_min - utc->tm_min ;
145 local.tm_sec += local.tm_sec - utc->tm_sec ;
147 return mktime(&local);
150 const char *date_to_str(time_t i)
152 static char str[sizeof ("YYYY-mm-dd HH:MM:SS")];
153 strftime(str, sizeof (str), "%Y-%m-%d %H:%M:%S", localtime(&i));
157 int file_exists(const char *name)
161 if ((fp = fopen(name, "r")))
169 int file_rename(const char *src, const char *dst)
172 if (file_exists(dst))
175 return rename(src, dst);
178 void file_copy(FILE *fin, FILE *fout)
183 while ((size = fread(buff, 1, sizeof (buff), fin)) > 0)
184 fwrite(buff, 1, size, fout);
187 /*---------------------------------------------------------------------------*/
189 int path_is_sep(int c)
192 return c == '/' || c == '\\';
198 int path_is_abs(const char *path)
200 if (path_is_sep(path[0]))
204 if (isalpha(path[0]) && path[1] == ':' && path_is_sep(path[2]))
211 char *path_join(const char *head, const char *tail)
213 return *head ? concat_string(head, "/", tail, NULL) : strdup(tail);
216 const char *path_last_sep(const char *path)
220 sep = strrchr(path, '/');
225 sep = strrchr(path, '\\');
231 if ((tmp = strrchr(path, '\\')) && sep < tmp)
239 const char *path_next_sep(const char *path)
244 skip = strcspn(path, "/\\");
246 skip = strcspn(path, "/");
249 return *(path + skip) ? path + skip : NULL;
252 const char *base_name_sans(const char *name, const char *suffix)
254 static char base[MAXSTR];
260 return base_name(name);
262 /* Remove the directory part. */
264 SAFECPY(base, base_name(name));
266 /* Remove the suffix. */
269 slen = strlen(suffix);
271 if (blen >= slen && strcmp(base + blen - slen, suffix) == 0)
272 base[blen - slen] = '\0';
277 const char *base_name(const char *name)
280 return (name && (sep = path_last_sep(name))) ? sep + 1 : name;
283 const char *dir_name(const char *name)
285 static char buff[MAXSTR];
291 if ((sep = (char *) path_last_sep(buff)))
304 /*---------------------------------------------------------------------------*/
306 int rand_between(int low, int high)
308 return low + rand() / (RAND_MAX / (high - low + 1) + 1);
311 /*---------------------------------------------------------------------------*/