/*
* Mimas conversion tools
*
* Copyright (C) 2010 Benjamin Moody
*
* 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 3 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, see .
*/
#include
#include
#include
#include "utils.h"
void *xrealloc(void *p, unsigned long sz)
{
if (sz) {
if (p)
p = realloc(p, sz);
else
p = malloc(sz);
if (!p) {
fprintf(stderr, "Out of memory (need %lu bytes)\n", sz);
abort();
}
}
else {
if (p)
free(p);
p = 0;
}
return p;
}
char *xstrndup(const char *s, int n)
{
char *p;
if (!s)
return NULL;
p = xnew(char, n + 1);
memcpy(p, s, n);
p[n] = 0;
return p;
}
char *xstrdup(const char *s)
{
if (!s)
return NULL;
else
return xstrndup(s, strlen(s));
}
char *read_line(FILE *inf)
{
char *buf = NULL;
int n = 0, na = 0;
int c;
do {
c = fgetc(inf);
if (c == EOF)
break;
else if (c == '\r') {
c = fgetc(inf);
if (c != '\n') {
ungetc(c, inf);
c = '\n';
}
}
if (n + 1 >= na) {
na = na + 100;
buf = xrenew(char, buf, na);
}
if (c != '\n')
buf[n++] = c;
buf[n] = 0;
} while (c && c != '\n');
return buf;
}