readme-thing/node.c

105 lines
2.1 KiB
C
Raw Normal View History

2021-07-14 08:15:46 -07:00
#include <stdio.h>
#include <string.h>
#include "node.h"
#define STRING_BUFF_SIZE 1024
static char stringBuff[STRING_BUFF_SIZE];
Node* parseHeader(TokenList** list);
NodeList*
ParseNodes(TokenList* list)
{
NodeList* nl = malloc(sizeof(NodeList));
NodeList* currentNode = nl;
currentNode->next = NULL;
currentNode->node = NULL;
TokenList* current = list;
//while(current != NULL) {
while (1) {
switch (current->token->type) {
case TT_NEWLINE:
break;
case TT_HASH:
// start of header
//Node* nodes;
//nodes = parseHeader(current);
currentNode->node = parseHeader(&current);
break;
default:
break;
}
if (current->next == NULL) {
//printf("next is null\n");
break;
}
//printf("current = current->next;\n");
current = current->next;
}
return nl;
}
Node*
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);
return NULL;
}