-
Notifications
You must be signed in to change notification settings - Fork 0
/
styleparsertester.c
140 lines (121 loc) · 4.12 KB
/
styleparsertester.c
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* PEG Markdown Highlight
* Copyright 2011 Ali Rantakari -- http://hasseg.org
* Licensed under the GPL2+ and MIT licenses (see LICENSE for more info).
*
* styleparsertester.c
*
* Program for testing the stylesheet parser. Reads stylesheet input from stdin
* and prints out the parsing results in a human-readable format.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pmh_styleparser.h"
#include "pmh_parser.h"
#define READ_BUFFER_LEN 1024
char *get_contents(FILE *f)
{
char buffer[READ_BUFFER_LEN];
size_t content_len = 1;
char *content = malloc(sizeof(char) * READ_BUFFER_LEN);
content[0] = '\0';
while (fgets(buffer, READ_BUFFER_LEN, f))
{
content_len += strlen(buffer);
content = realloc(content, content_len);
strcat(content, buffer);
}
return content;
}
void print_styles(pmh_style_attribute *list)
{
while (list != NULL)
{
char *attr_name = (list->type == pmh_attr_type_other)
? list->name
: pmh_attr_name_from_type(list->type);
if (list->type == pmh_attr_type_other)
printf(" \"%s\" = ", attr_name);
else
printf(" %s = ", attr_name);
if (list->type == pmh_attr_type_background_color
|| list->type == pmh_attr_type_foreground_color
|| list->type == pmh_attr_type_caret_color
)
printf("%i,%i,%i,%i\n",
list->value->argb_color->alpha,
list->value->argb_color->red,
list->value->argb_color->green,
list->value->argb_color->blue);
else if (list->type == pmh_attr_type_font_style)
{
bool any_styles = false;
if (list->value->font_styles->bold == true)
printf("bold "), any_styles = true;
if (list->value->font_styles->italic == true)
printf("italic "), any_styles = true;
if (list->value->font_styles->underlined == true)
printf("underlined "), any_styles = true;
if (!any_styles)
printf("(none)");
printf("\n");
}
else if (list->type == pmh_attr_type_font_size_pt)
{
bool rel = list->value->font_size->is_relative;
int size = list->value->font_size->size_pt;
printf("%s%i pt (%s)\n",
(rel ? ((0 < size) ? "+" : "") : ""),
size,
(rel ? "relative" : "absolute"));
}
else if (list->type == pmh_attr_type_font_family)
printf("\"%s\"\n", list->value->font_family);
else if (list->type == pmh_attr_type_other)
printf("\"%s\"\n", list->value->string);
else
printf("??? (unknown attribute type)\n");
list = list->next;
}
}
void parsing_error_callback(char *error_message, int line_number,
void *context_data)
{
fprintf(stderr, "ERROR (line %i): %s\n", line_number, error_message);
}
int main(int argc, char *argv[])
{
char *input = get_contents(stdin);
pmh_style_collection *styles = pmh_parse_styles(input, &parsing_error_callback, NULL);
printf("------\n");
if (styles->editor_styles != NULL)
{
printf("Editor styles:\n");
print_styles(styles->editor_styles);
printf("\n");
}
if (styles->editor_current_line_styles != NULL)
{
printf("Current line styles:\n");
print_styles(styles->editor_current_line_styles);
printf("\n");
}
if (styles->editor_selection_styles != NULL)
{
printf("Selection styles:\n");
print_styles(styles->editor_selection_styles);
printf("\n");
}
int i;
for (i = 0; i < pmh_NUM_LANG_TYPES; i++)
{
pmh_style_attribute *cur = styles->element_styles[i];
if (cur == NULL)
continue;
printf("%s:\n", pmh_element_name_from_type(cur->lang_element_type));
print_styles(cur);
}
pmh_free_style_collection(styles);
free(input);
return 0;
}