aboutsummaryrefslogtreecommitdiff
path: root/Converter/src/main.c
blob: e41304b51065ebfbd61883567ea690378b2657be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <malloc.h>
#include <stdio.h>
#include <string.h>

typedef char *string;

string compile_line(string line, size_t size_of_line)
{
  string compiled_line;
  size_t size_of_content;

  if (line[0] == '#')
  {
    // Compile a header

    // Get the level of header (h1, h2, etc)
    int depth;
    for (depth = 1; line[depth] == '#'; ++depth);
    size_of_content = size_of_line - depth; // remove hashes

    // Generate the header tags based on depth
    const int tag_size = 4;
    const int untag_size = 5;
    string tag = malloc(sizeof(*tag) * 5);
    sprintf(tag, "<h%d>", depth);
    string untag = malloc(sizeof(*untag) * 6);
    sprintf(untag, "</h%d>", depth);

    // allocate buffer with extra 9 characters for the tags
    compiled_line = malloc(sizeof(*compiled_line) * (size_of_line + 9));
    // write h1 to start of compiled_line
    strncpy(compiled_line, "<h1>", 4);
    // write the rest of the line to the compiled_line
    strncpy(compiled_line + 4, line + 1, size_of_line - 1);
    // write the end tags
    strncpy(compiled_line + size_of_line + 3, "</h1>", 5);
  }

  else
  {
    compiled_line = malloc(sizeof(*compiled_line) * size_of_line);
    strncpy(compiled_line, line, size_of_line);
  }

  return compiled_line;
}

int main()
{
  return 0;
}