-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_file.c
279 lines (250 loc) · 6.67 KB
/
read_file.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
This code includes the 'my_getline' function obtained from the following site:
https://solarianprogrammer.com/2019/04/03/c-programming-read-file-lines-fgets-getline-implement-portable-getline/
It is provided without any warranty, express or implied.
You may modify and distribute it at your own risk.
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS 1
#define restrict __restrict
#endif
#ifndef EOVERFLOW
#define EOVERFLOW 132
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#if defined(_WIN32)
#include <direct.h>
#endif
#include "cobgdb.h"
// Function to get the current directory
char* getCurrentDirectory() {
char* currentDirectory = NULL;
#ifdef _WIN32
// Windows environment
currentDirectory = (char*)_getcwd(NULL, 0);
if (currentDirectory == NULL) {
perror("Error getting current directory");
}
#else
// POSIX environment (Linux, macOS, etc.)
size_t size = pathconf(".", _PC_PATH_MAX);
if ((currentDirectory = (char *)malloc((size_t)size)) != NULL) {
if (getcwd(currentDirectory, size) == NULL) {
perror("Error getting current directory");
free(currentDirectory);
currentDirectory = NULL;
}
} else {
perror("Error allocating memory for current directory");
}
#endif
return currentDirectory;
}
void fileNameWithoutExtension(char * file, char * onlyName){
int qtd=strlen(file);
if(strchr(file,'.')!=NULL){
while(file[qtd]!='.') qtd--;
onlyName[qtd--]='\0';
while(qtd>=0){
onlyName[qtd]=file[qtd];
qtd--;
}
}else{
strcpy(onlyName, file);
}
}
char *istrstr(const char *haystack, const char *needle) {
while (*haystack) {
const char *h = haystack;
const char *n = needle;
while (*h && *n && tolower((unsigned char)*h) == tolower((unsigned char)*n)) {
h++;
n++;
}
if (!*n)
return (char *)haystack;
haystack++;
}
return NULL;
}
char* toLower(char* str) {
int j = 0;
while (str[j]) {
str[j]=(char) tolower(str[j]);
j++;
}
return str;
}
char* toUpper(char* str) {
int j = 0;
while (str[j]) {
str[j]=(char) toupper(str[j]);
j++;
}
return str;
}
boolean isSpace(char c) {
switch (c) {
case ' ':
case '\n':
case '\t':
case '\f':
case '\r':
return TRUE;
break;
default:
return FALSE;
break;
}
}
char * Trim(char * s){
int ix, jx;
char * buf;
int len = strlen(s); /* possibly should use strnlen */
buf = (char *) malloc(strlen(s)+1);
for(ix=0, jx=0; ix < len; ix++)
if(!isSpace(s[ix]))
buf[jx++] = s[ix];
buf[jx] = '\0';
strncpy(s, buf, jx); /* always looks as far as the null, but who cares? */
free(buf); /* no good leak goes unpunished */
return s; /* modifies s in place *and* returns it for swank */
}
char* subString(const char* input, int offset, int len, char* dest)
{
int input_len = strlen (input);
if ((offset + len) > input_len){
if(input!=NULL) strcpy(dest, input);
return dest;
}
char * org = strdup(input);
memcpy (dest, org + offset, len);
dest[len]='\0';
free(org);
return dest;
}
void normalizePath(char * path){
char bef = ' ';
char * temp = strdup(path);
int x=0;
for(size_t i=0; i < strlen(path);i++) {
if (temp[i] == '\\') {
if(bef!='\\') path[x++] = '/';
}else{
path[x++]=temp[i];
}
bef=temp[i];
}
path[x]='\0';
free(temp);
}
char* getFileNameFromPath(char* path) {
char sep = '/';
for(size_t i = strlen(path) - 1; i; i--)
{
if (path[i] == sep)
{
return &path[i+1];
}
}
return path;
}
void getPathName(char * path, char * org) {
char sep = '/';
int final_pos=0;
for(size_t i = strlen(org) - 1; i; i--){
if (org[i] == sep)
{
final_pos=i;
break;
}
}
subString (org, 0, final_pos, path);
path[final_pos]='\0';
}
int isAbsolutPath(char * path){
if(strstr(path, "/")!=NULL) return 1;
if(strstr(path, "\\")!=NULL) return 1;
return 0;
}
int64_t my_getline(char **restrict line, size_t *restrict len, FILE *restrict fp) {
if (line == NULL || len == NULL || fp == NULL) {
errno = EINVAL;
return -1;
}
char chunk[512];
if (*line == NULL || *len < sizeof(chunk)) {
*len = sizeof(chunk);
if ((*line = malloc(*len)) == NULL) {
errno = ENOMEM;
return -1;
}
}
(*line)[0] = '\0';
while (fgets(chunk, sizeof(chunk), fp) != NULL) {
size_t len_used = strlen(*line);
size_t chunk_used = strlen(chunk);
if (*len - len_used < chunk_used) {
if (*len > SIZE_MAX / 2) {
errno = EOVERFLOW;
return -1;
} else {
*len *= 2;
}
char *new_line = realloc(*line, *len);
if (new_line == NULL) {
free(*line);
errno = ENOMEM;
return -1;
}
*line = new_line;
}
memcpy(*line + len_used, chunk, chunk_used);
len_used += chunk_used;
(*line)[len_used] = '\0';
if ((*line)[len_used - 1] == '\n') {
return len_used;
}
}
return -1;
}
int readCodFile(struct st_cobgdb *program) {
FILE *fp = fopen(program->name_file, "r");
if (fp == NULL) {
perror("Unable to open file!");
exit(1);
}
char *line = NULL;
size_t len = 0;
Lines *lines = NULL;
Lines *line_before = NULL;
int qtd = 0;
while (my_getline(&line, &len, fp) != -1) {
Lines *new_line = (Lines *)malloc(sizeof(Lines));
new_line->line_after = NULL;
new_line->line_before = NULL;
new_line->breakpoint='N';
new_line->high = NULL;
if (lines == NULL) {
lines = new_line;
} else {
new_line->line_before = line_before;
line_before->line_after = new_line;
}
line_before = new_line;
new_line->line = line;
new_line->file_line = qtd + 1;
qtd++;
line = NULL;
}
fclose(fp);
program->lines = lines;
program->qtd_lines = qtd;
return TRUE;
}