-
Notifications
You must be signed in to change notification settings - Fork 3
/
filesystem.c
157 lines (141 loc) · 4.19 KB
/
filesystem.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
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "filesystem.h"
#include "stack.h"
char* pathForAnimations = OTHER_PATH;
char* pathForBlinks = BLINK_PATH;
char* pathForPalette = NULL;
/**
* Allocate memory for path complete file path and ditch path and filename together
* @param _path relativ or absolut path, where the file is laying
* @param _file filename
* @return relative or absolut path to file
*/
char* getFilePath(char* _path, char* _file) {
char* path = malloc(sizeof(char) * (MAX_PATH_LENGTH) + 1);
strcpy(path, _path);
strcat(path, _file);
return path;
}
/**
* Check if a directory exists
* @param _path Path of the directory, that's supposed to be checked
* @return If the directory of the given path exists
*/
bool checkIfDirectoryExist(char* _path) {
DIR* dir = opendir(_path);
if (dir) {
closedir(dir);
return true;
}
fprintf(stderr, "[WARNING] Directory %s does not exist. Errno is %d\n", _path, errno);
return false;
}
/**
* Check if a given file exists
* @param _file Path to the file, that should be checked
* @return If the file for the given path exists
*/
bool checkIfFileExist(char* _file) {
if (access(_file, F_OK) == 0) {
return true;
}
return false;
}
/**
* Read lines of a given file into a given array
* @param _path The file, that should be parsed into the array
* @param _count The count of lines, the file has
* @param _out The array, the lines should be read into
*/
void readFile(const char* _path, int _count, char* _out[]) {
//Check if file exists
FILE* ptr = fopen(_path, "r");
if (ptr == NULL) {
fprintf(stderr, "[ERROR] File can't be opened \n");
}
//Read line after line into give array
for (int i = 0; i < _count; i++) {
_out[i] = malloc(sizeof(unsigned int));
if (!_out[i]) {
failExit("[ERROR] readFile: Failed to allocate memory for reading file");
}
if (fscanf(ptr, "%s\n", _out[i]) != 1) {
fprintf(stderr, "[ERROR] readFile: Failed to read line %d\n", i);
}
}
//Closing the file
fclose(ptr);
}
/**
* Count the files in a given directory
* @param _path The path of the directory, it's files should be counted
* @return The number of files in that directory
*/
int countFilesInDir(char* _path) {
DIR* d = opendir(_path);
if (d) {
int counter = 0;
struct dirent* dir;
while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG) {
counter++;
}
}
closedir(d);
return counter;
}
fprintf(stderr, "[WARNING] countFilesInDir() called on nonexistent directory (%s). Errno is %d\n", _path, errno);
return -1;
}
/**
* Writes all file names of an given directory into the given _out-Array
* @param _path The path of the directory, it file names should be written into _out
* @param _out Pointer to output array. Results get writen into here.
* @return If the directory was successfully open
*/
bool getFileList(const char* _path, char* _out[]) {
DIR* d = opendir(_path);
if (d) {
int counter = 0;
struct dirent* dir;
while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG) {
_out[counter] = strdup(dir->d_name);
counter++;
}
}
closedir(d);
return true;
}
fprintf(stderr, "[WARNING] getFileList() called on nonexistent directory %s. Errno is %d\n", _path, errno);
return false;
}
/**
* Count the lines of a file
* @param _path The file whose lines should be count
* @return The number of lines
*/
int countLines(const char* _path) {
//Check if file exists
FILE* ptr = fopen(_path, "r");
if (ptr == NULL) {
fprintf(stderr, "[ERROR] File can't be opened \n");
}
//Read every character of the file and count new lines/line feeds
int newLines = 0;
int ch;
do {
ch = fgetc(ptr);
if (ch == '\n') {
newLines++;
}
} while (ch != EOF);
//Closing the file
fclose(ptr);
//Last line doesn't have a line feed, so add one
return newLines + 1;
}