Add parse lib
This commit is contained in:
parent
1475216b23
commit
725f78788b
4
Makefile
4
Makefile
|
@ -10,3 +10,7 @@ all: debug
|
|||
debug:
|
||||
mkdir -p build/debug
|
||||
$(CC) -o $(BUILD_DIR)/debug/$(BUILD_NAME)-$(OS) $(CFLAGS) src/*
|
||||
|
||||
testgemparse:
|
||||
mkdir -p $(BUILD_DIR)/test
|
||||
$(CC) -o $(BUILD_DIR)/test/gemparse-$(OS) -D TESTGEMPARSE src/gemparse.c
|
||||
|
|
|
@ -0,0 +1,156 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef TESTGEMPARSE
|
||||
|
||||
// specific test functions
|
||||
|
||||
void addH1 (const char *line);
|
||||
void addH2 (const char *line);
|
||||
void addH3 (const char *line);
|
||||
void addLink (const char *url, const char *text);
|
||||
|
||||
// general functions
|
||||
|
||||
int parseFile (char *path);
|
||||
|
||||
#endif
|
||||
|
||||
int
|
||||
parseFile (char *path)
|
||||
{
|
||||
FILE *fileToParse = fopen (path, "r");
|
||||
char line[4096];
|
||||
|
||||
if (fileToParse == NULL)
|
||||
return 1;
|
||||
|
||||
while (fgets (line, 4096, fileToParse) != NULL)
|
||||
{
|
||||
int size = strlen (line);
|
||||
|
||||
if (size > 3 && line[0] == '#' && line[1] == '#' && line[2] == '#')
|
||||
{
|
||||
int i = 3;
|
||||
|
||||
//Remove whitespace before the text
|
||||
while (i < size && line[i] == ' ')
|
||||
i++;
|
||||
|
||||
//Remove tailing '\n'
|
||||
line[size - 1] = '\0';
|
||||
|
||||
addH3 (line + i);
|
||||
}
|
||||
else if (size > 2 && line[0] == '#' && line [1] == '#')
|
||||
{
|
||||
int i = 2;
|
||||
|
||||
//Remove whitespace before the text
|
||||
while (i < size && line[i] == ' ')
|
||||
i++;
|
||||
|
||||
//Remove tailing '\n'
|
||||
line[size - 1] = '\0';
|
||||
|
||||
addH2 (line + i);
|
||||
}
|
||||
else if (size > 1 && line[0] == '#')
|
||||
{
|
||||
int i = 1;
|
||||
|
||||
//Remove whitespace before the text
|
||||
while (i < size && line[i] == ' ')
|
||||
i++;
|
||||
|
||||
//Remove tailing '\n'
|
||||
line[size - 1] = '\0';
|
||||
|
||||
addH1 (line + i);
|
||||
}
|
||||
else if (size > 2 && line[0] == '=' && line[1] == '>')
|
||||
{
|
||||
int beginUrl = 2;
|
||||
int urlLen = 0;
|
||||
int beginText;
|
||||
|
||||
//Remove whitespace before the url
|
||||
while (beginUrl < size && line[beginUrl] == ' ')
|
||||
beginUrl++;
|
||||
|
||||
//Get URL lenght
|
||||
while (beginUrl + urlLen < size && line[beginUrl + urlLen] != ' ')
|
||||
urlLen++;
|
||||
|
||||
//mark end of url
|
||||
|
||||
line[beginUrl + urlLen] = '\0';
|
||||
|
||||
// text begin after the whitespace after url
|
||||
beginText = urlLen + beginUrl + 1;
|
||||
|
||||
//Remove whitespace between URL and text
|
||||
while (beginText < size && line[beginText] == ' ')
|
||||
beginText++;
|
||||
|
||||
//If not text, text = url
|
||||
if (beginText == size)
|
||||
addLink (line + beginUrl, line + beginUrl);
|
||||
else
|
||||
{
|
||||
//Remove tailing '\n'
|
||||
line[size - 1] = '\0';
|
||||
|
||||
addLink (line + beginUrl, line + beginText);
|
||||
}
|
||||
}
|
||||
else
|
||||
printf ("%s", line);
|
||||
}
|
||||
|
||||
fclose (fileToParse);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef TESTGEMPARSE
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char **argv)
|
||||
{
|
||||
if (argc >= 2)
|
||||
return parseFile (argv[1]);
|
||||
else
|
||||
{
|
||||
fprintf (stderr, "USAGE: %s FILE\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
addH1 (const char *text)
|
||||
{
|
||||
printf ("h1 {\n%s\n}\n", text);
|
||||
}
|
||||
|
||||
void
|
||||
addH2 (const char *text)
|
||||
{
|
||||
printf ("h2 {\n%s\n}\n", text);
|
||||
}
|
||||
|
||||
void
|
||||
addH3 (const char *text)
|
||||
{
|
||||
printf ("h3 {\n%s\n}\n", text);
|
||||
}
|
||||
|
||||
void
|
||||
addLink (const char *url,
|
||||
const char *text)
|
||||
{
|
||||
printf ("link {\n%s\n%s\n}\n", url, text);
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue