Welcome to mirror list, hosted at ThFree Co, Russian Federation.

main.c « Modest « osx « devel - github.com/lexborisov/Modest.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aea92203140aeeea5367bdffb83e20dfb16b7db3 (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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 Copyright (C) 2016 Alexander Borisov
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
 
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 Author: lex.borisov@gmail.com (Alexander Borisov)
*/

#include "myhtml/myhtml.h"
#include "mycss/mycss.h"
#include "modest/modest.h"

#include "mycss/selectors/serialization.h"
#include "mycss/declaration/init.h"
#include "modest/finder/finder.h"
#include "modest/finder/thread.h"
#include "myhtml/encoding.h"
#include "myfont/myfont.h"
#include "modest/node/serialization.h"

struct res_data {
    char *data;
    size_t size;
};

struct res_data load_data(const char* filename)
{
    FILE *f = fopen(filename, "rb");
    fseek(f, 0L, SEEK_END);
    
    long l = ftell(f);
    fseek(f, 0L, SEEK_SET);
    
    char *html = (char*)myhtml_malloc(l);
    if(fread(html, 1, l, f) != l) {
        exit(1);
    }
    
    fclose(f);
    
    struct res_data res = {html, (size_t)l};
    return res;
}

void serialization_callback(const char* data, size_t len, void* ctx)
{
    printf("%.*s", (int)len, data);
}

myhtml_tree_t * myhtml(const char* data, size_t data_size, bool is_file, bool print_result, myhtml_callback_tree_node_f cai, void* cai_ctx)
{
    setbuf(stdout, NULL);
    
    myhtml_t* myhtml = myhtml_create();
    myhtml_init(myhtml, MyHTML_OPTIONS_PARSE_MODE_SINGLE, 1, 0);
    
    uint64_t all_start = myhtml_hperf_clock(NULL);
    uint64_t tree_init_start = myhtml_hperf_clock(NULL);
    
    myhtml_tree_t* tree = myhtml_tree_create();
    myhtml_tree_init(tree, myhtml);
    
    myhtml_callback_tree_node_insert_set(tree, cai, cai_ctx);
    
    uint64_t tree_init_stop = myhtml_hperf_clock(NULL);
    uint64_t parse_start = myhtml_hperf_clock(NULL);
    
    if(is_file) {
        struct res_data res = load_data(data);
        
        myhtml_encoding_t encoding = myhtml_encoding_prescan_stream_to_determine_encoding(res.data, res.size);
        
        if(encoding == MyHTML_ENCODING_NOT_DETERMINED)
            encoding = MyHTML_ENCODING_UTF_8;
        
        myhtml_parse(tree, encoding, res.data, res.size);
        free(res.data);
    }
    else {
        myhtml_parse(tree, MyHTML_ENCODING_UTF_8, data, data_size);
    }
    
    uint64_t parse_stop = myhtml_hperf_clock(NULL);
    uint64_t all_stop = myhtml_hperf_clock(NULL);
    
    if(print_result) {
        myhtml_tree_print_node_children(tree, tree->document, stdout, 0);
    }
    
    printf("\n\nMyHTML Information:\n");
    printf("\tTicks/sec: %llu\n", (unsigned long long) myhtml_hperf_res(NULL));
    myhtml_hperf_print("\tFirst Tree init", tree_init_start, tree_init_stop, stdout);
    myhtml_hperf_print("\tParse html", parse_start, parse_stop, stdout);
    myhtml_hperf_print("\tTotal", all_start, all_stop, stdout);
    printf("\n");
    
    //myhtml_tree_destroy(tree);
    //myhtml_destroy(myhtml);
    
    return tree;
}

mycss_entry_t * mycss(const char* data, size_t data_size, bool is_file, bool print_result)
{
    // base init
    mycss_t *mycss = mycss_create();
    mycss_init(mycss);
    
    // currenr entry work init
    mycss_entry_t *entry = mycss_entry_create();
    mycss_entry_init(mycss, entry);
    
    uint64_t parse_start = myhtml_hperf_clock(NULL);
    
    if(is_file) {
        struct res_data res = load_data(data);
        mycss_parse(entry, MyHTML_ENCODING_UTF_8, res.data, res.size);
        free(res.data);
    }
    else {
        mycss_parse(entry, MyHTML_ENCODING_UTF_8, data, data_size);
    }
    
    uint64_t parse_stop = myhtml_hperf_clock(NULL);
    
    if(print_result) {
        printf("\n");
        mycss_stylesheet_t *stylesheet = mycss_entry_stylesheet(entry);
        mycss_selectors_serialization_list(entry->selectors, stylesheet->sel_list_first, serialization_callback, NULL);
    }
    
    printf("\n------------\nMyCSS Information:\n");
    printf("\tTicks/sec: %llu\n", (unsigned long long) myhtml_hperf_res(NULL));
    myhtml_hperf_print("\tParse css", parse_start, parse_stop, stdout);
    
    //mycss_entry_destroy(entry, true);
    //mycss_destroy(mycss, true);
    
    return entry;
}

void print_tree_after_all(modest_t* modest, myhtml_tree_t* myhtml_tree, myhtml_tree_node_t* scope_node, mycss_entry_t *mycss_entry)
{
    myhtml_tree_node_t* node = scope_node;
    
    while(node) {
        modest_node_t *m_node = (modest_node_t*)node->data;
        
        myhtml_tree_print_node(myhtml_tree, node, stdout);
        
        if(m_node) {
            printf("\tstyles: ");
            
            modest_node_raw_serialization(modest, m_node, serialization_callback, NULL);
            
            printf("\n");
        }
        
        if(node->child)
            node = node->child;
        else {
            while(node != scope_node && node->next == NULL)
                node = node->parent;
            
            if(node == scope_node)
                break;
            
            node = node->next;
        }
    }
}

size_t count = 0;

void modest_callback_for_create_mnode(myhtml_tree_t* tree, myhtml_tree_node_t* node, void* ctx)
{
    if(node->tag_id == MyHTML_TAG__TEXT) {
        return;
    }
    
    modest_t *modest = (modest_t*)ctx;
    
    /* create modest node */
    modest_node_t *m_node = modest_node_create(modest);
    if(m_node == NULL)
        return;
    
    modest_node_init(modest, m_node);
    
    node->data = (void*)m_node;
}

int main(int argc, const char * argv[]) {
    setbuf(stdout, NULL);
    
    //char *html_f = "/new/C-git/lexbor/test/test.html";
    char *html_f = "/new/C-git/habr/1.html";
    //char *html_f = "/new/C-git/HTML_BENTCHMARK/booking.com.html";
    
    //char *css_f = "/new/C-git/habr/1_glob.css";
    char *css_f = "/new/C-git/CSS_BENCHMARK/baseguide.css";
    
    //char *css_f = "/new/C-git/bootstrap.css";
    
    char *html = "Привет UTF8! <br><br>";
    char *css = ".radio input[type=\"radio\"]:checked + label::before {} .checkbox input[type=\"checkbox\"]:checked + label::before {}";
    
    char *selector = "menu";
    
    modest_t *modest = modest_create();
    modest_init(modest);
    
    myhtml_tree_t *myhtml_tree = myhtml(html_f, strlen(html_f), true, false, modest_callback_for_create_mnode, (void*)modest);
    mycss_entry_t *mycss_entry = mycss(css_f, strlen(css_f), true, false);
    
//    myhtml_tree_t *myhtml_tree = myhtml(html, strlen(html), false, true, modest_callback_for_create_mnode, (void*)modest);
//    mycss_entry_t *mycss_entry = mycss(css, strlen(css), false, false);
    
    modest->myhtml_tree = myhtml_tree;
    modest->mycss_entry = mycss_entry;
    
    mycss_stylesheet_t *stylesheet = mycss_entry_stylesheet(mycss_entry);
    
    /* simple api */
    uint64_t parse_simple_start = myhtml_hperf_clock(NULL);
    uint64_t parse_simple_stop = myhtml_hperf_clock(NULL);
    
    
    
    /* full api */
    uint64_t parse_start = myhtml_hperf_clock(NULL);
    
    
    modest_finder_t* finder = modest_finder_create();
    modest_finder_init(finder);
    
//     threads 
    modest_finder_thread_t *finder_thread = modest_finder_thread_create();
    modest_finder_thread_init(finder, finder_thread, 4);
    
    modest_finder_thread_process(modest, finder_thread, myhtml_tree->node_html, stylesheet->sel_list_first);
    
    finder_thread = modest_finder_thread_destroy(finder_thread, true);
    finder = modest_finder_destroy(finder, true);
    
    
    uint64_t parse_stop = myhtml_hperf_clock(NULL);
    
//    print_tree_after_all(modest, myhtml_tree, myhtml_tree->node_html, mycss_entry);
    
    printf("\n\n------------\nInformation:\n");
    printf("\tTicks/sec: %llu\n", (unsigned long long) myhtml_hperf_res(NULL));
    myhtml_hperf_print("\tFound selectors", parse_start, parse_stop, stdout);
    myhtml_hperf_print("\tParse and Found Simple selectors", parse_simple_start, parse_simple_stop, stdout);
    printf("\n");
    
//    printf("\nPrint result:\n");
//    for(size_t i = 0; i < full_collection->length; i++) {
//        myhtml_tree_print_node(myhtml_tree, full_collection->list[i], stdout);
//    }
//    printf("\nFound: %zu\n", full_collection->length);
//    printf("\n");
//
//    modest_finder_destroy(finder_full, true);
    
    modest = modest_destroy(modest, true);
    
    mycss_t* mycss = mycss_entry->mycss;
    mycss_stylesheet_destroy(stylesheet, true);
    mycss_entry_destroy(mycss_entry, true);
    mycss_destroy(mycss, true);
    
    myhtml_t* myhtml = myhtml_tree->myhtml;
    myhtml_tree_destroy(myhtml_tree);
    myhtml_destroy(myhtml);
    
    return 0;
}