1 |
/**************************************************************************** |
2 |
* F i l e D a t a |
3 |
* $Id: formatws2.c,v 1.3 2004/10/13 11:03:20 faber Exp $ |
4 |
* $Revision: 1.3 $ |
5 |
* $Date: 2004/10/13 11:03:20 $ |
6 |
* $RCSfile: formatws2.c,v $ |
7 |
* |
8 |
**************************************************************************** |
9 |
* S W D e v e l o p m e n t E n v i r o n m e n t |
10 |
* |
11 |
* $Author: faber $ |
12 |
* : |
13 |
**************************************************************************** |
14 |
* U p d a t i n g |
15 |
|
16 |
* $Log: formatws2.c,v $ |
17 |
* Revision 1.3 2004/10/13 11:03:20 faber |
18 |
* new format for WS2 TABLE |
19 |
* |
20 |
* Revision 1.2 2004/10/12 12:46:43 sebastiani |
21 |
* *** empty log message *** |
22 |
* |
23 |
* Revision 1.1 2004/06/22 09:34:38 faber |
24 |
* formatws2 added |
25 |
* |
26 |
* Revision 1.1 2003/10/03 16:12:26 faber |
27 |
* *** empty log message *** |
28 |
* |
29 |
* |
30 |
*****************************************************************************/ |
31 |
|
32 |
#define I386 |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
#include "../CM_Common_INFN.h" |
38 |
#include "stdio.h" |
39 |
|
40 |
//#define DEBUG |
41 |
|
42 |
unsigned int len; |
43 |
|
44 |
typedef struct { |
45 |
unsigned char mode; |
46 |
unsigned char time; |
47 |
} RECORD; |
48 |
|
49 |
RECORD table[1000]; |
50 |
unsigned short table_len=0; |
51 |
|
52 |
|
53 |
int main(int argc, char *argv[]) { |
54 |
int status=0; |
55 |
unsigned int u; |
56 |
int c=1,i; |
57 |
|
58 |
static unsigned short int b[3000]; |
59 |
int b_len; |
60 |
unsigned short int *p = b; |
61 |
unsigned short int s,u2; |
62 |
unsigned int offset=0; |
63 |
int line=0; |
64 |
char mode=0; |
65 |
char *fname; |
66 |
FILE *F; |
67 |
unsigned int written_bits=0,written_words; |
68 |
if(argc != 2) { |
69 |
fprintf(stderr,"filename missed"); |
70 |
return 1; |
71 |
} |
72 |
fname = argv[1]; |
73 |
F=fopen(fname,"r"); |
74 |
if(F == NULL) { |
75 |
fprintf(stderr,"error opening file %s",fname); |
76 |
return 1; |
77 |
} |
78 |
|
79 |
while( c != EOF ) { |
80 |
if(status != 2) |
81 |
c=fscanf(F,"%u\n",&u); |
82 |
else |
83 |
c=fscanf(F,"%c %u\n",&mode,&u); |
84 |
if(c == EOF) |
85 |
break; |
86 |
line++; |
87 |
#ifdef DEBUG |
88 |
printf("-- status: %d line: %d - u=%u %X\n",status,line,u,u); |
89 |
#endif |
90 |
|
91 |
u2=(unsigned short int)(u); |
92 |
switch(status) { |
93 |
case 0: |
94 |
// first line is the Absolute Moscow Time 4 BYTE integer: |
95 |
s=(unsigned short int)(u >> 16); |
96 |
CM_WRITE_NEXT_BITS_UINT16(p,offset,16,s); written_bits+=16; |
97 |
CM_WRITE_NEXT_BITS_UINT16(p,offset,16,u2); written_bits+=16; |
98 |
status = 1; |
99 |
break; |
100 |
case 1: |
101 |
// second line is the table number: 0..15 |
102 |
if(u > 0xF) { |
103 |
fprintf(stderr,"line: %d - invalid table number: %d; allowed 0..%d",line,u,0xF); |
104 |
return 1; |
105 |
} |
106 |
CM_WRITE_NEXT_BITS_UINT16(p,offset,4,u2); written_bits+=4; |
107 |
status =2; |
108 |
break; |
109 |
case 2: |
110 |
// record line: format <A|B> <moscow-time-offset in seconds> |
111 |
switch(mode) { |
112 |
case 'A': |
113 |
case 'a': |
114 |
table[table_len].mode = 0; |
115 |
break; |
116 |
case 'B': |
117 |
case 'b': |
118 |
table[table_len].mode = 1; |
119 |
break; |
120 |
defualt: |
121 |
fprintf(stderr,"line: %d - invalid mode number: %u\n",line,u); |
122 |
return 1; |
123 |
break; |
124 |
} |
125 |
|
126 |
if(u > 0xFF) { |
127 |
fprintf(stderr,"line: %d - invalid time out of range: %u",line,u); |
128 |
return 1; |
129 |
} |
130 |
table[table_len].time = u; |
131 |
table_len++; |
132 |
break; |
133 |
} |
134 |
} |
135 |
// complete writing: |
136 |
if(table_len & (0xFFFF << 10)) { |
137 |
fprintf(stderr,"line: %d - invalid table len out of range. must fit in 10 bit: %X",line,table_len); |
138 |
return 1; |
139 |
} |
140 |
CM_WRITE_NEXT_BITS_UINT16(p,offset,10,table_len); written_bits+=10; |
141 |
|
142 |
for(i=0;i<table_len;i++) { |
143 |
s=table[i].mode ? 1 : 0; |
144 |
CM_WRITE_NEXT_BITS_UINT16(p,offset,1,s); |
145 |
s=table[i].time; |
146 |
if(s >> 9) { |
147 |
fprintf(stderr,"line: %d - invalid table len out of range. must fit in 10 bit: %X",line,table_len); |
148 |
return 1; |
149 |
} |
150 |
CM_WRITE_NEXT_BITS_UINT16(p,offset,8,s); |
151 |
|
152 |
written_bits+=9; |
153 |
|
154 |
#ifdef DEBUG |
155 |
printf("table[%d] mode=%u time=%u %02X\n\n",i,table[i].mode,table[i].time,table[i].time); |
156 |
#endif |
157 |
|
158 |
} |
159 |
written_words =(written_bits / 16 ) + (written_bits % 16 != 0); |
160 |
for(i=0;i<written_words;i++) { |
161 |
printf("$%X ",b[i]); |
162 |
} |
163 |
|
164 |
#ifdef DEBUG |
165 |
printf("written bits=%d .written_words=%d\n",written_bits,written_words); |
166 |
#endif |
167 |
return 0; |
168 |
|
169 |
} |