00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <stdio.h>
00038 #include <string.h>
00039 #include "sem.h"
00040 #include "constants.h"
00041
00042 #ifndef WIN32
00043 #define _CRT_SECURE_NO_DEPRECATE
00044 #endif
00045
00046
00047 BOOL SEM_ReadAlmanacDataFromFile(
00048 const char* semFilePath,
00049 SEM_structAlmanac* alm,
00050 unsigned char max_to_read,
00051 unsigned char* number_read
00052 )
00053 {
00054 FILE* in;
00055 int n;
00056 unsigned char i;
00057 unsigned utmp;
00058 unsigned number_of_records;
00059 unsigned week;
00060 unsigned toa;
00061 double dtmp;
00062 char description[128];
00063
00064
00065 *number_read = 0;
00066
00067
00068 if( max_to_read < 1 )
00069 return FALSE;
00070
00071 #ifndef _CRT_SECURE_NO_DEPRECATE
00072 if( fopen_s( &in, semFilePath, "r" ) != 0 )
00073 return FALSE;
00074 if( !in )
00075 return FALSE;
00076
00077 n = fscanf_s( in, "%u", &number_of_records ); if(n != 1){return FALSE;}
00078 if( number_of_records < 1 )
00079 return FALSE;
00080
00081 n = fscanf_s( in, "%s", &description ); if(n != 1){return FALSE;}
00082 n = fscanf_s( in, "%u", &week ); if(n != 1){return FALSE;}
00083 n = fscanf_s( in, "%u", &toa ); if(n != 1){return FALSE;}
00084
00085
00086 for( i = 0; i < number_of_records && i < max_to_read; i++ )
00087 {
00088 alm[i].week = (unsigned short) week;
00089 alm[i].toa = toa;
00090 n = fscanf_s( in, "%u", &utmp ); if(n != 1){break;} alm[i].prn = (unsigned short)utmp; if(n != 1){return FALSE;}
00091 n = fscanf_s( in, "%u", &utmp ); if(n != 1){break;} alm[i].svn = (unsigned short)utmp; if(n != 1){return FALSE;}
00092 n = fscanf_s( in, "%u", &utmp ); if(n != 1){break;} alm[i].ura = (unsigned char)utmp; if(n != 1){return FALSE;}
00093
00094 n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].ecc = dtmp; if(n != 1){return FALSE;}
00095 n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].i0 = dtmp*PI; if(n != 1){return FALSE;}
00096 n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].omegadot = dtmp*PI; if(n != 1){return FALSE;}
00097 n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].sqrta = dtmp; if(n != 1){return FALSE;}
00098 n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].omega0 = dtmp*PI; if(n != 1){return FALSE;}
00099 n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].w = dtmp*PI; if(n != 1){return FALSE;}
00100 n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].m0 = dtmp*PI; if(n != 1){return FALSE;}
00101 n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].af0 = dtmp; if(n != 1){return FALSE;}
00102 n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].af1 = dtmp; if(n != 1){return FALSE;}
00103
00104 n = fscanf_s( in, "%u", &utmp ); if(n != 1){break;} alm[i].health = (unsigned char)utmp; if(n != 1){return FALSE;}
00105 n = fscanf_s( in, "%u", &utmp ); if(n != 1){break;} alm[i].config_code = (unsigned char)utmp; if(n != 1){return FALSE;}
00106
00107 *number_read = (unsigned char)(i+1);
00108 }
00109
00110 fclose(in);
00111 #else
00112 in = fopen( semFilePath, "r" );
00113 if( !in )
00114 return FALSE;
00115
00116 n = fscanf( in, "%u", &number_of_records ); if(n != 1){return FALSE;}
00117 if( number_of_records < 1 )
00118 return FALSE;
00119
00120 n = fscanf( in, "%s", &description ); if(n != 1){return FALSE;}
00121 n = fscanf( in, "%u", &week ); if(n != 1){return FALSE;}
00122 n = fscanf( in, "%u", &toa ); if(n != 1){return FALSE;}
00123
00124
00125 for( i = 0; i < number_of_records && i < max_to_read; i++ )
00126 {
00127 alm[i].week = (unsigned short) week;
00128 alm[i].toa = toa;
00129 n = fscanf( in, "%u", &utmp ); if(n != 1){break;} alm[i].prn = (unsigned short)utmp; if(n != 1){return FALSE;}
00130 n = fscanf( in, "%u", &utmp ); if(n != 1){break;} alm[i].svn = (unsigned short)utmp; if(n != 1){return FALSE;}
00131 n = fscanf( in, "%u", &utmp ); if(n != 1){break;} alm[i].ura = (unsigned char)utmp; if(n != 1){return FALSE;}
00132
00133 n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].ecc = dtmp; if(n != 1){return FALSE;}
00134 n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].i0 = dtmp*PI; if(n != 1){return FALSE;}
00135 n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].omegadot = dtmp*PI; if(n != 1){return FALSE;}
00136 n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].sqrta = dtmp; if(n != 1){return FALSE;}
00137 n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].omega0 = dtmp*PI; if(n != 1){return FALSE;}
00138 n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].w = dtmp*PI; if(n != 1){return FALSE;}
00139 n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].m0 = dtmp*PI; if(n != 1){return FALSE;}
00140 n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].af0 = dtmp; if(n != 1){return FALSE;}
00141 n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].af1 = dtmp; if(n != 1){return FALSE;}
00142
00143 n = fscanf( in, "%u", &utmp ); if(n != 1){break;} alm[i].health = (unsigned char)utmp; if(n != 1){return FALSE;}
00144 n = fscanf( in, "%u", &utmp ); if(n != 1){break;} alm[i].config_code = (unsigned char)utmp; if(n != 1){return FALSE;}
00145
00146 *number_read = (unsigned char)(i+1);
00147 }
00148
00149 fclose(in);
00150 #endif
00151
00152 return TRUE;
00153 }
00154
00155
00156 BOOL SEM_WriteAlmanacDataToFile(
00157 const char* semFilePath,
00158 SEM_structAlmanac* alm,
00159 unsigned char number_to_write
00160 )
00161 {
00162 char buffer[1024];
00163 unsigned char i;
00164
00165 FILE* out;
00166
00167 #ifndef _CRT_SECURE_NO_DEPRECATE
00168 if( fopen_s( &out, semFilePath, "w") != 0 )
00169 return FALSE;
00170 if( !out )
00171 return FALSE;
00172 #else
00173 out = fopen(semFilePath, "w");
00174 if( !out )
00175 return FALSE;
00176 #endif
00177
00178 fprintf( out, "%d %s\n", number_to_write, "CURRENT.ALM" );
00179 fprintf( out, "%d %d\n\n", alm[0].week, alm[0].toa );
00180
00181 for( i = 0; i < number_to_write; i++ )
00182 {
00183 SEM_WriteSingleAlmanacElementToBuffer( alm[i], buffer, 1024 );
00184 fprintf( out, "%s", buffer );
00185 }
00186
00187 fclose(out);
00188 return TRUE;
00189 }
00190
00191
00192 BOOL SEM_WriteSingleAlmanacElementToBuffer(
00193 SEM_structAlmanac alm,
00194 char* buffer,
00195 unsigned short bufferSize
00196 )
00197 {
00198 int scount = 0;
00199
00200 #ifndef _CRT_SECURE_NO_DEPRECATE
00201 int dcount = 0;
00202 if( bufferSize < 1024 )
00203 {
00204 sprintf_s( buffer, bufferSize, "" );
00205 return FALSE;
00206 }
00207 dcount = sprintf_s( buffer+scount, bufferSize-scount, "%d\n%d\n%d\n", alm.prn, alm.svn, alm.ura ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00208 dcount = sprintf_s( buffer+scount, bufferSize-scount, "%-23.14g %23.14g %23.14g\n", alm.ecc, alm.i0/PI, alm.omegadot/PI ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00209 dcount = sprintf_s( buffer+scount, bufferSize-scount, "%-23.14g %23.14g %23.14g\n", alm.sqrta, alm.omega0/PI, alm.w/PI ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00210 dcount = sprintf_s( buffer+scount, bufferSize-scount, "%-23.14g %23.14g %23.14g\n", alm.m0/PI, alm.af0, alm.af1 ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00211 dcount = sprintf_s( buffer+scount, bufferSize-scount, "%d\n%d\n\n", alm.health, alm.config_code ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00212 #else
00213 if( bufferSize < 1024 )
00214 {
00215 sprintf( buffer, "" );
00216 return FALSE;
00217 }
00218 scount += sprintf( buffer+scount, "%d\n%d\n%d\n", alm.prn, alm.svn, alm.ura );
00219 scount += sprintf( buffer+scount, "%-23.14g %23.14g %23.14g\n", alm.ecc, alm.i0/PI, alm.omegadot/PI );
00220 scount += sprintf( buffer+scount, "%-23.14g %23.14g %23.14g\n", alm.sqrta, alm.omega0/PI, alm.w/PI );
00221 scount += sprintf( buffer+scount, "%-23.14g %23.14g %23.14g\n", alm.m0/PI, alm.af0, alm.af1 );
00222 scount += sprintf( buffer+scount, "%d\n%d\n\n", alm.health, alm.config_code );
00223 #endif
00224
00225 return TRUE;
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269