/* Read a .b file. */ int readBFile (FILE *s, unsigned char *data, unsigned maxSize) { unsigned char x, y; int byteCount = 0; /* Strip the 1st two lines */ stripLine (s); stripLine (s); for (;;) { /* read the 1st ascii char */ do { x = fgetc (s); if (x == (unsigned char)EOF) return (byteCount); } while (!asciiByte(x)); /* Read the next ascii char */ y = fgetc (s); if (y == (unsigned char)EOF) return (byteCount); if (asciiByte(y)) data[byteCount++] = (toNum(x) << 4) | toNum (y); if (byteCount >= maxSize) { fprintf (stderr, "Max input array size exceeded\n"); return (-1); } } }
bool mygetline(int maxline) { char line[maxline]; int c, i; for(i = 0; i < maxline - 1 && (c = getchar()) != EOF && c != '\n'; ++i) { line[i] = c; } line[i] = '\n'; line[i+1] = '\0'; stripLine(line, i); printf("%s", line); if(c == EOF) return false; else return true; }
PoMessageEntryList *parsePoFile(const char *file, PoMessageList& messages) { FILE *inFile = fopen(file, "r"); if (!inFile) return NULL; char msgidBuf[1024], msgctxtBuf[1024], msgstrBuf[1024]; char line[1024], *currentBuf = msgstrBuf; // Get language from file name and create PoMessageEntryList int index = 0, start_index = strlen(file) - 1; while (start_index > 0 && file[start_index - 1] != '/' && file[start_index - 1] != '\\') { --start_index; } while (file[start_index + index] != '.' && file[start_index + index] != '\0') { msgidBuf[index] = file[start_index + index]; ++index; } msgidBuf[index] = '\0'; PoMessageEntryList *list = new PoMessageEntryList(msgidBuf); // Initialize the message attributes. bool fuzzy = false; bool fuzzy_next = false; // Parse the file line by line. // The msgstr is always the last line of an entry (i.e. msgid and msgctxt always // precede the corresponding msgstr). msgidBuf[0] = msgstrBuf[0] = msgctxtBuf[0] = '\0'; while (!feof(inFile) && fgets(line, 1024, inFile)) { if (line[0] == '#' && line[1] == ',') { // Handle message attributes. if (strstr(line, "fuzzy")) { fuzzy_next = true; continue; } } // Skip empty and comment line if (*line == '\n' || *line == '#') continue; if (strncmp(line, "msgid", 5) == 0) { if (currentBuf == msgstrBuf) { // add previous entry if (*msgstrBuf != '\0' && !fuzzy) { messages.insert(msgidBuf); list->addMessageEntry(msgstrBuf, msgidBuf, msgctxtBuf); } msgidBuf[0] = msgstrBuf[0] = msgctxtBuf[0] = '\0'; // Reset the attribute flags. fuzzy = fuzzy_next; fuzzy_next = false; } strcpy(msgidBuf, stripLine(line)); currentBuf = msgidBuf; } else if (strncmp(line, "msgctxt", 7) == 0) { if (currentBuf == msgstrBuf) { // add previous entry if (*msgstrBuf != '\0' && !fuzzy) { messages.insert(msgidBuf); list->addMessageEntry(msgstrBuf, msgidBuf, msgctxtBuf); } msgidBuf[0] = msgstrBuf[0] = msgctxtBuf[0] = '\0'; // Reset the attribute flags fuzzy = fuzzy_next; fuzzy_next = false; } strcpy(msgctxtBuf, stripLine(line)); currentBuf = msgctxtBuf; } else if (strncmp(line, "msgstr", 6) == 0) { strcpy(msgstrBuf, stripLine(line)); currentBuf = msgstrBuf; } else { // concatenate the string at the end of the current buffer if (currentBuf) strcat(currentBuf, stripLine(line)); } } fclose(inFile); return list; }