static tagFile *initialize (const char *const filePath, tagFileInfo *const info) { tagFile *result = (tagFile*) malloc (sizeof (tagFile)); if (result != NULL) { memset (result, 0, sizeof (tagFile)); growString (&result->line); growString (&result->name); result->fields.max = 20; result->fields.list = (tagExtensionField*) malloc ( result->fields.max * sizeof (tagExtensionField)); result->fp = fopen (filePath, "r"); if (result->fp == NULL) { free (result); result = NULL; info->status.error_number = errno; } else { fseek (result->fp, 0, SEEK_END); result->size = ftell (result->fp); rewind (result->fp); readPseudoTags (result, info); info->status.opened = 1; result->initialized = 1; } } return result; }
static int readTagLineRaw(tagFile * const file) { int result = 1; int reReadLine; /* If reading the line places any character other than a null or a * newline at the last character position in the buffer (one less than * the buffer size), then we must resize the buffer and reattempt to read * the line. */ do { char *const pLastChar = file->line.buffer + file->line.size - 2; char *line; file->pos = ftell(file->fp); reReadLine = 0; *pLastChar = '\0'; line = fgets(file->line.buffer, (int) file->line.size, file->fp); if (line == NULL) { /* read error */ if (!feof(file->fp)) perror("readTagLine"); result = 0; } else if (*pLastChar != '\0' && *pLastChar != '\n' && *pLastChar != '\r') { /* buffer overflow */ growString(&file->line); fseek(file->fp, file->pos, SEEK_SET); reReadLine = 1; } else { size_t i = strlen(file->line.buffer); while (i > 0 && (file->line.buffer[i - 1] == '\n' || file->line.buffer[i - 1] == '\r')) { file->line.buffer[i - 1] = '\0'; --i; } } } while (reReadLine && result); if (result) copyName(file); return result; }
/** Copy name of tag out of tag line */ static void copyName (tagFile *const file) { size_t length; const char *end = strchr (file->line.buffer, '\t'); if (end == NULL) { end = strchr (file->line.buffer, '\n'); if (end == NULL) end = strchr (file->line.buffer, '\r'); } if (end != NULL) length = end - file->line.buffer; else length = strlen (file->line.buffer); while (length >= file->name.size) growString (&file->name); strncpy (file->name.buffer, file->line.buffer, length); file->name.buffer [length] = '\0'; }