-
Notifications
You must be signed in to change notification settings - Fork 0
/
iobook.c
207 lines (182 loc) · 5.28 KB
/
iobook.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
/* Operacje wejscia-wyjscia na ksizkach 1996 03 04/31; 05 01 08 26
Wszystkie operacje wykonywane sa w standarcie IBM RS6000
Wersja zmodyfikowana (zmiana struktury zapisu) 1996 12 10/14
Poprawka 1997 04 29, 1998 01 06 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "iobook.h"
unsigned long NumberOfAllWaveForms;
extern int prn;
extern char *trim(char *);
#ifdef INTELSWP
#ifndef __BORLANDC__
#ifndef __USE_XOPEN
#define __USE_XOPEN
#include <unistd.h>
#undef __USE_XOPEN
#else
#include <unistd.h>
#endif
#endif
float unix_float_to_dos(char *f)
{
short isav;
union {
char fc[4];
short fsi[2];
float ff;
} gf[2];
(void)memcpy((void *)gf[0].fc,(void *)f,4);
swab((void *)gf[0].fc,(void *)gf[1].fc,4);
isav=gf[1].fsi[0];
gf[1].fsi[0]=gf[1].fsi[1]; gf[1].fsi[1]=isav;
return gf[1].ff;
}
int unix_int_to_dos(char *f)
{
short isav;
union {
char fc[4];
short fsi[2];
int ff;
} gf[2];
(void)memcpy((void *)gf[0].fc,(void *)f,4);
swab((void *)gf[0].fc,(void *)gf[1].fc,4);
isav=gf[1].fsi[0];
gf[1].fsi[0]=gf[1].fsi[1]; gf[1].fsi[1]=isav;
return gf[1].ff;
}
void dos_float_to_unix(float f,char *ret)
{
short isav;
union {
char fc[4];
short fsi[2];
float ff;
} gf[2];
gf[0].ff=f;
swab((void *)gf[0].fc,(void *)gf[1].fc,4);
isav=gf[1].fsi[0];
gf[1].fsi[0]=gf[1].fsi[1]; gf[1].fsi[1]=isav;
(void *)memcpy((void *)ret,(void *)&gf[1].ff,4);
}
void dos_int_to_unix(int f,char *ret)
{
short isav;
union {
char fc[4];
short fsi[2];
int ff;
} gf[2];
gf[0].ff=f;
swab((void *)gf[0].fc,(void *)gf[1].fc,4);
isav=gf[1].fsi[0];
gf[1].fsi[0]=gf[1].fsi[1]; gf[1].fsi[1]=isav;
(void *)memcpy((void *)ret,(void *)&gf[1].ff,4);
}
#endif
int WriteHeader(HEADER *head,FILE *plik) /* Zapis naglownka */
{
#ifdef INTELSWP
swab((char *)&head->file_offset,(char *)&head->file_offset,2);
dos_int_to_unix(head->signal_size,(char *)&head->signal_size);
swab((char *)&head->book_size,(char *)&head->book_size,2);
dos_float_to_unix(head->signal_energy,(char *)&head->signal_energy);
dos_float_to_unix(head->book_energy,(char *)&head->book_energy);
dos_float_to_unix(head->points_per_micro_V,(char *)&head->points_per_micro_V);
dos_float_to_unix(head->FREQUENCY,(char *)&head->FREQUENCY);
#endif
if(fwrite((void *)head,sizeof(HEADER),1,plik)==0U)
return -1;
return 0;
}
int WriteAtom(ATOM *atom,FILE *plik) /* Zapis atomu */
{
#ifdef INTELSWP
swab((char *)&atom->number_of_atom_in_book,(char *)&atom->number_of_atom_in_book,2);
swab((char *)&atom->frequency,(char *)&atom->frequency,2);
swab((char *)&atom->position,(char *)&atom->position,2);
dos_float_to_unix(atom->modulus,(char *)&atom->modulus);
dos_float_to_unix(atom->amplitude,(char *)&atom->amplitude);
dos_float_to_unix(atom->phase,(char *)&atom->phase);
#endif
if(fwrite((void *)atom,sizeof(ATOM),1,plik)==0U)
return -1;
return 0;
}
int ReadHeader(HEADER *head,FILE *plik) /* Odczyt naglownka */
{
if(fread((void *)head,sizeof(HEADER),1,plik)==0U)
return -1;
#ifdef INTELSWP
swab((char *)&head->file_offset,(char *)&head->file_offset,2);
head->signal_size=unix_int_to_dos((char *)&head->signal_size);
swab((char *)&head->book_size,(char *)&head->book_size,2);
head->signal_energy=unix_float_to_dos((char *)&head->signal_energy);
head->book_energy =unix_float_to_dos((char *)&head->book_energy);
head->points_per_micro_V=unix_float_to_dos((char *)&head->points_per_micro_V);
head->FREQUENCY=unix_float_to_dos((char *)&head->FREQUENCY);
#endif
return 0;
}
int ReadAtom(ATOM *atom,FILE *plik) /* Odczyt atomu */
{
if(fread((void *)atom,sizeof(ATOM),1,plik)==0U)
return -1;
#ifdef INTELSWP
swab((char *)&atom->number_of_atom_in_book,(char *)&atom->number_of_atom_in_book,2);
swab((char *)&atom->frequency,(char *)&atom->frequency,2);
swab((char *)&atom->position,(char *)&atom->position,2);
atom->modulus=unix_float_to_dos((char *)&atom->modulus);
atom->amplitude=unix_float_to_dos((char *)&atom->amplitude);
atom->phase =unix_float_to_dos((char *)&atom->phase);
#endif
return 0;
}
int SetActualBook(int ktory,FILE *plik)
{
HEADER head; /* Ustawienie nowej pozycji w ksiazce */
long poz=0L;
int i=0;
rewind(plik);
while(i<ktory)
{
if(ReadHeader(&head,plik)==-1)
return -1;
poz+=(long)head.book_size*(long)sizeof(ATOM)+(long)sizeof(HEADER);
if(fseek(plik,poz,SEEK_SET)!=0)
return -1;
i++;
}
return 0;
}
int LicznikKsiazek(char *opt) /* Zliczenie ksiazek zawartych */
{ /* w pliku */
HEADER head;
FILE *plik;
int i=0;
long poz=0L;
NumberOfAllWaveForms=0UL;
if((plik=fopen(trim(opt),"rb"))==0)
{
fprintf(stderr,"Brak pliku %s !\n",opt);
return -1;
}
for( ; ; )
{
if(ReadHeader(&head,plik)==-1)
break;
poz+=(long)head.book_size*(long)sizeof(ATOM)+(long)sizeof(HEADER);
NumberOfAllWaveForms+=(unsigned long)head.book_size;
if(fseek(plik,poz,SEEK_SET)!=0)
break;
i++;
}
if(prn==1)
fprintf(stdout,"LICZBA ZESTAWOW ATOMOW W PLIKU : %d\n"
"LICZBA WSZYSTKICH ATOMOW W PLIKU : %lu\n",
i,NumberOfAllWaveForms);
fclose(plik);
return i;
}