/* * Program: Global Communicator II - band data converter * Programmer: Robert John Morton UK-YE572246C * Date: Wed 30 Sep 2020 From the directory in which bandconv.c resides: to compile: gcc bandconv.c -o bandconv INPUT FILE FORMAT Number of bands of this type | Type of Band | | #29 Broadcast Starting frequency of graticule [kHz] | Official starting frequency of band | | Extent of band | | | Name of band [up to 20 characters] | | | | 11600 11600 300 25-metres 1 OUTPUT FILE FORMAT IIIIBANDTYPE00000000000000000000 // band header IIIIIIIIIIIIBANDNAME000000000000 // band data line */ #include // input output to/from the console and keyboard #include // standard functions for the 'C' language FILE *FI; // open the input file for reading FILE *FO; // open the output file for writing char S[33]; // to hold the string versions of numbers, names and types int NB = 0, // number of bands of the current type type c, // for a character got from the input file GS, // Graticule Start frequency BS, // official Band Start frequency BE, // official Band Extent R = 0; // File pointer for that '.dat' output file int getInt(FILE *F) { // GET INT FROM A 4-BYTE TRAIN IN A FILE return fgetc(F) << 24 // shift first byte to left of int | fgetc(F) << 16 // shift second byte to 2nd from left | fgetc(F) << 8 // shift 3rd byte to 2nd from right | fgetc(F); // leave 4th byte where it is at far right } void getStr() { // GET A STRING FROM THE INPUT FILE int x = 0; while((c = fgetc(FI)) != '\0') S[x++] = c; S[x] = '\0'; } void putInt(int x, FILE *F) { // SAVE AN INT TO FILE AS A 4-BYTE TRAIN fputc((x >> 24) & 0xFF, F); // Store the new number of records fputc((x >> 16) & 0xFF, F); // as a 4 byte int in the first fputc((x >> 8) & 0xFF, F); // 4 bytes of the records file. fputc(x & 0xFF, F); } /* GET AND SAVE THE NAME OF THE BAND OR THE BAND-TYPE. 'i' is the number of the start character of the name in the 32-byte output file record. */ void getName(int i) { fgetc(FI); // pass over the space character while((c = fgetc(FI)) != '\n') { // until a 'new line' character is en- fputc(c,FO); // transfer characters to the output i++; // file, counting them as you go } for(int j = i; j < 32; j++) // Fill in the rest of the 32-byte record fputc('\0',FO); // with null characters. } // GET & SAVE THE NUMBER OF BANDS OF THIS TYPE AND THE NAME OF THE BAND-TYPE int getType() { S[0] = fgetc(FI); // get the first digit S[1] = fgetc(FI); // get the second digit S[2] = '\0'; // append a terminating null character NB = atoi(S); // integer from string if(NB == 0) return -1; // end of input file is marked by '#00' putInt(NB,FO); // save integer as 4-byte train getName(4); // read-in and save the band name return 0; } // GET & SAVE THE DETAILS OF THE CURRENT BAND void getBand() { S[0] = c; // first digit already got from #-test S[1] = fgetc(FI); S[2] = fgetc(FI); // These 5 digits pertain to the S[3] = fgetc(FI); // graticule start frequency S[4] = fgetc(FI); S[5] = '\0'; // append null terminator for atoi() putInt(atoi(S),FO); // save integer as a 4-byte train fgetc(FI); // pass over the space character S[0] = fgetc(FI); S[1] = fgetc(FI); // Thes 5 digits pertain to the S[2] = fgetc(FI); // official band-start frequency S[3] = fgetc(FI); S[4] = fgetc(FI); S[5] = '\0'; // append null terminator for atoi() putInt(atoi(S),FO); // save integer as a 4-byte train fgetc(FI); // pass over the space character S[0] = fgetc(FI); S[1] = fgetc(FI); // These 3 digits pertain to the S[2] = fgetc(FI); // extent of the official band. S[3] = '\0'; // append null terminator for atoi() putInt(atoi(S),FO); // save integer as a 4-byte train getName(12); // Read-in and save the band name, } // which starts at character 12 in the output file. void main() { FI = fopen("bands.txt","rb"); // open the input file for reading FO = fopen("bands.dat","wb"); // open the output file for writing while(!feof(FI)) { // while not yet reached end of input file if((c = fgetc(FI)) == '#') { // if line begins with a '#' if(getType() == -1) // get number of bands & band type name break; // '#00' end of input file encountered } else // else it is a band data line getBand(); // so get the band data } fclose(FI); // Close both input and output files because the fclose(FO); // creation of the '.dat' file is now complete. // READ BACK THE DATA FILE TO CHECK THAT IT HAS BEEN FORMED CORRECTLY FI = fopen("bands.dat","rb"); FO = fopen("bandy.txt","w"); for(int j = 0; j < 4; j++) { fseek(FI,R,SEEK_SET); NB = getInt(FI); getStr(); fprintf(FO,"NumBands=%i TypeName=%s\n",NB,S); R += 32; for(int i = 0; i < NB; i++) { fseek(FI,R,SEEK_SET); GS = getInt(FI); BS = getInt(FI); BE = getInt(FI); getStr(); fprintf(FO,"GS=%i BS=%i BE=%i %s\n",GS,BS,BE,S); R += 32; } } fclose(FI); fclose(FO); }