185
191
if (!needDir && (S_ISDIR(statbuf.st_mode)))
186
192
throw _Exception(_("Not a file: ") + path);
194
if ((filesize != NULL) && S_ISREG(statbuf.st_mode))
195
*filesize = statbuf.st_size;
188
197
return statbuf.st_mtime;
200
bool is_executable(String path, int *err)
202
int ret = access(path.c_str(), R_OK | X_OK);
212
String find_in_path(String exec)
214
String PATH = String(getenv("PATH"));
215
if (!string_ok(PATH))
218
Ref<StringTokenizer> st(new StringTokenizer(PATH));
224
path = st->nextToken(_(":"));
225
next = st->nextToken(_(":"));
230
if ((next != nil) && !next.startsWith(_("/")))
232
path = path + _(":") + next;
236
String check = path + _("/") + exec;
237
if (check_path(check))
246
} while (path != nil);
191
251
bool string_ok(String str)
193
253
if ((str == nil) || (str == ""))
380
440
return buf->toString();
443
String mt_strerror(int mt_errno)
445
#ifdef DONT_USE_YET_HAVE_STRERROR_R
446
char *buffer = (char *)MALLOC(512);
448
#ifdef STRERROR_R_CHAR_P
449
err_str = strerror_r(errno, buffer, 512);
453
int ret = strerror_r(errno, buffer, 512);
455
return _("cannot get error string: error while calling XSI-compliant strerror_r");
458
String errStr(err_str);
462
return String(strerror(errno));
383
466
String read_text_file(String path)
385
468
FILE *f = fopen(path.c_str(), "r");
388
471
throw _Exception(_("read_text_file: could not open ") +
389
path + " : " + strerror(errno));
472
path + " : " + mt_strerror(errno));
391
474
Ref<StringBuffer> buf(new StringBuffer());
392
475
char *buffer = (char *)MALLOC(1024);
394
477
while((bytesRead = fread(buffer, 1, 1024, f)) > 0)
396
*buf << String(buffer, bytesRead);
479
buf->concat(buffer, bytesRead);
420
503
throw _Exception(_("write_text_file: error writing to ") +
421
path + " : " + strerror(errno));
504
path + " : " + mt_strerror(errno));
509
void copy_file(String from, String to)
511
FILE *f = fopen(from.c_str(), "r");
514
throw _Exception(_("copy_file: could not open ") +
515
from + " for read: " + mt_strerror(errno));
517
FILE *t = fopen(to.c_str(), "w");
521
throw _Exception(_("copy_file: could not open ") +
522
to + " for write: " + mt_strerror(errno));
524
char *buffer = (char *)MALLOC(1024);
525
size_t bytesRead = 0;
526
size_t bytesWritten = 0;
527
while(bytesRead == bytesWritten && ! feof(f) && ! ferror(f) && ! ferror(t)
528
&& (bytesRead = fread(buffer, 1, 1024, f)) > 0)
530
bytesWritten = fwrite(buffer, 1, bytesRead, t);
533
if (ferror(f) || ferror(t))
535
int my_errno = errno;
538
throw _Exception(_("copy_file: error while copying ") + from + " to " +
539
to + ": " + mt_strerror(my_errno));
428
547
int StringBaseComparator(void *arg1, void *arg2)
1201
Ref<Array<StringBase> > parseCommandLine(String line, String in, String out)
1203
Ref<Array<StringBase> > params = split_string(line, ' ');
1204
for (int i = 0; i < params->size(); i++)
1206
String param = params->get(i);
1207
String newParam = param.replace(_("%in"), in);
1208
newParam = newParam.replace(_("%out"), out);
1209
if (param != newParam)
1210
params->set(newParam, i);
1216
// The tempName() function is borrowed from gfileutils.c from the glibc package
1218
/* gfileutils.c - File utility functions
1220
* Copyright 2000 Red Hat, Inc.
1222
* GLib is free software; you can redistribute it and/or modify it
1223
* under the terms of the GNU Lesser General Public License as
1224
* published by the Free Software Foundation; either version 2 of the
1225
* License, or (at your option) any later version.
1227
* GLib is distributed in the hope that it will be useful,
1228
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1229
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1230
* Lesser General Public License for more details.
1232
* You should have received a copy of the GNU Lesser General Public
1233
* License along with GLib; see the file COPYING.LIB. If not,
1234
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1235
* Boston, MA 02111-1307, USA.
1238
* create_temp_file based on the mkstemp implementation from the GNU C library.
1239
* Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
1241
// tempName is based on create_temp_file, see (C) above
1242
String tempName(String leadPath, char *tmpl)
1246
static const char letters[] =
1247
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1248
static const int NLETTERS = sizeof (letters) - 1;
1251
static int counter = 0;
1252
struct stat statbuf;
1255
/* find the last occurrence of "XXXXXX" */
1256
XXXXXX = strstr (tmpl, "XXXXXX");
1258
if (!XXXXXX || strncmp (XXXXXX, "XXXXXX", 6))
1263
/* Get some more or less random data. */
1264
gettimeofday(&tv, NULL);
1265
value = (tv.tv_usec ^ tv.tv_sec) + counter++;
1267
for (count = 0; count < 100; value += 7777, ++count)
1271
/* Fill in the random bits. */
1272
XXXXXX[0] = letters[v % NLETTERS];
1274
XXXXXX[1] = letters[v % NLETTERS];
1276
XXXXXX[2] = letters[v % NLETTERS];
1278
XXXXXX[3] = letters[v % NLETTERS];
1280
XXXXXX[4] = letters[v % NLETTERS];
1282
XXXXXX[5] = letters[v % NLETTERS];
1284
String check = leadPath + tmpl;
1285
ret = stat(check.c_str(), &statbuf);
1288
if ((errno == ENOENT) ||
1296
/* We got out of the loop because we ran out of combinations to try. */
1300
bool isTheora(String ogg_filename)
1304
f = fopen(ogg_filename.c_str(), "rb");
1306
if (fread(buffer, 1, 4, f) != 4)
1309
throw _Exception(_("Error reading ") + ogg_filename);
1312
if (memcmp(buffer, "OggS", 4) != 0)
1318
if (fseek(f, 28, SEEK_SET) != 0)
1321
throw _Exception(_("Incomplete file ") + ogg_filename);
1324
if (fread(buffer, 1, 7, f) != 7)
1327
throw _Exception(_("Error reading ") + ogg_filename);
1330
if (memcmp(buffer, "\x80theora", 7) != 0)
1341
String getAVIFourCC(zmm::String avi_filename)
1343
#define FCC_OFFSET 0xbc
1345
FILE *f = fopen(avi_filename.c_str(), "rb");
1347
throw _Exception(_("could not open file ") + avi_filename + " : " +
1348
mt_strerror(errno));
1350
buffer = (char *)MALLOC(FCC_OFFSET+6);
1354
throw _Exception(_("Out of memory when allocating buffer for file ") +
1358
size_t rb = fread(buffer, 1, FCC_OFFSET+4, f);
1360
if (rb != FCC_OFFSET+4)
1363
throw _Exception(_("could not read header of ") + avi_filename +
1364
" : " + mt_strerror(errno));
1367
buffer[FCC_OFFSET+5] = '\0';
1369
if (strncmp(buffer, "RIFF", 4) != 0)
1375
if (strncmp(buffer+8, "AVI ", 4) != 0)
1381
String fourcc = String(buffer+FCC_OFFSET, 4);
1384
if (string_ok(fourcc))
1039
1391
#ifdef LOG_TOMBDEBUG
1041
1393
void profiling_thread_check(struct profiling_t *data)