summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--groffdown.c57
-rw-r--r--tree.h267
-rw-r--r--tree.txt6
3 files changed, 202 insertions, 128 deletions
diff --git a/groffdown.c b/groffdown.c
index 8338361..f0416e9 100644
--- a/groffdown.c
+++ b/groffdown.c
@@ -18,15 +18,60 @@
=============================================================================*/
+#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+
+FILE *fileptr;
+char *buffer;
+long filelen;
+
+// Node tree used by the algorithm:
#include "tree.h"
+
+node * match(node *n) {
+ /* int nomatch = 1; */
+ printf("Master node: %c\n", n->pattern );
+ for (int i = 0; i < n->childsize-1; i++) {
+ printf("Child %d, pattern: %c, identity: %d, position: %d\n",
+ i,
+ (n->child[i])->pattern,
+ (n->child[i])->identity,
+ (n->child[i])->pos);
+ };
+ printf("\n");
+ return (n->child[1]) ;
+};
+
int main() {
- /* system("clear"); */
-/* node *curr_node = &node_b; */
-printf("Pattern: '%c'\n", root.pattern);
-printf("End of test.");
-return 0;
-}
+ node *test = match(&n_a);
+ test = match(test);
+ /* test = match(&test, '*'); */
+
+ /* // Importing the file into a char[] {{{ */
+ /* // TODO: file should be either piped or passed as an argument */
+ /* fileptr = fopen("sample.gd", "rb"); */
+ /* fseek(fileptr, 0, SEEK_END); // Jump to EOF */
+ /* filelen = ftell(fileptr); // Get byte offset */
+ /* rewind(fileptr); // Jump back to beggining */
+ /* buffer = (char *)malloc(filelen * sizeof(char)); // Enough memory for the file */
+ /* fread(buffer, filelen, 1, fileptr); // Read all the file */
+ /* fclose(fileptr); // Close file */
+ /* // }}} */
+
+ /* // Pattern matching algorithm {{{ */
+ /* for (long i = 0; i < filelen; i++) { */
+ /* printf("%s", *fileptr); */
+ /* fileptr++; */
+ /* } */
+ /* // }}} */
+
+ /* // Node Tree test {{{ */
+ /* printf("Pattern: '%c'\n", root.pattern); */
+ /* printf("End of test."); */
+ /* // }}} */
+
+return 0;
+};
diff --git a/tree.h b/tree.h
index 94caad7..9de31aa 100644
--- a/tree.h
+++ b/tree.h
@@ -1,6 +1,6 @@
-typedef struct treeNode {
+typedef struct singleNode {
char pattern; // Character to match
- int pos; // Position (index) relative to the first treeNode to match
+ int pos; // Position (index) relative to the first node to match
int identity; // What happens when this treeNode is the last to be matched
// 0: Invalid match: Match pattern is most likely incomplete (or escaped)
// 1: italics delimiter
@@ -17,133 +17,142 @@ typedef struct treeNode {
// 12: automatic date (date will be formatted for groff on compile time)
// 13: manual date
// 14: indent text
+ // 15: bold&italics delimiter with a -1 offset
+ // 16: bold&italics delimiter with a -2 offset
int childsize;
- struct treeNode *child[]; // Pointer array to other child nodes
+ struct singleNode *child[]; // Pointer array to other child nodes
} node ;
-node n_a = {
- '*', 0, 1, 4,
- {&n_aa, &n_ab, &n_ac, &n_ad}
-};
+typedef struct nodeRoot {
+ int childsize;
+ struct singleNode *child[];
+} root ;
-node n_aa = {
- '\\', -1, 0, 0,
+
+// 8 depth {{{
+node n_dfaaaaaa = {
+ ':', 7, 11, 0,
{}
};
+// }}}
-node n_ab = {
- '*', 1, 2, 3,
- {&n_aba, &n_abb, &n_abc}
+// 7 depth {{{
+node n_dfaaaaa = {
+ 'r', 6, 0, 1,
+ {&n_dfaaaaaa}
};
-node n_aba = {
- '_', -1, 3, 0,
+node n_dfcaaaa = {
+ ':', 6, 10, 0,
{}
};
+// }}}
-node n_abb = {
- '*', 2, 3, 0,
- {}
+// 6 depth {{{
+node n_dfaaaa = {
+ 'o', 5, 0, 1,
+ {&n_dfaaaaa}
};
-node n_abc = {
- '_', 2, 3, 0,
+node n_dfbaaa = {
+ ':', 5, 13, 0,
{}
};
-node n_ac = {
- '_', -1, 0, 1,
- {&n_aca}
-};
-
-node n_aca = {
- '_', -2, 3, 0,
+node n_dfbaab = {
+ '\n', 5, 12, 0,
{}
};
-node n_ad = {
- '_', 1, 0, 1,
- {&n_ada}
+node n_dfcaaa = {
+ 'e', 5, 0, 1,
+ {&n_dfcaaaa}
};
+// }}}
-node n_ada = {
- '_', 2, 3, 0,
- {}
+// 5 depth {{{
+node n_dfaaa = {
+ 'h', 4, 0, 1,
+ {&n_dfaaaa}
};
-node n_b = {
- '_', 0, 1, 2,
- {&n_ba, &n_bb}
+node n_dfbaa = {
+ 'e', 4, 0, 2,
+ {&n_dfbaaa, &n_dfbaab}
};
-node n_ba = {
- '\\', -1, 0, 0,
- {}
+node n_dfcaa = {
+ 'l', 4, 0, 1,
+ {&n_dfcaaa}
};
+// }}}
-node n_bb = {
- '_', 1, 0, 1,
- {&n_bba}
+// 4 depth {{{
+node n_dfca = {
+ 'i', 2, 0, 1,
+ {&n_dfcaa}
};
-node n_bba = {
- '_', 2, 3, 0,
+node n_cbaa = {
+ '\n', -1, 5, 0,
{}
};
-node n_c = {
- '`', 0, 4, 2,
- {&n_ca, &n_cb}
+node n_deaa = {
+ '-', -3, 9, 0,
+ {}
};
-node n_ca = {
- '\\', -1, 0, 0,
- {}
+node n_dfaa = {
+ 'u', 2, 0, 1,
+ {&n_dfaaa}
};
-node n_cb = {
- '`', 1, 0, 1,
- {&n_cba}
+node n_dfba = {
+ 'a', 2, 0, 1,
+ {&n_dfbaa}
};
+// }}}
-node n_cba = {
- '`', 2, 0, 1,
- {&n_cbaa}
+// 3 depth {{{
+node n_dfc = {
+ 't', 1, 0, 1,
+ {&n_dfca}
};
-node n_cbaa = {
- '\n', -1, 5, 0,
+node n_aba = {
+ '_', -1, 15, 0,
{}
};
-node n_d = {
- '\n', 0, 0, 6,
- {&n_da, &n_db, &n_dc, &n_dd, &n_de, &n_df, }
+node n_abb = {
+ '*', 2, 3, 0,
+ {}
};
-node n_da = {
- '#', 1, 8, 0,
+node n_abc = {
+ '_', 2, 3, 0,
{}
};
-node n_db = {
- '=', 1, 6, 0,
+node n_aca = {
+ '_', -2, 16, 0,
{}
};
-node n_dc = {
- '-', 1, 7, 0,
+node n_ada = {
+ '_', 2, 3, 0,
{}
};
-node n_dd = {
- '>', 1, 14, 0,
+node n_bba = {
+ '_', 2, 3, 0,
{}
};
-node n_de = {
- '-', -1, 0, 1,
- {&n_dea}
+node n_cba = {
+ '`', 2, 0, 1,
+ {&n_cbaa}
};
node n_dea = {
@@ -151,93 +160,113 @@ node n_dea = {
{&n_deaa}
};
-node n_deaa = {
- '-', -3, 9, 0,
- {}
+node n_dfa = {
+ 'a', 1, 0, 1,
+ {&n_dfaa}
};
+node n_dfb = {
+ 'd', 1, 0, 1,
+ {&n_dfba}
+};
+// }}}
+
+// 2 depth {{{
node n_df = {
't', 3, 0, 3,
{&n_dfa, &n_dfb, &n_dfc}
};
-node n_dfa = {
- 'a', 1, 0, 1,
- {&n_dfaa}
+node n_aa = {
+ '\\', -1, 0, 0,
+ {}
};
-node n_dfaa = {
- 'u', 2, 0, 1,
- {&n_dfaaa}
+node n_ab = {
+ '*', 1, 2, 3,
+ {&n_aba, &n_abb, &n_abc}
};
-node n_dfaaa = {
- 'h', 4, 0, 1,
- {&n_dfaaaa}
+node n_ac = {
+ '_', -1, 0, 1,
+ {&n_aca}
};
-node n_dfaaaa = {
- 'o', 5, 0, 1,
- {&n_dfaaaaa}
+node n_ad = {
+ '_', 1, 0, 1,
+ {&n_ada}
};
-node n_dfaaaaa = {
- 'r', 6, 0, 1,
- {&n_dfaaaaaa}
+node n_ba = {
+ '\\', -1, 0, 0,
+ {}
};
-node &n_dfaaaaaa = {
- ':', 7, 11, 0,
+node n_bb = {
+ '_', 1, 0, 1,
+ {&n_bba}
+};
+
+node n_ca = {
+ '\\', -1, 0, 0,
{}
};
-node n_dfb = {
- 'd', 1, 0, 1,
- {&n_dfba}
+node n_cb = {
+ '`', 1, 0, 1,
+ {&n_cba}
};
-node n_dfba = {
- 'a', 2, 0, 1,
- {&n_dfbaa}
+node n_da = {
+ '#', 1, 8, 0,
+ {}
};
-node n_dfbaa = {
- 'e', 4, 0, 2,
- {&n_dfbaaa, &n_dfbaab}
+node n_db = {
+ '=', 1, 6, 0,
+ {}
};
-node n_dfbaaa = {
- ':', 5, 13, 0,
+node n_dc = {
+ '-', 1, 7, 0,
{}
};
-node n_dfbaab = {
- '\n', 5, 12, 0,
+node n_dd = {
+ '>', 1, 14, 0,
{}
};
-node n_dfc = {
- 't', 1, 0, 1,
- {&n_dfca}
+node n_de = {
+ '-', -1, 0, 1,
+ {&n_dea}
};
+// }}}
-node n_dfca = {
- 'i', 2, 0, 1,
- {&n_dfcaa}
+// 1 depth {{{
+node n_a = {
+ '*', 0, 1, 4,
+ {&n_aa, &n_ab, &n_ac, &n_ad}
};
-node n_dfcaa = {
- 'l', 4, 0, 1,
- {&n_dfcaaa}
+node n_b = {
+ '_', 0, 1, 2,
+ {&n_ba, &n_bb}
};
-node n_dfcaaa = {
- 'e', 5, 0, 1,
- {&n_dfcaaaa}
+node n_c = {
+ '`', 0, 4, 2,
+ {&n_ca, &n_cb}
};
-node n_dfcaaaa = {
- ':', 6, 10, 0,
- {}
+node n_d = {
+ '\n', 0, 0, 6,
+ {&n_da, &n_db, &n_dc, &n_dd, &n_de, &n_df, }
+};
+// }}}
+
+root tree_root = {
+ 4,
+ {&n_a, &n_b, &n_c, &n_d}
};
diff --git a/tree.txt b/tree.txt
index 59d5750..7ae7bd9 100644
--- a/tree.txt
+++ b/tree.txt
@@ -8,11 +8,11 @@ make mistakes. Let's keep this as a debugging tool if I miswrite code.
├── *,0 | (A) | * | italic (1) |
│   ├── \,-1 | (AA) | \* | void (0) |
│   ├── *,1 | (AB) | ** | bold (2) |
-│   │   ├── _,-1 | (ABA) | _** | boldit (3) |
+│   │   ├── _,-1 | (ABA) | _** | boldit(15) |
│   │   ├── *,2 | (ABB) | *** | boldit (3) |
│   │   └── _,2 | (ABC) | **_ | boldit (3) |
│   ├── _,-1 | (AC) | _* | void (0) |
-│   │   └── _,-2 | (ACA) | __* | boldit (3) |
+│   │   └── _,-2 | (ACA) | __* | boldit(16) |
│   └── _,1 | (AD) | *_ | void (0) |
│   └── _,2 | (ADA) | *__ | boldit (3) |
├── _,0 | (B) | _ | italic (1) |
@@ -21,7 +21,7 @@ make mistakes. Let's keep this as a debugging tool if I miswrite code.
│   └── _,2 | (BBA) | ___ | boldit (3) |
├── `,0 | (C) | ` | code (4) |
│   ├── \,-1 | (CA) | \` | void (0) |
-│   └── `,1 | (CB) | `` | void (0) |
+│   └── `,1 | (CB) | `` | void (0) |
│   └── `,2 | (CBA) | ``` | void (0) |
│   └── ¶,-1 | (CBAA) | ¶``` | codeB (5) |
└── ¶,0 | (D) | ¶ | void (0) |