-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.c
215 lines (190 loc) · 5.53 KB
/
Model.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
//Nome: Eduardo Airton
//GitHub: https://github.com/EduardoAirton
//Linkedin: https://www.linkedin.com/in/eduardo-airton/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//directive to work with Mac/Linux and Windows without have to change the code
#ifdef WINDOWS
#define BUFFER fflush(stdin);
#define CLEAN system("cls");
#define PAUSE getch();
#else
#define BUFFER fpurge(stdin);
#define CLEAN system("clear");
#define PAUSE getchar();
#endif
//Prototypes
void readInt(char *msg, int *value);
void readValidateInt(char *msg, char *errorMsg, int minValue, int maxValue, int *value);
void readFloat(char *msg, float *value);
void readValidateFloat(char *msg, char *errorMsg, float minValue ,float maxValue, float *value);
void readValidateText(char *msg, char *errorMsg, char *text, int size);
char readValidateChar(char *msg, char *opc);
void readValidateOption(char *msg, char *errorMsg, char *validOptions, char *option);
void removeRegister(char *text);
FILE *openFile(char *fileName, char *openingMode);
void readFile();
int checkFile(char *fileName);
int main(int argc, char const *argv[]) {
//Put your code here
}
// Objective: Read a int
// parameters: String memory address
void readInt(char *msg, int *value) {
int ret;
do{
printf("%s", msg);
BUFFER
ret = scanf("%d", value);
BUFFER
if(ret == 0){
printf("error occurred while trying to read the number.\n");
break;
}
}while(ret == 0);
}
// Objective: Read and validate a int
// parameters: String Memory address, message and error message, Min and Max value of the int
void readValidateInt(char *msg, char *errorMsg, int minValue, int maxValue, int *value){
do{
readInt(msg, value);
BUFFER
if(*value < minValue || *value > maxValue)
{
printf("%s", errorMsg);
CLEAN
}
}while(*value < minValue || *value > maxValue);
}
// Objective: Read a float number
// parameters: Message Address
void readFloat(char *msg, float *value){
int ret = 0;
do{
printf("%s", msg);
BUFFER
ret = scanf("%f", value);
BUFFER
if(ret == 0){
printf("error occurred while trying to read the number.\n");
caracter
CLEAN
}
}while(ret == 0);
}
// Objective: Read and validate a float number
// parameters: Msg address, error Msg address, min and max value of the flat
void readValidateFloat(char *msg, char *errorMsg, float minValue ,float maxValue, float *value){
do{
readFloat(msg, value);
if(*value < minValue || *value > maxValue){
printf("%s", errorMsg);
caracter
CLEAN
}
}while(*value < minValue || *value > maxValue);
}
// Objective: Read and validate a String
// parameters: Mensage and error mensage memory address, the String and the size of that String
void readValidateText(char *msg, char *errorMsg, char *text, int size) {
do{
printf("%s", msg);
BUFFER
fgets(text, size, stdin);
BUFFER
removeRegister(text);
CLEAN
if(strlen(text) ==0){
printf("%s", errorMsg);
caracter
CLEAN
}
}while(strlen(text) == 0);
}
// Objective: Validate a char
// parameters: Mensage and option memory address
// Returns: The answer
char readValidateChar(char *msg, char *opc){
int flag=0;
char resp;
do{
printf("%s",msg);
BUFFER
resp = toupper(getchar());
if (strchr(opc, resp) != NULL){
flag=1;
}
}while(flag==0);
return resp;
}
// Objective: Read a option
// parameters: Mensage and error mensage memory address, valid options
void readValidateOption(char *msg, char *errorMsg, char *validOptions, char *option) {
do {
printf("%s",msg);
BUFFER
*option = toupper(getchar());
BUFFER
if(strchr(validOptions, *option) == NULL) {
printf("%s",errorMsg);
}
} while(strchr(validOptions, *option) == NULL);
}
// Objective: Remove the \n in the String that change to the next line
// parameters: Mensage memory address
void removeRegister(char *text) {
if(text[strlen(text)-1] == '\n') {
text[strlen(text)-1] = '\0';
}
}
//---------------------------------------------FILE------------------------------------
//"r" Open for reading. If the file does not exist, fopen() returns NULL.
//"rb" Open for reading in binary mode. If the file does not exist, fopen() returns NULL.
//"w" Open for writing. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
//"wb" Open for writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
//"r+" Open for both reading and writing. If the file does not exist, fopen() returns NULL.
//"rb+" Open for both reading and writing in binary mode. If the file does not exist, fopen() returns NULL.
//-------------------------------------------------------------------------------------
//Objective: Open a file
//parameters: File name memory address, mode
//Returns: A file
FILE *openFile(char *fileName, char *openingMode) {
FILE *arq;
arq = fopen(fileName, openingMode);
if(arq != NULL) {
printf("File successfully opened!\n");
CLEAN
}
return arq;
}
// Objective: Abrir o arquivo de farmacia
// parameters: nenhum
void readFile(){
Class class;
FILE *arq;
arq = openFile("FileName.txt","Mode");
CLEAN
if(arq != NULL){
if(fread(&class,sizeof(class),1,arq) == 1){
//printf("Name: %.30s\n", class.name); Exemple on who to use it
//printf("Address: %s,%s-%s\n",class.address,class.city,class.state);
}
}
fclose(arq);
}
// Objective: Check if the file exist
// parameters: File name address
// Returns: 1 if doesn't exist and 0 if exist
int checkFile(char *fileName){
int flag;
FILE *arq;
arq = fopen(fileName, "r");
if(arq == NULL){
flag = 1;
}else{
flag = 0;
}
fclose(arq);
return flag;
}