-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-DB.c
134 lines (134 loc) · 2.33 KB
/
make-DB.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
#include "stdio.h"
#include "conio.h"
#include "io.h"
#include "math.h"
#include <sys/stat.h>
typedef struct
{
char manufacturer[25];
char model[25];
unsigned int year;
double displacement;
long quantity;
}
RECORD;
typedef struct
{
char sig[8];
unsigned long count;
}
HEADER;
#define DATABASE_SIG "DATABASE"
main(argc, margv)
int argc;
char *agrv[];
{
HEADER header;
RECORD record;
FILE *in;
char b[129], *p;
int out;
puts("database creator");
if(argc <= 2)
error("arguments: path to a source file\n");
if((in=fopen(argv[1],"ra"))==NULL)
error("unable to open source file");
if((out=open(argv[2],O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_TWRITE))==NULL)
error("unable to create destination file");
memset((char *)&header,0,sizeof(HEADER));
if(!writeheader(out,&header))
error("unable to write header");
do
{
if((p=fgets(b,128,in))!=NULL)
{
parserecord(&record,b);
if(!writerecord(out,&record,header,count))
error("unable to write record");
++header.count;
}
}
while(p!=NULL);
if(!writeheader(out,&header))
error("unable to write header");
close(out);
fclose(in);
}
parserecord(r,b)
RECORD *r;
char *b;
{
int i;
char s[129];
memset((char *)r,0,sizeof(RECORD));
/*get the manufacturer*/
for(i=0;*b!=0&&*b!=','&&*b!='\n';++b,++i)
{
if(i<24)
r->manufacturer[i]=*b;
}
r->manufacturer[i]=0;
while(*b==',')++b;
/*get the model*/
for(i=0;*b!=0&&*b!=','&&*b!='\n';++b,++i)
{
if(i<24)
r->model[i]=*b;
}
r->model[i]=0;
while(*b==',')++b;
/*get the year*/
for(i=0;*b!=0&&*b!=','&&*b!='\n';++b,++i)
{
if(i<24)
s[i]=*b;
}
s[i]=0;
while(*b==',')++b;
r->year=atoi(s);
/*get the displacement*/
for(i=0;*b!=0&&*b!=','&&*b!='\n';++b,++i)
{
if(i<24)
s[i]=*b;
}
s[i]=0;
while(*b==',')++b;
r->displacement=atof(s);
/*get the quantity*/
for(i=0;*b!=0&&*b!=','&&*b!='\n';++b,++i)
{
if(i<24)
s[i]=*b;
}
s[i]=0;
while(*b==',')++b;
r->quantity=atol(s);
}
writerecord(fh,r,n)
int fh;
RECORD *r;
unsigned long n;
{
lseek(fh,(long)sizeof(HEADER)+n*(long)sizeof(RECORD),SEEK_SET);
if(write(fh,(char *)r,sizeof(RECORD))==sizeof(RECORD))
return(1);
else
return(0);
}
writeheader(fh,h)
int fh;
HEADER *h;
{
memcpy(h->sig,DATABASE_SIG,8);
lseek(fh,0L,SEEK_SET);
if(write(fh,(char *)h,sizeof(HEADER))==sizeof(HEADER))
return(1);
else return(0);
}
error(s)
char *s;
{
put(s);
exit(1);
}