1
1
#include <fcntl.h>
2
2
#include <stdio.h>
3
3
#include <stdlib.h>
4
+ #include <string.h>
4
5
#include <sys/mman.h>
5
6
#include <sys/stat.h>
6
7
@@ -39,7 +40,8 @@ typedef struct {
39
40
40
41
#define is_whitespace (c ) ((c) == ' ' || (c) == '\t' || c == '\n')
41
42
42
- char advance_char (char * * c , int * col , int * row )
43
+ char
44
+ advance_char (char * * c , int * col , int * row )
43
45
{
44
46
(* c )++ ;
45
47
@@ -55,12 +57,11 @@ char advance_char (char **c, int *col, int *row)
55
57
int
56
58
parse (int fd , int len , const char * error )
57
59
{
58
- char * c ;
60
+ char * c , * file ;
59
61
Token current_token ;
60
62
Element * list ;
61
63
Element * current_element ;
62
- int is_start_of_line = 1 ;
63
- int row , col ;
64
+ int is_start_of_line = 1 , row , col ;
64
65
65
66
#define next_element () \
66
67
current_element++; \
@@ -84,7 +85,7 @@ parse (int fd, int len, const char *error)
84
85
85
86
current_element -> type = NONE ;
86
87
87
- c = (char * ) mmap (NULL , len , PROT_READ , MAP_PRIVATE , fd , 0 );
88
+ file = c = (char * ) mmap (NULL , len , PROT_READ , MAP_PRIVATE , fd , 0 );
88
89
89
90
if (c == MAP_FAILED )
90
91
return 0 ;
@@ -95,15 +96,15 @@ parse (int fd, int len, const char *error)
95
96
96
97
switch (* c ) {
97
98
case '{' :
98
- finish_none_element ();
99
-
100
- if (!is_start_of_line )
99
+ // only parse if it's the very first element
100
+ if (list != current_element )
101
101
break ;
102
102
103
+ finish_none_element ();
104
+
103
105
current_element -> type = HEADER ;
104
106
current_element -> data = c ;
105
107
106
- // just assume header and skip for now
107
108
while (next_char () != '}' ) {
108
109
if (* c == '\0' ) {
109
110
error = "Header not terminated" ;
@@ -117,6 +118,45 @@ parse (int fd, int len, const char *error)
117
118
next_char ();
118
119
next_element ();
119
120
break ;
121
+ case '=' :
122
+ {
123
+ char * linebreak , * cur , * ret , * prev ;
124
+ int prev_len ;
125
+
126
+ if (!is_start_of_line )
127
+ break ;
128
+
129
+ if (current_element -> len < 1 )
130
+ break ;
131
+
132
+ cur = current_element -> data ;
133
+ while ((ret = strstr (cur , "\n" ))) {
134
+ cur = ret + 1 ;
135
+ if (ret >= current_element -> data + current_element -> len - 1 ) {
136
+ ret = prev ;
137
+ break ;
138
+ }
139
+
140
+ prev = ret ;
141
+ }
142
+
143
+ if (!ret )
144
+ break ;
145
+
146
+ prev_len = current_element -> len ;
147
+ current_element -> len = ret - current_element -> data ;
148
+
149
+ finish_none_element ();
150
+
151
+ current_element -> type = HEADING_1 ;
152
+ current_element -> data = (char * ) ret ;
153
+ current_element -> len = c - ret - 1 ;
154
+
155
+ while (* c == '=' ) next_char ();
156
+
157
+ next_element ();
158
+ }
159
+ break ;
120
160
case '*' :
121
161
finish_none_element ();
122
162
@@ -241,6 +281,9 @@ parse (int fd, int len, const char *error)
241
281
printf ("%i, %.*s\n" , i -> type , i -> len , i -> data );
242
282
}
243
283
284
+ if (munmap (file , len ) != 0 )
285
+ return 0 ;
286
+
244
287
return 1 ;
245
288
}
246
289
0 commit comments