readme-thing/node.c

179 lines
3.9 KiB
C

#include <stdio.h>
#include <string.h>
#include "node.h"
#define STRING_BUFF_SIZE 1024
static char stringBuff[STRING_BUFF_SIZE];
HeaderNode* parseHeader(TokenList** list);
NodeList*
ParseNodes(TokenList* list)
{
printf("ParseNodes() start\n");
NodeList* nl = malloc(sizeof(NodeList));
nl->first = NULL;
/*currentNode->next = NULL;*/
/*currentNode->node = NULL;*/
TokenList* currentToken = list;
Node* prevNode = NULL;
printf("ParseNodes() loop\n");
while (1) {
Node* currentNode = NULL;
switch (currentToken->token->type) {
case TT_NEWLINE:
break;
case TT_HASH:
// start of header
currentNode = (Node*)parseHeader(&currentToken);
break;
case TT_EOF:
printf("EOF found\n");
return nl;
default:
break;
}
if (currentToken->next == NULL) {
printf("currentToken->next == NULL\n");
break;
}
currentToken = currentToken->next;
if (currentNode == NULL)
continue;
if (prevNode != NULL) {
prevNode->next = currentNode;
}
if (nl->first == NULL) {
nl->first = currentNode;
}
prevNode = currentNode;
}
return nl;
}
HeaderNode*
parseHeader(TokenList** list)
{
TokenList* l = *list;
// Count the number of TT_HASH tokens
int count = 1;
while (l->next != NULL && l->next->token->type == TT_HASH)
{
count++;
l = l->next;
}
if (l->next == NULL)
{
printf("Header missing text");
return NULL;
}
l = l->next;
// Trim leading whitespace
while (l->next != NULL && l->token->type == TT_WHITESPACE)
{
l = l->next;
}
if (l->next == NULL)
{
printf("Header missing text");
return NULL;
}
stringBuff[0] = '\0';
while (1)
{
int bufSize = strlen(stringBuff);
int litSize = strlen(l->token->literal);
if (bufSize + litSize + 1 > STRING_BUFF_SIZE)
{
printf("Buffer not big enough!");
return NULL;
}
strncat(stringBuff, l->token->literal, strlen(l->token->literal));
if (l->next == NULL || l->next->token->type == TT_NEWLINE)
{
break;
}
l = l->next;
}
*list = l;
printf("header hash count: %d\ntext: '%s'\n", count, stringBuff);
HeaderNode* retval = malloc(sizeof(HeaderNode));
switch(count) {
case 1:
retval->type = NT_Header1;
break;
case 2:
retval->type = NT_Header2;
break;
case 3:
retval->type = NT_Header3;
break;
default:
retval->type = NT_Header4;
break;
}
retval->next = NULL;
retval->rawText = stringBuff;
return retval;
}
char*
NodeTypeString(NodeType t)
{
switch(t) {
case NT_Header1:
return "NT_Header1";
case NT_Header2:
return "NT_Header2";
case NT_Header3:
return "NT_Header3";
case NT_Header4:
return "NT_Header4";
case NT_Paragraph:
return "NT_Paragraph";
case NT_UnorderedList:
return "NT_UnorderedList";
case NT_OrderedList:
return "NT_OrderedList";
case NT_InlineCode:
return "NT_InlineCode";
case NT_BlockCode:
return "NT_BlockCode";
case NT_BlockQuote:
return "NT_BlockQuote";
case NT_Bold:
return "NT_Bold";
case NT_Underline:
return "NT_Underline";
default:
snprintf(stringBuff, 1000, "unknown NodeType: %d", t);
return stringBuff;
}
}