Modular program design

My question, unfortunately, is poorly formed, as I am not quite sure to name what I am trying to do. We apologize for this.

It appeared as I try to encode a really basic browser that I want to implement in C, and I was thinking about how best to do it. The main idea is something like libcurl (for networking) -> libxml2 (parse HTML) -> UI, and then some way to get libcurl to receive GET or POST requests from the user interface (until we get to that).

However, this approach is strictly limited, if I say, I want to check if it is a PDF, and then send it to libpoppler before passing it to libxml2. I will have to transcode the entire stream of the entire program. Also, if I want to use parts of my program (say libcurl β†’ pdftohtml β†’ libxml2 part) and send it to another program (for example, w3m instead of my user interface), I again do not see how I will manage this.

Instead, I could just write a Perl or Python wrapper for curl, libxml2, etc. or do something according to "curl example.com | parser | UI". However, to do this in Perl or Python still seems that I will have to transcode my program logic every time I want to do something new, and the pipeline all seems inelegant. I would also like to do this in C, if possible.

So my question is; what can be called this idea? I drove me crazy trying to figure out how to look for a solution to a problem that I cannot name. I know that this has something to do with modularity, but I don’t know that specifically modularity is a very broad term. Secondly, and not necessarily, if someone can point me towards a solution, I would appreciate it, although this is not as important as what he called.

Thanks to everyone who read this. :)

+5
source share
1 answer

First I suggest you take a look at http://www.amazon.com/Interfaces-Implementations-Techniques-Creating-Reusable/dp/0201498413 . Secondly, most browsers are asynchronous, so you will need an event library like libuv or libev . In addition, most modern websites require javascript to function properly, but adding a JavaScript browser to your browser will greatly complicate the design. I also do not see mention of how you plan to parse the http sent to and from your browser, I suggest https://github.com/joyent/http-parser .

As for your question about the control flow, I will have a function that parses the response from the server and uses switch() to process the various types of data sent to your browser. There is a field in the http header that explains the type of content, and so your browser should be able to call different functions based on the type of content.

Also look at pointers to functions, how is Polymorphism (in C) here, and here How do pointers to objects in C work? . Function pointers would / could be a more eloquent way to solve your problem, instead using large switch statements through your code. With function pointers, you can have one function that behaves differently when called in your program.

In the following example, I will try to explain the browser.

So, let's say your browser just received an HTTP response from some server. The http response looks something like this: C

 struct http_res { struct http_header *header; struct http_body *body int (*decode_body)(char **); }; 

So first, your http parser will parse the http header and find out if it is a valid answer, and if there is content, etc. etc. If there is content, the parser will check the type and rely on it if it is html, javascript, css or regardless of the fact that the analyzer sets the function pointer to the desired function to decode the http body.

 static int decode_javascript(char **body) { /* Whatever it takes to parse javascript from http. */ return 0; } static int decode_html(char **body) { /* Whatever it takes to parse html from http. */ return 0; } static int decode_css(char **body) { /* Whatever it takes to parse css from http. */ return 0; } int parse_http_header(struct http_res *http) { /* ... lots of other code to 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 we pass the HTTP request to another part of the browser, this function can call decode_body() in the HTTP response object, and in the end it will receive a decoded body, which it can understand without knowing what it is decrypting.

 int next_function(struct http_res * res) { char *decoded_body; int rtrn; /* Now we can decode the http body with out knowing anything about it. We just call decode_body() and end up with a buffer with the 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 your program truly modular, at least in C , you could use different parts of your browser in different shared libraries, such as an HTTP parser, an event library, a Javascript engine, an html parser, etc. Then you will create interfaces between each library, and you can swap each library with another to change your program, you would link another library at runtime. Take a look at Dr. Robert Martin (Uncle Bob), whom he talks about. This conversation is good, but it lacks slides https://www.youtube.com/watch?v=asLUTiJJqdE , starts at 8:20. This is also interesting, and it has slides: https://www.youtube.com/watch?v=WpkDN78P884 .

And finally, nothing about C , perl or python means you have to transcode your program logic. You will need to develop your program so that each module does not know about each other. The module knows about the interface, and if you connect two modules that "speak" on the same interface, you have created a modular system. Just as the Internet works on different computers on the Internet, you don’t need to know what another computer is or what it does, or its operating system, all they need to know is TCP/IP , and they can communicate with all other devices in the Internet.

+4
source

All Articles