Showing the changes to src/helpdata/code (1 of 300 files). Show all
Unified · Split
| @@ -0,0 +1,180 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | +#include <stdint.h> | |
| 3 | +#include <stdlib.h> | |
| 4 | +#include <string.h> | |
| 5 | + | |
| 6 | +#define TMP_FILENAME "TMP.HLP" | |
| 7 | +#define DATA_LINE_SIZE 12 | |
| 8 | + | |
| 9 | +static void delTmpPackFile(void) | |
| 10 | +{ | |
| 11 | + char command[256]; | |
| 12 | + | |
| 13 | +#ifdef _WIN32 | |
| 14 | + sprintf(command, "del %s 2>NUL", TMP_FILENAME); | |
| 15 | +#else | |
| 16 | + sprintf(command, "rm %s &> /dev/null", TMP_FILENAME); | |
| 17 | +#endif | |
| 18 | + | |
| 19 | + system(command); | |
| 20 | +} | |
| 21 | + | |
| 22 | +int main(int argc, char *argv[]) | |
| 23 | +{ | |
| 24 | + char *helpData, *textPtr2, *textPtr; | |
| 25 | + uint8_t textLenByte; | |
| 26 | + uint32_t textLen, fileSize, currLine, totalCharLen; | |
| 27 | + FILE *fIn, *fOut; | |
| 28 | + | |
| 29 | + printf("ft2hlp_to_h - by 8bitbubsy\n"); | |
| 30 | + | |
| 31 | +#ifndef _DEBUG | |
| 32 | + if (argc != 2) | |
| 33 | + { | |
| 34 | + printf("Usage: ft2hlp_to_h <helpfile.hlp>\n"); | |
| 35 | + return -1; | |
| 36 | + } | |
| 37 | + | |
| 38 | + fIn = fopen(argv[1], "rb"); | |
| 39 | +#else | |
| 40 | + fIn = fopen("FT2.HLP", "rb"); | |
| 41 | +#endif | |
| 42 | + | |
| 43 | + if (fIn == NULL) | |
| 44 | + { | |
| 45 | + printf("Error: Could not open input file for reading!\n"); | |
| 46 | + return 1; | |
| 47 | + } | |
| 48 | + | |
| 49 | + fOut = fopen(TMP_FILENAME, "wb"); | |
| 50 | + if (fOut == NULL) | |
| 51 | + { | |
| 52 | + printf("Error: Could not open temp file for writing!\n"); | |
| 53 | + fclose(fIn); | |
| 54 | + | |
| 55 | + return 1; | |
| 56 | + } | |
| 57 | + | |
| 58 | + fseek(fIn, 0, SEEK_END); | |
| 59 | + fileSize = ftell(fIn); | |
| 60 | + rewind(fIn); | |
| 61 | + | |
| 62 | + helpData = (char *)malloc(fileSize); | |
| 63 | + if (helpData == NULL) | |
| 64 | + { | |
| 65 | + printf("ERROR: Out of memory!\n"); | |
| 66 | + fclose(fIn); | |
| 67 | + fclose(fOut); | |
| 68 | + return -1; | |
| 69 | + } | |
| 70 | + | |
| 71 | + fread(helpData, 1, fileSize, fIn); | |
| 72 | + fclose(fIn); | |
| 73 | + | |
| 74 | + printf("Parsing...\n"); | |
| 75 | + | |
| 76 | + totalCharLen = 0; | |
| 77 | + currLine = 0; | |
| 78 | + | |
| 79 | + textPtr = helpData; | |
| 80 | + while (totalCharLen < fileSize) | |
| 81 | + { | |
| 82 | + if (!strncmp(textPtr, "END", 3) && totalCharLen >= fileSize-(3+2)) | |
| 83 | + { | |
| 84 | + /* we reached the final END */ | |
| 85 | + fputc(3, fOut); | |
| 86 | + fprintf(fOut, "END"); | |
| 87 | + break; | |
| 88 | + } | |
| 89 | + | |
| 90 | + textPtr2 = textPtr; | |
| 91 | + | |
| 92 | + textLen = 0; | |
| 93 | + while (*textPtr2 != '\r') | |
| 94 | + { | |
| 95 | + textLen++; | |
| 96 | + textPtr2++; | |
| 97 | + } | |
| 98 | + | |
| 99 | + if (textLen > 255) | |
| 100 | + { | |
| 101 | + printf("ERROR: Line %d has more than 255 characters!\n", currLine); | |
| 102 | + fclose(fOut); | |
| 103 | + return 1; | |
| 104 | + } | |
| 105 | + | |
| 106 | + textLenByte = textLen & 0xFF; | |
| 107 | + fwrite(&textLenByte, 1, 1, fOut); | |
| 108 | + fwrite(textPtr, 1, textLen, fOut); | |
| 109 | + | |
| 110 | + totalCharLen += textLen + 2; | |
| 111 | + textPtr += textLen + 2; | |
| 112 | + | |
| 113 | + currLine++; | |
| 114 | + } | |
| 115 | + | |
| 116 | + printf("Successfully parsed %d lines.\n", currLine - 1); | |
| 117 | + fclose(fOut); | |
| 118 | + | |
| 119 | + fIn = fopen(TMP_FILENAME, "rb"); | |
| 120 | + if (fIn == NULL) | |
| 121 | + { | |
| 122 | + printf("Error: Could not open temp file for reading!\n"); | |
| 123 | + delTmpPackFile(); | |
| 124 | + return 1; | |
| 125 | + } | |
| 126 | + | |
| 127 | + fOut = fopen("ft2_help_data.h", "w"); | |
| 128 | + if (fOut == NULL) | |
| 129 | + { | |
| 130 | + printf("Error: Could not open ft2_help_data.h for writing!\n"); | |
| 131 | + fclose(fIn); | |
| 132 | + delTmpPackFile(); | |
| 133 | + return 1; | |
| 134 | + } | |
| 135 | + | |
| 136 | + // fetch file size | |
| 137 | + fseek(fIn, 0, SEEK_END); | |
| 138 | + fileSize = ftell(fIn); | |
| 139 | + rewind(fIn); | |
| 140 | + | |
| 141 | + printf("Converting data to header bytes..\n"); | |
| 142 | + | |
| 143 | + // put header | |
| 144 | + fprintf(fOut, "#ifndef __FT2_HELP_DATA_H\n"); | |
| 145 | + fprintf(fOut, "#define __FT2_HELP_DATA_H\n"); | |
| 146 | + fprintf(fOut, "\n"); | |
| 147 | + fprintf(fOut, "#include <stdint.h>\n"); | |
| 148 | + fprintf(fOut, "\n"); | |
| 149 | + fprintf(fOut, "#define HELP_DATA_LEN %d\n", fileSize); | |
| 150 | + fprintf(fOut, "\n"); | |
| 151 | + fprintf(fOut, "const uint8_t helpData[%d] =\n", fileSize); | |
| 152 | + fprintf(fOut, "{\n"); | |
| 153 | + | |
| 154 | + // put data | |
| 155 | + for (uint32_t i = 0; i < fileSize; i++) | |
| 156 | + { | |
| 157 | + if ((i % DATA_LINE_SIZE) == 0) fprintf(fOut, "\t"); // tab | |
| 158 | + | |
| 159 | + if (i == fileSize-1) | |
| 160 | + fprintf(fOut, "0x%02X", fgetc(fIn)); | |
| 161 | + else | |
| 162 | + fprintf(fOut, "0x%02X,", fgetc(fIn)); | |
| 163 | + | |
| 164 | + if ((i % DATA_LINE_SIZE) == DATA_LINE_SIZE-1 || i == fileSize-1) | |
| 165 | + fputc(0x0A, fOut); // linefeed | |
| 166 | + } | |
| 167 | + | |
| 168 | + // put footer | |
| 169 | + fprintf(fOut, "};\n"); | |
| 170 | + fprintf(fOut, "\n"); | |
| 171 | + fprintf(fOut, "#endif\n"); | |
| 172 | + | |
| 173 | + fclose(fOut); | |
| 174 | + fclose(fIn); | |
| 175 | + | |
| 176 | + printf("Done! %d bytes sucessfully written to ft2_help_data.h.\n", fileSize); | |
| 177 | + delTmpPackFile(); | |
| 178 | + | |
| 179 | + return 0; | |
| 180 | +} | |