-
Notifications
You must be signed in to change notification settings - Fork 0
/
Order.c
149 lines (136 loc) · 4.1 KB
/
Order.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
#include "Order.h"
void Error(char* msg, char* mem1, char* mem2, char* mem3, char* mem4, char* mem5, char* mem6) {
//int i;
fprintf(stderr, "Error:%s- Exiting Program...\n", msg);
//for(i = 0; i < strlen(mem); i++)
if (mem1 != NULL) {
if (mem2 != NULL) {
if(mem3 != NULL){
if(mem4 != NULL){
if(mem5 != NULL){
if(mem6 != NULL){
free(mem6);
}
free(mem5);
}
free(mem4);
}
free(mem3);
}
free(mem2);
}
free(mem1);
}
exit(-1);
}
float getDiscount(char* line){
char *ch, *percent, discount_ptr[3];
ch = trim(line); //trim spaces
percent = strchr(line, 37); // get index of '%' 37 is its ascii
strncpy(discount_ptr, ch, strlen(ch) - strlen(percent));
return convertStrToNum(discount_ptr)/100; //this is the discount
}
char* get_csv_item(char* line){
char* pipe_ptr = strchr(line, 124), *item; //ascii of |
if((item = (char*)malloc(sizeof(char) * BUFF)) == NULL)
return NULL;
strncpy(item, pipe_ptr - (strlen(line) - strlen(pipe_ptr)), strlen(line) - strlen(pipe_ptr)); //returns the item
item[strlen(line) - strlen(pipe_ptr)] = '\0';
return item;
}
float get_csv_price(char* line, float discount){
char* pipe_ptr = strchr(line, 124), *csv_price;
float cost;
if((csv_price = (char*)malloc(sizeof(char) * BUFF)) == NULL)
return -1; //in the main will check if -1, it means allocation failed
strncpy(csv_price, pipe_ptr +2, strlen(pipe_ptr)+2);
csv_price[strlen(pipe_ptr)+2] = '\0';
cost = atoi(csv_price);
free(csv_price);
return cost * discount;
}
char *ltrim(char *str){
while (isspace(*str))
str++;
return str;
}
char *rtrim(char *str){
char *back = str + strlen(str);
while (isspace(*--back))
*(back + 1) = '\0';
return str;
}
char *trim(char *str){
return rtrim(ltrim(str));
}
char* read_file(char* path){
char *text;
int fd, length, rbytes;
if((fd = open(path, O_RDONLY, 0664)) == -1){
perror("open");
return NULL;
}
if((length = lseek(fd, 0, SEEK_END)) == -1){
perror("seek to end\n");
close(fd);
return NULL;
}
if (lseek(fd, 0, SEEK_SET) == -1) {
perror("seek to begining");
close(fd);
return NULL;
}
if((text = (char*)malloc(sizeof(char) * length)) == NULL){
close(fd);
return NULL;
}
if((rbytes = read(fd, text, length)) == -1){
perror("read failed");
//free(path);
free(text);
close(fd);
return NULL;
}
close(fd);
text[rbytes] = '\0'; //delete all binary data or line breaks that might have been read
return text; //because read also read bin data
}
/*converts string to num*/
float convertStrToNum(char* text){
int i, count=1;
float sum = 0;
for(i = strlen(text) -1; i >=0; i--){
sum += ((int)(text[i])-48) * count;
count *= 10;
}
return sum;
}
char* getQty(char *order, int delimeter){
// order is of the sort "Item Name Quantity"
char *ch, *quantity;
ch = strrchr(order, delimeter); // 32 is space's ascii
if ((quantity = (char *)malloc(sizeof(char) * strlen(ch))) == NULL)
return NULL;
strncpy(quantity, ch + 1, strlen(ch));
quantity[strlen(ch)]='\0';
return quantity;
}
/*returns item that client typed in*/
char* getItem(char* order, int delimeter){
char *ch, *item;
ch = strrchr(order, delimeter); // 32 is space's ascii
if ((item = (char *)malloc(sizeof(char) * (strlen(order)-strlen(ch)))) == NULL) //allocate the size of the item's name
return NULL;
strncpy(item, order, strlen(order) - strlen(ch));
item[strlen(order) - strlen(ch)] = '\0';
return item;
}
int write_to_file(int fd, char* str, int length){
int written = 0, wbytes;
while(written < length){
if((wbytes = write(fd,str + written, length - written)) < 0) //write one after the other
return -1;
written += wbytes;
}
return 0;
}