iBuy data structures
Functions to read and write iBuy data from files
 All Data Structures
ibuy_io.c
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "structures.h"
10 #include "ibuy_io.h"
11 
12 /******************* DO NOT MODIFY ********************/
13 
14 int readUsersFromFile(char inputFile[], struct User users[], int *nUsers) {
15 
16  FILE *ifp; // file pointer
17  char *mode = "r", line[BUFSIZ], item[BUFSIZ]; // temporal strings
18  int indexLine = 0, indexItem; // line and field indexes
19  const int nbItems = 5; // number of fields per user
20  size_t len;
21 
22  ifp = fopen(inputFile, mode);
23  if (ifp == NULL) {
24  fprintf(stderr, "Cannot open input file: %s\n", inputFile);
25  return 1;
26  }
27 
28  while ( fgets(line, sizeof line, ifp) != NULL ) {
29  // For each line...
30  char *token = line; // line is processed as a pointer to char
31  indexItem = 0; // index counter is initialized to 0
32 
33  while ( 1 ) {
34  len = strcspn(token, ";\n"); // field delimiters (; and \n) are defined
35  sprintf(item, "%.*s", (int)len, token); // field data is copied into a char[]
36  token += len; // the pointer is moved forward
37  if ( *token == '\0' )
38  break; // end of string
39  ++token; // skip ';' delimiter
40 
41  switch (indexItem) {
42  // read data is stored into the structure according to indexItem
43 
44  case 0: // login
45  strcpy(users[indexLine].login, item);
46  break;
47  case 1: // password
48  strcpy(users[indexLine].password, item);
49  break;
50  case 2: // name
51  strcpy(users[indexLine].name, item);
52  break;
53  case 3: // surname
54  strcpy(users[indexLine].surname, item);
55  break;
56  case 4: // favorites' list
57  parseIntSeq(item, users[indexLine].favoriteSellers, MAX_USERS, &users[indexLine].nFavoriteSellers);
58  }
59 
60  if ( (++indexItem == nbItems) ) {
61  // all fields have been read
62  users[indexLine].userID = indexLine+1; // set user id
63  break;
64  }
65  }
66 
67  if ( (++indexLine == MAX_USERS) )
68  break; // MAX_USERS is reached, stop
69  }
70 
71  fclose(ifp);
72 
73  *nUsers = indexLine;
74  return 0;
75 }
76 
77 int writeUsersToFile(char outputFile[], struct User users[], int nUsers) {
78  FILE *ofp;
79  char *mode = "w";
80  int i, j;
81 
82  ofp = fopen(outputFile, mode);
83  if (ofp == NULL) {
84  fprintf(stderr, "Cannot open output file: %s\n", outputFile);
85  return 1;
86  }
87 
88  for (i=0; i<nUsers; i++) {
89  fprintf (ofp, "%s;%s;%s;%s;", users[i].login, users[i].password, users[i].name, users[i].surname);
90  for(j=0; j<users[i].nFavoriteSellers; j++) {
91  if (j !=0 )
92  fprintf (ofp, ","); // print ',' before the favorite user IDs but the first one
93 
94  fprintf (ofp, "%i", users[i].favoriteSellers[j]);
95  }
96  fprintf (ofp,";\n");
97  }
98 
99  fclose(ofp);
100 
101  return 0;
102 }
103 
104 int readItemsFromFile(char inputFile[], struct Item items[], int *nItems) {
105 
106  FILE *ifp;
107  char *mode = "r", line[BUFSIZ], item[BUFSIZ];
108  int indexLine = 0, indexItem; // line and field indexes
109  const int nbItems = 7; // number of fields per item
110  size_t len;
111  char *stopstring;
112 
113  ifp = fopen(inputFile, mode);
114  if (ifp == NULL) {
115  fprintf(stderr, "Cannot open input file: %s\n",inputFile);
116  return 1;
117  }
118 
119  while ( fgets(line, sizeof line, ifp) != NULL ) {
120  // For each line...
121  char *token = line; // line is processed as a pointer to char
122  indexItem = 0; // index counter is initialized to 0
123 
124  while ( 1 ) {
125  len = strcspn(token, ";\n"); // field delimiters (; and \n) are defined
126  sprintf(item, "%.*s", (int)len, token); // field data is copied into a char[]
127  token += len; // the pointer is moved forward
128  if ( *token == '\0' )
129  break; // end of string
130  ++token; // skip ';' delimiter
131 
132  switch (indexItem) {
133  // read data is stored into the structure according to indexItem
134 
135  case 0: // description
136  strcpy(items[indexLine].description, item);
137  break;
138  case 1: // name
139  strcpy(items[indexLine].name, item);
140  break;
141  case 2: // price
142  items[indexLine].price = strtof(item, &stopstring);
143  break;
144  case 3: // seller
145  items[indexLine].sellerID = atoi(item);
146  break;
147  case 4: // like votes
148  parseIntSeq(item, items[indexLine].likeRatings, MAX_USERS, &items[indexLine].nLikeRatings);
149  break;
150  case 5: // deal votes
151  parseIntSeq(item, items[indexLine].dealRatings, MAX_USERS, &items[indexLine].nDealRatings);
152  break;
153  case 6: // inappropriate votes
154  parseIntSeq(item, items[indexLine].soldRatings, MAX_USERS, &items[indexLine].nSoldRatings);
155  }
156 
157  if ( (++indexItem == nbItems) ) {
158  items[indexLine].itemID = indexLine+1; // set item id
159  break; // all fields have been read
160  }
161 
162  }
163 
164  if ( (++indexLine == MAX_ITEMS) )
165  break; // MAX_ITEMS is reached, stop
166  }
167 
168  fclose(ifp);
169  *nItems = indexLine;
170 
171  return 0;
172 }
173 
174 
175 int writeItemsToFile(char outputFile[], struct Item items[], int nItems) {
176  FILE *ofp;
177  char *mode = "w";
178  int i,j;
179  ofp = fopen(outputFile, mode);
180 
181  if (ofp == NULL) {
182  fprintf(stderr, "Cannot open output file: %s\n", outputFile);
183  return 1;
184  }
185 
186  for(i=0; i<nItems; i++){
187  fprintf (ofp, "%s;%s;%.2f;%i;", items[i].description, items[i].name, items[i].price, items[i].sellerID);
188  for(j=0; j<items[i].nLikeRatings; j++) {
189  if (j!=0)
190  fprintf (ofp,",");
191  fprintf(ofp, "%i", items[i].likeRatings[j]);
192  }
193 
194  fprintf (ofp,";");
195  for(j=0; j<items[i].nDealRatings; j++) {
196  if (j!=0)
197  fprintf (ofp,",");
198  fprintf (ofp, "%i", items[i].dealRatings[j]);
199  }
200 
201  fprintf (ofp,";");
202  for(j=0; j<items[i].nSoldRatings; j++) {
203  if (j!=0)
204  fprintf (ofp,",");
205  fprintf (ofp, "%i", items[i].soldRatings[j]);
206  }
207  fprintf (ofp,";\n");
208  }
209 
210  fclose(ofp);
211  return 0;
212 }
213 
215 int parseIntSeq(char *token, int vector[], int lVector, int *n){
216  size_t len;
217  int index = 0;
218  char item[3];
219  *n=0;
220  while ( 1 ) {
221  len = strcspn(token, ","); // field delimiter (,) is defined
222 
223  if(len > 0) {
224  sprintf(item, "%.*s", (int)len, token); // process item
225  vector[index] = atoi(item); // the char is transformed into an integer and stored
226  token += len; // move forward token pointer
227  if ( *token == '\0' ) {
228  ++index;
229  break; // no more items
230  }
231  ++token; // skip delimiter
232 
233  if ( (++index == lVector) )
234  break; // maximum number of expected elements is reached
235  } else {
236  break; // last element was read
237  }
238  }
239 
240  *n = index;
241  return 0;
242 };
243 
244 /******************* DO NOT MODIFY ********************/
245 /******************* DO NOT MODIFY ********************/