1 | /*
2 | * Recdump.c -- dumps player records into an output file -- also allows
3 | * user to modify a particular record.
4 | *
5 | * Created 2001 - T. Miller and M. Becroft.
6 | */
7 |
8 | /*
9 | $ID$
10 | */
11 | #include <stdio.h>
12 | #include <stdlib.h>
13 | #include <unistd.h>
14 | #include <string.h>
15 |
16 | #include "plyrdata.h"
17 |
18 | void writetodata(void);
19 |
20 | int main(int argc, char *argv[])
21 | {
22 | int fd,i;
23 | char *s;
24 | FILE *fp;
25 | PLYRDATA_RECORD rec;
26 |
27 | if(argc == 1)
28 | {
29 | /* The user just wants a nice pretty text output. */
30 | if((fd = open_plyrdata()) != 0)
31 | {
32 | printf("Error opening plyrdata file\n");
33 | return 1;
34 | } else {
35 | if(!(fp = fopen("text.rec", "w")))
36 | {
37 | printf("Can't open text.rec file\n");
38 | return 1;
39 | }
40 | for(i = 0;; i++)
41 | {
42 | if(get_plyrdata_record(i,&rec) == 0)
43 | {
44 | printf("Got %d records\n",i);
45 | close_plyrdata();
46 | fclose(fp);
47 | return 0;
48 | } else {
49 | fprintf(fp,"Record %d\n",i);
50 | fprintf(fp,"Ontime %lu\n",get_long(rec.ontime));
51 | fprintf(fp,"Total %lu\n",get_long(rec.total));
52 | fprintf(fp,"Started %lu\n",get_long(rec.started));
53 | fprintf(fp,"Tookover %lu\n",get_long(rec.tookover));
54 | fprintf(fp,"Resigned %lu\n",get_long(rec.resigned));
55 | }
56 | }
57 | }
58 | }
59 | for(i = 1; i < argc; i++)
60 | {
61 | if(*argv[i] == '-')
62 | {
63 | for(s = argv[1] + 1; *s; s++)
64 | {
65 | switch(*s)
66 | {
67 | case 'w':
68 | /* User wants to write to
69 | the file. */
70 | writetodata();
71 | break;
72 | default:
73 | printf("Unrecognized flag '%c'\n",*s);
74 | return 1;
75 | break;
76 | }
77 | }
78 | }
79 | }
80 | /* Nothing else to do, might as well return an error. */
81 | return 1;
82 |
83 | }
84 | void writetodata(void)
85 | {
86 | PLYRDATA_RECORD rec;
87 | int a,b,c,d;
88 | if(open_plyrdata() != 0)
89 | {
90 | printf("Unable to open plyrdata file.\n");
91 | exit(0);
92 | }
93 | printf("Which record do you want to modify?\n");
94 | scanf("%d", &a);
95 |
96 | /* Print the record for the user. */
97 | if (get_plyrdata_record( a, &rec ) < 1)
98 | {
99 | printf( "Could not read record #%d\n", a);
100 | if(a > 50000)
101 | {
102 | printf("%d is very large; really create this?\n",a);
103 | printf("Type 0 for no, all else for yes.\n");
104 | scanf("%d", &d);
105 | if(d == 0) {
106 | exit(0);
107 | }
108 | }
109 | printf("Creating record.\n");
110 | memset(&rec,0,sizeof(PLYRDATA_RECORD));
111 | }
112 | else
113 | {
114 | printf( "Record #%d\n"
115 | "Ontime = %lu\n"
116 | "Total = %lu\n"
117 | "Started = %lu\n"
118 | "Resigned = %lu\n"
119 | "Tookover = %lu\n",
120 | a, get_long(rec.ontime), get_long(rec.total),
121 | get_long(rec.started), get_long(rec.resigned),
122 | get_long(rec.tookover));
123 | }
124 | printf("Choose a field to modify, 1 for ontime, 2 for total, 3 for\n");
125 | printf("started, 4 for resigned, or 5 for tookover.\n");
126 | scanf("%d", &b);
127 | printf("What should the new value for this field be?\n");
128 | scanf("%d", &c);
129 |
130 | switch(b)
131 | {
132 | case 1:
133 | rec.ontime = put_long(c);
134 | break;
135 |
136 | case 2:
137 | rec.total = put_long(c);
138 | break;
139 |
140 | case 3:
141 | rec.started = put_long(c);
142 | break;
143 |
144 | case 4:
145 | rec.resigned = put_long(c);
146 | break;
147 |
148 | case 5:
149 | rec.tookover = put_long(c);
150 | break;
151 |
152 | default:
153 | printf("Invalid selection for field to modify.\n");
154 | exit(0);
155 | }
156 | put_plyrdata_record(a, &rec);
157 | if (get_plyrdata_record( a, &rec ) < 1)
158 | {
159 | printf( "Could not read record #%d\n", a);
160 | }
161 | else
162 | {
163 | printf( "Record #%d\n"
164 | "Ontime = %lu\n"
165 | "Total = %lu\n"
166 | "Started = %lu\n"
167 | "Resigned = %lu\n"
168 | "Tookover = %lu\n",
169 | a, get_long(rec.ontime), get_long(rec.total),
170 | get_long(rec.started), get_long(rec.resigned),
171 | get_long(rec.tookover) );
172 | }
173 | close_plyrdata();
174 | }