1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <glib.h>
#include "general.h"
/* prune filename removes all of the leading path information to a filename */
char *
prune_filename (char *filename)
{
char *last_slash = filename;
while (*filename)
if (*filename++ == '/')
last_slash = filename;
return last_slash;
}
char*
search_in_path (char *search_path,
char *filename)
{
static char path[256];
char *local_path, *token;
struct stat buf;
int err;
local_path = g_strdup (search_path);
token = strtok (local_path, ":");
while (token)
{
sprintf (path, "%s", token);
if (token[strlen (token) - 1] != '/')
strcat (path, "/");
strcat (path, filename);
err = stat (path, &buf);
if (!err && S_ISREG (buf.st_mode))
{
token = path;
break;
}
token = strtok (NULL, ":");
}
g_free (local_path);
return token;
}
/*****/
/* FIXME: This is straight from the GNU libc sources. We should use
* autoconf to test for a system having strsep() and similar
* stuff... then use HAVE_STRSEP to decide whether to include or not
* our own functions.
*/
char *
xstrsep (char **pp,
char *delim)
{
char *p, *q;
if (!(p = *pp))
return NULL;
if ((q = strpbrk (p, delim))) {
*pp = q + 1;
*q = '\0';
} else
*pp = NULL;
return p;
} /* xstrsep */
char *token_str;
char *token_sym;
double token_num;
int token_int;
int
get_token (ParseInfo *info)
{
char *buffer;
char *tokenbuf;
int tokenpos = 0;
int state;
int count;
int slashed;
state = 0;
buffer = info->buffer;
tokenbuf = info->tokenbuf;
slashed = FALSE;
while (1)
{
if (info->inc_charnum && info->charnum)
info->charnum += 1;
if (info->inc_linenum && info->linenum)
{
info->linenum += 1;
info->charnum = 1;
}
info->inc_linenum = FALSE;
info->inc_charnum = FALSE;
if (info->position >= (info->buffer_size - 1))
info->position = -1;
if ((info->position == -1) || (buffer[info->position] == '\0'))
{
count = fread (buffer, sizeof (char), info->buffer_size - 1, info->fp);
if ((count == 0) && feof (info->fp))
return TOKEN_EOF;
buffer[count] = '\0';
info->position = 0;
}
info->inc_charnum = TRUE;
if ((buffer[info->position] == '\n') ||
(buffer[info->position] == '\r'))
info->inc_linenum = TRUE;
switch (state)
{
case 0:
if (buffer[info->position] == '#')
{
info->position += 1;
state = 4;
}
else if (buffer[info->position] == '(')
{
info->position += 1;
return TOKEN_LEFT_PAREN;
}
else if (buffer[info->position] == ')')
{
info->position += 1;
return TOKEN_RIGHT_PAREN;
}
else if (buffer[info->position] == '"')
{
info->position += 1;
state = 1;
slashed = FALSE;
}
else if ((buffer[info->position] == '-') || isdigit (buffer[info->position]))
{
tokenbuf[tokenpos++] = buffer[info->position];
info->position += 1;
state = 3;
}
else if ((buffer[info->position] != ' ') &&
(buffer[info->position] != '\t') &&
(buffer[info->position] != '\n') &&
(buffer[info->position] != '\r'))
{
tokenbuf[tokenpos++] = buffer[info->position];
info->position += 1;
state = 2;
}
else if (buffer[info->position] == '\'')
{
info->position += 1;
state = 2;
}
else
{
info->position += 1;
}
break;
case 1:
if ((buffer[info->position] == '\\') && (!slashed))
{
slashed = TRUE;
info->position += 1;
}
else if (slashed || (buffer[info->position] != '"'))
{
slashed = FALSE;
tokenbuf[tokenpos++] = buffer[info->position];
info->position += 1;
}
else
{
tokenbuf[tokenpos] = '\0';
token_str = tokenbuf;
token_sym = tokenbuf;
info->position += 1;
return TOKEN_STRING;
}
break;
case 2:
if ((buffer[info->position] != ' ') &&
(buffer[info->position] != '\t') &&
(buffer[info->position] != '\n') &&
(buffer[info->position] != '\r') &&
(buffer[info->position] != '"') &&
(buffer[info->position] != '(') &&
(buffer[info->position] != ')'))
{
tokenbuf[tokenpos++] = buffer[info->position];
info->position += 1;
}
else
{
tokenbuf[tokenpos] = '\0';
token_sym = tokenbuf;
return TOKEN_SYMBOL;
}
break;
case 3:
if (isdigit (buffer[info->position]) ||
(buffer[info->position] == '.'))
{
tokenbuf[tokenpos++] = buffer[info->position];
info->position += 1;
}
else if ((buffer[info->position] != ' ') &&
(buffer[info->position] != '\t') &&
(buffer[info->position] != '\n') &&
(buffer[info->position] != '\r') &&
(buffer[info->position] != '"') &&
(buffer[info->position] != '(') &&
(buffer[info->position] != ')'))
{
tokenbuf[tokenpos++] = buffer[info->position];
info->position += 1;
state = 2;
}
else
{
tokenbuf[tokenpos] = '\0';
token_sym = tokenbuf;
token_num = atof (tokenbuf);
token_int = atoi (tokenbuf);
return TOKEN_NUMBER;
}
break;
case 4:
if (buffer[info->position] == '\n')
state = 0;
info->position += 1;
break;
}
}
}
/* Parse "line" and look for a string of characters without spaces
following a '(' and copy them into "token_r". Return the number of
characters stored, or 0. */
int
find_token (char *line,
char *token_r,
int maxlen)
{
char *sp;
char *dp;
int i;
/* FIXME: This should be replaced by a more intelligent parser which
checks for '\', '"' and nested parentheses. See get_token(). */
for (sp = line; *sp; sp++)
if (*sp == '(' || *sp == '#')
break;
if (*sp == '\000' || *sp == '#')
{
*token_r = '\000';
return 0;
}
dp = token_r;
sp++;
i = 0;
while ((*sp != '\000') && (*sp != ' ') && (*sp != '\t') && i < maxlen)
{
*dp = *sp;
dp++;
sp++;
i++;
}
if (*sp == '\000' || i >= maxlen)
{
*token_r = '\000';
return 0;
}
*dp = '\000';
return i;
}
/* Generate a string containing the date and time and return a pointer
to it. If user_buf is not NULL, the string is stored in that
buffer (21 bytes). If strict is FALSE, the 'T' between the date
and the time is replaced by a space, which makes the format a bit
more readable (IMHO). */
char *
iso_8601_date_format (char *user_buf, int strict)
{
static char static_buf[21];
char *buf;
time_t clock;
struct tm *now;
if (user_buf != NULL)
buf = user_buf;
else
buf = static_buf;
clock = time (NULL);
now = gmtime (&clock);
/* date format derived from ISO 8601:1988 */
sprintf(buf, "%04d-%02d-%02d%c%02d:%02d:%02d%c",
now->tm_year + 1900, now->tm_mon + 1, now->tm_mday,
(strict ? 'T' : ' '),
now->tm_hour, now->tm_min, now->tm_sec,
(strict ? 'Z' : '\000'));
return buf;
}
|