static void ASTILCover_Load(ASTILCover *self, FILE *fp, char *line) { const char *ts; self->startSeconds = -1; self->endSeconds = -1; ts = strrchr(line, '('); if (ts != NULL) { int i = ts - line + 1; int startSeconds = ASTILCover_ParseTimestamp(line, &i); int endSeconds = -1; if (startSeconds >= 0 && line[i] == '-') { i++; endSeconds = ASTILCover_ParseTimestamp(line, &i); } if (line[i] == ')' && line[i + 1] == '\0') { int len = ts - line; if (line[len - 1] == ' ') len--; line[len] = '\0'; self->startSeconds = startSeconds; self->endSeconds = endSeconds; } } ASTILCover_SetText(self->titleAndSource, line + 9); self->artist[0] = '\0'; self->comment[0] = '\0'; if (ASTIL_ReadLine(fp, line) >= 9 && memcmp(line, " ARTIST: ", 9) == 0) { ASTILCover_SetText(self->artist, line + 9); ASTIL_ReadComment(fp, line, self->comment); } }
static cibool ASTIL_ReadUntilSongNo(FILE *fp, char *line) { while (ASTIL_ReadLine(fp, line) > 0) { if (line[0] == '(' && line[1] == '#') return TRUE; } return FALSE; }
cibool ASTIL_Load(ASTIL *self, const char *filename, int song) { int lastSlash = ASTIL_FindPreviousSlash(filename, strlen(filename)); int rootSlash; FILE *fp; char line[ASTIL_MAX_TEXT_LENGTH + 1]; self->nCovers = 0; self->stilFilename[0] = '\0'; self->title[0] = '\0'; self->author[0] = '\0'; self->directoryComment[0] = '\0'; self->fileComment[0] = '\0'; self->songComment[0] = '\0'; if (lastSlash < 0 || lastSlash >= FILENAME_MAX - 14) /* strlen("/Docs/STIL.txt") */ return FALSE; memcpy(self->stilFilename, filename, lastSlash + 1); for (rootSlash = lastSlash; ; rootSlash = ASTIL_FindPreviousSlash(filename, rootSlash)) { if (rootSlash < 0) { self->stilFilename[0] = '\0'; return FALSE; } strcpy(self->stilFilename + rootSlash + 1, "Docs/STIL.txt"); self->stilFilename[rootSlash + 5] = self->stilFilename[rootSlash]; /* copy dir separator - slash or backslash */ fp = fopen(self->stilFilename, "rb"); if (fp != NULL) break; strcpy(self->stilFilename + rootSlash + 1, "STIL.txt"); fp = fopen(self->stilFilename, "rb"); if (fp != NULL) break; } self->isUTF8 = ASTIL_ReadUTF8BOM(fp); while (ASTIL_ReadLine(fp, line) >= 0) { int len = ASTIL_MatchFilename(line, filename + rootSlash); if (len == -1) { ASTIL_ReadComment(fp, line, self->fileComment); if (line[0] == '(' && line[1] == '#') { do { int i = 2; if (ASTIL_ParseInt(line, &i) - 1 == song && line[i] == ')' && line[i + 1] == '\0') { ASTIL_ReadComment(fp, line, self->songComment); ASTIL_ReadStilBlock(self, fp, line); break; } } while (ASTIL_ReadUntilSongNo(fp, line)); } else ASTIL_ReadStilBlock(self, fp, line); break; } if (len == lastSlash - rootSlash + 1 && line[len] == '\0') ASTIL_ReadComment(fp, line, self->directoryComment); } fclose(fp); return TRUE; }
static void ASTIL_ReadComment(FILE *fp, char *line, char *comment) { int len = ASTIL_ReadLine(fp, line); if (len >= 9 && memcmp(line, "COMMENT: ", 9) == 0) { len -= 9; memcpy(comment, line + 9, len + 1); for (;;) { int len2 = ASTIL_ReadLine(fp, line); if (len2 >= 9 && memcmp(line, " ", 9) == 0) { if (len < ASTIL_MAX_COMMENT_LENGTH - ASTIL_MAX_TEXT_LENGTH) { comment[len++] = '\n'; len2 -= 9; memcpy(comment + len, line + 9, len2 + 1); len += len2; } } else break; } } }
static void ASTIL_ReadStilBlock(ASTIL *self, FILE *fp, char *line) { for (;;) { if (memcmp(line, " NAME: ", 9) == 0) strcpy(self->title, line + 9); else if (memcmp(line, " AUTHOR: ", 9) == 0) strcpy(self->author, line + 9); else break; if (ASTIL_ReadLine(fp, line) < 9) return; } while (self->nCovers < ASTIL_MAX_COVERS && memcmp(line, " TITLE: ", 9) == 0) { ASTILCover *cover = self->covers + self->nCovers++; ASTILCover_Load(cover, fp, line); } }