hmmlconv.c: Deep optimisation

This commit is contained in:
Matt Mascarenhas 2017-05-01 10:44:22 +01:00
parent 39f9e30117
commit edf3975646
1 changed files with 34 additions and 20 deletions

View File

@ -12,6 +12,8 @@ exit
#include <sys/types.h> // open, mkdir
#include <sys/stat.h> // open, mkdir
#define LError(Format, ...) do { fprintf(stderr, " l.%d: " Format, LineNumber, ##__VA_ARGS__); } while(0)
typedef struct
{
char *Site;
@ -114,6 +116,11 @@ char *InPlaceUnescape(char *In)
*Out++ = *In++;
}
if(*In != '"')
{
return NULL;
}
*Out = 0;
return Result;
}
@ -142,6 +149,13 @@ Resource *LookupResource(char *Tag, char *Line)
return Res;
}
enum
{
TC_NAN = 1,
TC_COLONS = 2,
TC_OUT_OF_RANGE = 3
};
int
ValidateTimecode(char *Timecode)
{
@ -149,12 +163,12 @@ ValidateTimecode(char *Timecode)
int Colons = 0;
while(*Timecode)
{
if(!(*Timecode >= '0' && *Timecode <= '9') && *Timecode != ':') { return 1; }
if(!(*Timecode >= '0' && *Timecode <= '9') && *Timecode != ':') { return TC_NAN; }
if(*Timecode == ':')
{
++Colons;
if(Colons > 2) { return 2; }
if(Colons > 2) { return TC_COLONS; }
for(int i = 0; i < Colons; ++i)
{
HMS[Colons - i] = HMS[Colons - (i + 1)];
@ -169,7 +183,7 @@ ValidateTimecode(char *Timecode)
++Timecode;
}
if(HMS[0] > 59 || HMS[1] > 59 || Timecode[-1] == ':') { return 3; }
if(HMS[0] > 59 || HMS[1] > 59 || Timecode[-1] == ':') { return TC_OUT_OF_RANGE; }
return 0;
}
@ -177,7 +191,7 @@ ValidateTimecode(char *Timecode)
void ProcessAnnotation(char *Line, int LineNumber, FILE *OutFile)
{
SkipWhitespace(&Line);
if(*Line != '"') { fprintf(stderr, " l.%d: Syntax error, line must begin with a \": %.*s...\n", LineNumber, 8, Line); exit(1); }
if(*Line != '"') { LError("Syntax error, line must begin with a \": %.*s...\n", 8, Line); exit(1); }
fputc('[', OutFile);
char Timecode[9];
@ -191,29 +205,29 @@ void ProcessAnnotation(char *Line, int LineNumber, FILE *OutFile)
switch(ValidateTimecode(Timecode))
{
case 0: break;
case 1: fprintf(stderr, " l.%d: Invalid timecode, not a number: %s\n", LineNumber, Timecode); exit(1);
case 2: fprintf(stderr, " l.%d: Invalid timecode, too many colons: %s\n", LineNumber, Timecode); exit(1);
case 3: fprintf(stderr, " l.%d: Invalid timecode, not 0-59: %s\n", LineNumber, Timecode); exit(1);
case TC_NAN: LError("Invalid timecode, not a number: %s\n", Timecode); exit(1);
case TC_COLONS: LError("Invalid timecode, too many colons: %s\n", Timecode); exit(1);
case TC_OUT_OF_RANGE: LError("Invalid timecode, not 0-59: %s\n", Timecode); exit(1);
}
fputs(Timecode, OutFile);
fputc(']', OutFile);
if(*++Line != ':') { fprintf(stderr, " l.%d: Syntax error, missing : before: %.*s...\n", LineNumber, 8, Line); exit(1); }
if(*++Line != ' ') { fprintf(stderr, " l.%d: Syntax error, missing space before: %.*s...\n", LineNumber, 8, Line); exit(1); }
if(*++Line != '"') { fprintf(stderr, " l.%d: Syntax error, missing \" before: %.*s...\n", LineNumber, 8, Line); exit(1); }
if(*++Line != ':') { LError("Syntax error, missing : before: %.*s...\n", 8, Line); exit(1); }
if(*++Line != ' ') { LError("Syntax error, missing space before: %.*s...\n", 8, Line); exit(1); }
if(*++Line != '"') { LError("Syntax error, missing \" before: %.*s...\n", 8, Line); exit(1); }
char *LinePtr = Line;
while(*LinePtr) { ++LinePtr; }
if(LinePtr[-1] != '"') { fprintf(stderr, " l.%d: Syntax error, missing closing \": %.*s\n", LineNumber, LinePtr-Line, Line); exit(1); }
Line = InPlaceUnescape(Line);
if(!(Line = InPlaceUnescape(Line)))
{
LError("Syntax error, missing closing \": %.*s\n", Line-LinePtr, LinePtr); exit(1);
}
// convert author
if(Line[0] == '@')
{
char *P = strchr(Line, ' ');
if(!P) { fprintf(stderr, " l.%d: Invalid annotation, cannot contain only a member: %.*s...\n", LineNumber, 8, Line); exit(1); }
if(!P) { LError("Invalid annotation, cannot contain only a member: %.*s...\n", 8, Line); exit(1); }
if(P[-1] == ':')
{
@ -227,7 +241,7 @@ void ProcessAnnotation(char *Line, int LineNumber, FILE *OutFile)
char RefBuf[256];
char *RunStart = Line;
char *FirstSpace = NULL;
int ConsiderQuote = 0;
int ConsiderAuthored = 0;
fputc('[', OutFile);
@ -260,7 +274,7 @@ void ProcessAnnotation(char *Line, int LineNumber, FILE *OutFile)
}
else
{
fprintf(stderr, " l.%d: WARNING: can't find resource: %s :(\n", LineNumber, RefBuf);
LError("WARNING: can't find resource: %s :(\n", RefBuf);
}
}
@ -279,16 +293,16 @@ void ProcessAnnotation(char *Line, int LineNumber, FILE *OutFile)
if(!FirstSpace)
{
FirstSpace = LinePtr;
ConsiderQuote = 1;
ConsiderAuthored = 1;
}
else
{
ConsiderQuote = 0;
ConsiderAuthored = 0;
}
}
// Q:
else if(LinePtr[0] == 'Q' && LinePtr[1] == ':' && ConsiderQuote)
else if(LinePtr[0] == 'Q' && LinePtr[1] == ':' && ConsiderAuthored)
{
LinePtr += 2;
RunStart = LinePtr+1;