tennis.map: Goal chair tweaks
[neverball] / share / common.c
1 /*
2  *  Copyright (C) 2007  Neverball contributors
3  *
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.
8  *
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.
13  *
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
18  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <time.h>
25
26 #include "common.h"
27
28 #define MAXSTR 256
29
30 /*---------------------------------------------------------------------------*/
31
32 int read_line(char **dst, FILE *fin)
33 {
34     char buffer[MAXSTR] = "";
35     int  buffer_size    = 0;
36
37     char *store      = NULL;
38     char *store_new  = NULL;
39     int   store_size = 0;
40
41     int seen_newline = 0;
42
43     while (!seen_newline)
44     {
45         if (fgets(buffer, sizeof (buffer), fin) == NULL)
46         {
47             if (store_size > 0)
48                 break;
49             else
50             {
51                 *dst = NULL;
52                 return 0;
53             }
54         }
55
56         buffer_size = strlen(buffer) + 1;
57
58         /* Erase trailing newline. */
59
60         if (buffer[buffer_size - 2] == '\n')
61         {
62             seen_newline = 1;
63             buffer[buffer_size - 2] = '\0';
64             buffer_size--;
65         }
66
67         /* Allocate or reallocate space for the buffer. */
68
69         if ((store_new = (char *) realloc(store, store_size + buffer_size)))
70         {
71             /* Avoid passing garbage to string functions. */
72
73             if (store == NULL)
74                 store_new[0] = '\0';
75
76             store       = store_new;
77             store_size += buffer_size;
78
79             store_new = NULL;
80         }
81         else
82         {
83             fprintf(stderr, "Failed to allocate memory.\n");
84
85             free(store);
86             *dst = NULL;
87             return 0;
88         }
89
90         strncat(store, buffer, buffer_size);
91     }
92
93     *dst = store;
94
95     return 1;
96 }
97
98 char *strip_newline(char *str)
99 {
100     char *c = str + strlen(str) - 1;
101
102     while (c >= str && (*c == '\n' || *c =='\r'))
103         *c-- = '\0';
104
105     return str;
106 }
107
108 time_t make_time_from_utc(struct tm *tm)
109 {
110     struct tm local, *utc;
111     time_t t;
112
113     t = mktime(tm);
114
115     local = *localtime(&t);
116     utc   =  gmtime(&t);
117
118     local.tm_year += local.tm_year - utc->tm_year;
119     local.tm_mon  += local.tm_mon  - utc->tm_mon ;
120     local.tm_mday += local.tm_mday - utc->tm_mday;
121     local.tm_hour += local.tm_hour - utc->tm_hour;
122     local.tm_min  += local.tm_min  - utc->tm_min ;
123     local.tm_sec  += local.tm_sec  - utc->tm_sec ;
124
125     return mktime(&local);
126 }
127
128 const char *date_to_str(time_t i)
129 {
130     static char str[sizeof ("YYYY-mm-dd HH:MM:SS")];
131     strftime(str, sizeof (str), "%Y-%m-%d %H:%M:%S", localtime(&i));
132     return str;
133 }
134
135 int file_exists(const char *name)
136 {
137     FILE *fp;
138
139     if ((fp = fopen(name, "r")))
140     {
141         fclose(fp);
142         return 1;
143     }
144     return 0;
145 }
146
147 int file_rename(const char *src, const char *dst)
148 {
149 #ifdef _WIN32
150     if (file_exists(dst))
151         remove(dst);
152 #endif
153     return rename(src, dst);
154 }
155
156 void file_copy(FILE *fin, FILE *fout)
157 {
158     char   buff[MAXSTR];
159     size_t size;
160
161     while ((size = fread(buff, 1, sizeof (buff), fin)) > 0)
162         fwrite(buff, 1, size, fout);
163 }
164
165 char *base_name(const char *name, const char *suffix)
166 {
167     static char buf[MAXSTR];
168     char *base;
169
170     if (!name)
171         return NULL;
172
173     /* Remove the directory part. */
174
175     base = strrchr(name, '/');
176
177 #ifdef _WIN32
178     if (!base)
179         base = strrchr(name, '\\');
180     else
181     {
182         char *tmp;
183
184         if ((tmp = strrchr(base, '\\')))
185             base = tmp;
186     }
187 #endif
188
189     strncpy(buf, base ? base + 1 : name, sizeof (buf));
190
191     /* Remove the suffix. */
192
193     if (suffix)
194     {
195         int l = strlen(buf) - strlen(suffix);
196
197         if (l >= 0 && strcmp(buf + l, suffix) == 0)
198             buf[l] = '\0';
199     }
200
201     return buf;
202 }
203
204 /*---------------------------------------------------------------------------*/