-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.c
88 lines (75 loc) · 1.67 KB
/
utils.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "hashtable/fnv.h"
#include "utils.h"
int ignore_case = FALSE;
static const char *check_for_stdin(const char *file)
{
if (strcmp(file, "-") == 0)
return "stdin";
else
return file;
}
void *checked_malloc(size_t size)
{
void* ptr = malloc(size);
if (ptr == NULL)
{
perror("mcp: Cannot allocate memory");
abort();
}
return ptr;
}
void *checked_realloc(void *ptr, size_t size)
{
ptr = realloc(ptr, size);
if (ptr == NULL)
{
perror("mcp: Cannot reallocate memory");
abort();
}
return ptr;
}
void toplevel_fatal_error(const char *error)
{
fprintf(stderr, "mcp: Fatal error: %s\n", error);
abort();
}
void fatal_error(const char *msg, const char *file, int line)
{
fprintf(stderr, "%s:%d: Fatal error: %s\n", check_for_stdin(file), line, msg);
abort();
}
void error(const char *msg, const char *file, int line)
{
fprintf(stderr, "%s:%d: Error: %s\n", check_for_stdin(file), line, msg);
}
void warning(const char *msg, const char *file, int line)
{
fprintf(stderr, "%s:%d: Warning: %s\n", check_for_stdin(file), line, msg);
}
char *new_string()
{
return (char*) checked_malloc(MAX_NAME_LEN + 1);
}
int strequal(void *ptr1, void *ptr2)
{
if (ignore_case)
{
const char *str1 = (const char*) ptr1;
const char *str2 = (const char*) ptr2;
while (*str1 && tolower(*(str1)) == tolower(*(str2)))
{
++str1; ++str2;
}
return *str1 == '\0' && *str2 == '\0';
}
else
return strcmp((char*) ptr1, (char*) ptr2) == 0;
}
hashtable_t *new_hashtable()
{
return create_hashtable(HASH_TABLE_SIZE, hash_str, strequal);
}