Repository navigation
Expand file tree
/
Copy pathmainFile.c
More file actions
161 lines (116 loc) · 3.3 KB
/
Copy pathmainFile.c
File metadata and controls
161 lines (116 loc) · 3.3 KB
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
/*
This mainFile contains library additions and the basic
program execution and verification environment
*/
//Dependencies
#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
//Constants
#define LIN 50
#define COL 50
//Global variables
char basePath[30] = "Database";
char yearsPath[25];
char categoryList[LIN][COL];
int sizeOfCategoryList;
FILE *dataBase;
FILE *yearList;
//Funtion to create directories
void ___mkdir (char *path);
//Defines '\' or '/' and mkdir on unix/windows environtments
#ifdef __unix__
const char *DIRECTORY_SEPARATOR_CHAR = "/";
void ___mkdir (char *path){
mkdir(path,0700);
}
#elif defined(_WIN32) || defined(WIN32)
const char *DIRECTORY_SEPARATOR_CHAR = "\\";
void ___mkdir (char *path){
mkdir(path);
}
#endif
//Custom libraries
#include"register.h"
#include"date.h"
#include"menu.h"
//Extern declarations
extern DateTime dateTime;
//Check data base
void checkDatabase(){
//Local variables
// char yearsPath[25];
char dataBaseFileCheck[30];
char itemResult[7];
char filePathTxt[30];
char filePathJson[30];
//Creating path to list years
strcat(basePath,DIRECTORY_SEPARATOR_CHAR);
strcpy(yearsPath,basePath);
strcat(yearsPath,"list_years.txt");
//Call datetime function
getDate();
//Creating path to data base
strcat(basePath,dateTime.Year);
strcat(basePath,DIRECTORY_SEPARATOR_CHAR);
//Creating path to check if a data base exists
strcpy(dataBaseFileCheck,basePath);
strcat(dataBaseFileCheck,"file_check.txt");
//Checking if an old data base exists
if((dataBase = fopen(dataBaseFileCheck,"rb")) == NULL){
//Creating directory to data base
___mkdir("Database");
___mkdir(basePath);
//Validating new data base
dataBase = fopen(dataBaseFileCheck,"wb");
fprintf(dataBase,"%s","file_check is an internal file, if it is modified or deleted the program may not work correctly");
//Close the file
fclose(dataBase);
//Concatenate current month
strcat(basePath,dateTime.Month);
strcpy(filePathTxt,basePath);
strcpy(filePathJson,basePath);
//Concatenate file extensions
strcat(filePathTxt,".txt");
strcat(filePathJson,".json");
//Write new files
dataBase = fopen(filePathJson,"w");
dataBase = fopen(filePathTxt,"w");
//Opening list of years in append mode
yearList = fopen(yearsPath,"a");
//Write the current year in the file
fprintf(yearList,"%s",dateTime.Year);
fprintf(yearList,"%c",'\n');
//Close the file
fclose(yearList);
fclose(dataBase);
}else{
//Concatenate current month
strcat(basePath,dateTime.Month);
}
//Opening list of years in append mode
yearList = fopen(yearsPath,"a");
//Search current year in file
while(fscanf(yearList,"%s",itemResult) != EOF){
//Add current year in file if don't exist
if(atoi(itemResult) == EOF && strcmp(itemResult,dateTime.Year) != 0){
//Write the current year in the file
fprintf(yearList,"%s",dateTime.Year);
fprintf(yearList,"%c",'\n');
}else if (atoi(itemResult) != EOF && strcmp(itemResult,dateTime.Year) == 0){
break;
}
}
//Close the files
fclose(yearList);
}
//Main function
int main(){
//Call checks data base function
checkDatabase();
//Load category list
loadConfig(categoryList);
//Call menu program function
Menu(newReg(),basePath,dataBase);
return(0);
}