-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.c
136 lines (119 loc) · 2.75 KB
/
lib.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
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
/**
* NW Editor - Super light terminal based editor
* Copyright (C) 2017 Tyler Steiman
*
* 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
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "lib.h"
/**
* Print to stderr `msg` and exit the program execution
* with EXIT_FAILURE.
*/
void
err(char *msg)
{
fprintf(stderr, "ERROR: %s\n", msg);
exit(EXIT_FAILURE);
}
/**
* Basic integer logging by displaying variable name and value
*/
void
logint(char *name, int var)
{
fprintf(stdout, "VARIABLE -- `%s`: %d -- (int)\n", name, var);
}
/**
* Basic string logging by displaying variable name and value
*/
void
logstr(char *name, char *str)
{
fprintf(stdout, "STRING -- `%s`: %s -- (str)\n", name, str);
}
/**
* Check if file exists and is readable and writable.
* Don't allow files that don't have r/w access to be opened for now.
* In the future we will probably allow users to open read only files.
*/
int
fileExists(char *path)
{
if (access(path, R_OK) != -1)
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* Wrap a string with color and print it to stderr
*/
void
colorStr(char *str, char *color)
{
fprintf(stderr, "%s%s%s", color, str, NOCOLOR);
}
void
dumpDebug(char *msg)
{
FILE *fp;
fp = fopen("bin/DEBUG-FILE", "w");
if (!fp)
{
err("Debug file could not be opened.");
}
fprintf(fp, "%s", msg);
fclose(fp);
}
/**
* Compare two strings and return TRUE if equal and FALSE
* otherwise. We are not concerned about the difference
* or anything between the two, just if they're literally
* different or not.
*/
int
stringEq (char *str1, char * str2)
{
if (strcmp(str1, str2) == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
char
*
substr(int start, int end, char *str)
{
int i = start;
int newStrIndex = 0;
char newStr[MAX_LINE_LENGTH];
for (; i < end + 1; i++)
{
newStr[newStrIndex] = str[i];
newStrIndex++;
}
char *ret = strdup(newStr);
ret[i] = '\0';
return ret;
}