c - Modular program design -
my question unfortunately badly formed i'm not entirely call i'm trying do. apologies that.
it came since i'm trying code basic browser want implement in c , thinking how best go it. basic idea being libcurl (for network interaction) -> libxml2 (parse html) -> ui , manner of getting libcurl accept or post requests ui (haven't gotten point yet).
however, approuch severely limited, if want check whether it's pdf , send off libpoppler before handing libxml2 i'll have recode entire program flow. further, if want use parts of program (say, libcurl -> pdftohtml -> libxml2 part) , send off program (for example w3m instead of ui), again don't see how manage that.
i instead write perl or python wrapper curl, libxml2, etc, or along lines of "curl example.com | parser | ui". doing in perl or python still seems i'll have recode program logic every time want new, , piping seems inelegant. in c if possible.
so question is; 1 call idea? i've been driving myself crazy trying figure out how search solution problem can't name. know has modularity, don't know , modularity very broad term. secondly , optionally if point me in direction of solution appreciate although it's not important it's called.
thanks read this. :)
first suggest take @ http://www.amazon.com/interfaces-implementations-techniques-creating-reusable/dp/0201498413. second browsers asynchronous going need event library libuv
or libev
. modern websites require javascript function properly, adding javascript engine browser complicate project. don't see mention of how plan on parsing http being sent , browser, suggest https://github.com/joyent/http-parser.
as question on control flow, have function parse's response server , use's switch()
handle various types of data being sent browser. there field in http header explains content type , way browser should able call different functions based of content type is.
also take @ function pointers, both here polymorphism (in c) , here how function pointers in c work? . function pointers would/could more eloquent way solve problem instead having large switch statements through out code. function pointers can have 1 function when called in program behaves differently.
i try explain below browser example.
so lets browser got http response server. http response looks in c
.
struct http_res { struct http_header *header; struct http_body *body int (*decode_body)(char **); };
so first http parser parse http header , figure out if it's valid response , if there's content, etc, etc. if there content parser check type , based off, if it's html, javascript, css, or whatever parser set function pointer point @ right function decode http body.
static int decode_javascript(char **body) { /* whatever takes parse javascript http. */ return 0; } static int decode_html(char **body) { /* whatever takes parse html http. */ return 0; } static int decode_css(char **body) { /* whatever takes parse css http. */ return 0; } int parse_http_header(struct http_res *http) { /* ... lots of other code figure out content type. ... */ switch(body_content_type) { case bct_javascript: http->decode_body = &decode_javascript; break; case bct_html: http->decode_body = &decode_html; break; case bct_css: http->decode_body = &decode_css; break; default: printf("error can't parse body type.\n"); return -1; } return 0; }
now when pass http request part of browser function can call decode_body()
in http response object , end decoded body can understand, out knowing it's decoding.
int next_function(struct http_res * res) { char *decoded_body; int rtrn; /* can decode http body out knowing it. call decode_body() , end buffer decoded data in it. */ rtrn = res->decode_body(&decoded_body); if(rtrn < 0) { printf("can't decode body.\n"); return -1; } return 0; }
to make program modular @ least in c
, stick various parts of browser in different shared libraries, http parser, event library, javascript engine, html parser, etc, etc. create interfaces between each library , able swap out each library different 1 having change program, link different library @ run time. take @ dr robert martin(uncle bob) talks extensively. talk lacks slides https://www.youtube.com/watch?v=aslutijjqde , starts @ 8:20. 1 interesting, , has slides: https://www.youtube.com/watch?v=wpkdn78p884 .
and nothing c
, perl
or python
means have recode program logic. have design program each module not know each other. module knows interface , if connect 2 modules both "speak" same interface have created modular system. it's how internet works various computers on internet not need know other computer is, or it's doing, or it's operating system, need know tcp/ip
, can communicate other devices on internet.
Comments
Post a Comment