103 lines
1.4 KiB
C
103 lines
1.4 KiB
C
#include "utils.h"
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <ctype.h>
|
|
|
|
int write_line_to_file(FILE *f, char *line)
|
|
{
|
|
int line_len;
|
|
int written;
|
|
|
|
line_len = strlen(line);
|
|
written = fprintf(f, "%s", line);
|
|
|
|
if (written < 0 || written != line_len)
|
|
return -1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
char *strdup(char *s)
|
|
{
|
|
int size;
|
|
char *p;
|
|
|
|
size = strlen(s) + 1;
|
|
p = calloc(size, sizeof(char));
|
|
|
|
if (p)
|
|
memcpy(p, s, size);
|
|
|
|
return p;
|
|
}
|
|
|
|
void strip_spaces_in_def(char *def)
|
|
{
|
|
int i;
|
|
int x;
|
|
int in_quotes;
|
|
|
|
in_quotes = 0;
|
|
|
|
for (i = x = 0; def[i]; i++) {
|
|
if (i > 0 && def[i] == '\"' && def[i - 1] != '\\' && !in_quotes)
|
|
in_quotes = 1;
|
|
else if (i > 0 && def[i] == '\"' && def[i - 1] != '\\' && in_quotes)
|
|
in_quotes = 0;
|
|
|
|
if (!in_quotes && (!isspace(def[i]) || (i > 0 && !isspace(def[i - 1]))))
|
|
def[x++] = def[i];
|
|
else if (in_quotes)
|
|
x++;
|
|
}
|
|
|
|
def[x] = 0x00;
|
|
}
|
|
|
|
int a_strcat(char **dest, char *src)
|
|
{
|
|
char *result;
|
|
char *temp;
|
|
int needed;
|
|
|
|
needed = strlen(*dest) + strlen(src) + 1;
|
|
result = calloc(needed, sizeof(char));
|
|
if (!result)
|
|
return -1;
|
|
|
|
sprintf(result, "%s%s", *dest, src);
|
|
|
|
temp = *dest;
|
|
*dest = result;
|
|
|
|
free(temp);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int get_next_whitespace_idx(char *s)
|
|
{
|
|
int c;
|
|
|
|
c = 0;
|
|
while (!isspace(*s)) {
|
|
s++;
|
|
c++;
|
|
}
|
|
|
|
return c;
|
|
}
|
|
|
|
char *skip_word(char *s)
|
|
{
|
|
char *_s;
|
|
|
|
_s = s;
|
|
while (!isspace(*_s++));
|
|
|
|
return _s;
|
|
}
|
|
|
|
|