-
Notifications
You must be signed in to change notification settings - Fork 0
/
UltraModularizer.c
157 lines (130 loc) · 4.86 KB
/
UltraModularizer.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 <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
/**
* Calculates the amount of digits a given number has.
* <br><br/>
* @param number The number to calculate the amount of digts of.
*/
int digits(int number) {
// If the number is 0, then the amount of digits is 1.
if (number == 0) {
return 1;
}
// Else calculate it using mathematical functions.
return floor(log10(abs(number))) + 1;
}
/**
* Used to create the possibly nested directories to the modularized program.
* <br></br>
* @param path The path to the modularized file.
*/
void createDir(char* path) {
char* sep = strrchr(path, '/'); // Search for the separator.
// Checks if it is not null.
if (sep != NULL) {
// If it isn't, call itself to create the previous directory.
*sep = 0;
createDir(path);
*sep = '/';
}
// Create itself.
mkdir(path);
}
/**
* Opens an instance of the modularized file after creating any possible nested directories.
* <br></br>
* @param path The path to the modularized file.
* @param mode The mode to open the file on.
*/
FILE* openFileAfterMkdir(char* path, char* mode) {
char* sep = strrchr(path, '/'); // Search for the separator.
// Checks if it exists.
if (sep) {
// If it does, attempt to create the directory.
char* path0 = strdup(path);
path0[sep - path] = 0;
createDir(path0);
free(path0);
}
return fopen(path, mode); // Open the file in the given mode.
}
/**
* Function that reads the contents of a file and passes them into a char* variable.
* <br></br>
* @param path The path of the file to analyze and get the contents from.
*/
char* getContents(char* path) {
FILE* file = fopen(path, "r");
if (file == NULL) {
printf("File does not exist. I cannot perform miracles.");
exit(1);
}
int i = 0; // Counter that begins at 0, used to determine the amount of characters.
char character; // Variable storing the current character.
// Iterates through every character in the file, incrementing the counter variable.
while ((character = fgetc(file)) != EOF) {
i++;
}
fclose(file); // Closes the already used file.
char* contents = malloc(i + 1); // Variable that will contain the file's contents.
FILE* file2 = fopen(path, "r"); // Opens the file a second time.
// Second counter to place each character in the right cell.
int j = 0;
// Iterates through each character once again, storing them instead.
while ((character = fgetc(file2)) != EOF) {
contents[j] = character;
j++;
}
contents[i] = '\0'; // Clears the last cell for accurate representation.
fclose(file2); // Closes the file.
return contents; // Returns the read contents.
}
int main(int argc, char** argv) {
// Checks if the program does not have any arguments, stopping the program if it doesn't.
if (argc != 2) {
printf("Expected 2 arguments, found %d.\nUsage: %s <file_to_modularize>");
return 1;
}
char* srcPath = argv[1]; // Path to the file.
// Replace all instances of \ with /.
for (size_t i = 0; i < strlen(srcPath); i++) {
if (srcPath[i] == '\\') {
srcPath[i] = '/';
}
}
// Get the file's contents.
char* contents = getContents(srcPath);
// Obtain the first line.
char* token = strtok(contents, "\n");
int i = 1;
// Iterate through all lines.
while (token != NULL) {
char* iAsCharStr = malloc(digits(i) + 1); // Allocates memory for the current line's index.
sprintf(iAsCharStr, "%d" ,i); // Convert the line index into a string, storing it in the variable above.
// Allocate memory for the file's path.
char* linePath = malloc(strlen(srcPath) + strlen("-M/line.txt") + strlen(iAsCharStr) + 1);
// Operate on the string to obtain the full path.
strcpy(linePath, srcPath);
strcat(linePath, "-M/line");
strcat(linePath, iAsCharStr);
strcat(linePath, ".txt");
// Create the file.
FILE* lineFile = openFileAfterMkdir(linePath, "w");
fprintf(lineFile, "%s", token); // Write the current line in.
fclose(lineFile); // Close the file.
// Free all memory allocated inside the loop.
free(iAsCharStr);
free(linePath);
// Go to the next line.
i++;
token = strtok(NULL, "\n");
}
remove(srcPath); // Delete the original file.
free(contents); // Free the memory allocated for it's contents.
// Warn the user ultra modularization has been completed.
printf("All done! Enjoy ultra modularization!!!");
return 0; // End the program successfully.
}