{"id":1644,"date":"2015-11-21T00:01:31","date_gmt":"2015-11-21T08:01:31","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1644"},"modified":"2015-11-28T08:15:37","modified_gmt":"2015-11-28T16:15:37","slug":"encoding-and-decoding-part-v","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1644","title":{"rendered":"Encoding and Decoding, Part V"},"content":{"rendered":"<p>You don&#8217;t want your decoding program to waste time attempting to process input that isn&#8217;t in the proper format. That&#8217;s why the encoding program bothers to put a specific header as the first line of text. Therefore, one of the first tasks for the decoding program is to check for that specific header.<br \/>\n<!--more--><br \/>\nThe following code builds upon the filter created in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1628\">last week&#8217;s Lesson<\/a>. Standard input is processed one line at a time. In this modification, however, special attention is paid to the first line of input. Here is the code, which includes the addition of the <em>check_file()<\/em> function:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n#define LINE_LENGTH 73\r\n#define TRUE 1\r\n#define FALSE 0\r\n\r\nvoid check_file(char *line);\r\n\r\nint main()\r\n{\r\n    char line_buffer[LINE_LENGTH];\r\n    char c;\r\n    int buffer_index = 0;\r\n    int first_line = TRUE;\r\n\r\n    while(1)\r\n    {\r\n        c = getchar();\r\n        if( c == EOF)\r\n            break;\r\n        line_buffer[buffer_index] = c;\r\n        buffer_index++;\r\n        if( buffer_index &gt; LINE_LENGTH)\r\n        {\r\n            <span class=\"comments\">\/* overflow condition *\/<\/span>\r\n            puts(\"\\nInvalid hexcode line format\");\r\n            return(1);\r\n        }\r\n        if( c == '\\n')\r\n        {\r\n            <span class=\"comments\">\/* terminate string *\/<\/span>\r\n            line_buffer[buffer_index] = '\\0';\r\n            <span class=\"comments\">\/* process first line separately *\/<\/span>\r\n            if(first_line)\r\n            {\r\n                check_file(line_buffer);\r\n                first_line = FALSE;\r\n            }\r\n            <span class=\"comments\">\/* display contents *\/<\/span>\r\n            printf(\"%s\",line_buffer);\r\n            <\/span>\/* reset index *\/<\/span>\r\n            buffer_index = 0;\r\n        }\r\n    }\r\n\r\n    return(0);\r\n}\r\n\r\nvoid check_file(char *line)\r\n{\r\n    if( strncmp(line,\"START HEX CODE\",14) == 0 )\r\n    {\r\n        <span class=\"comments\">\/* check version number here *\/<\/span>\r\n        <span class=\"comments\">\/* offset 17 bytes *\/<\/span>\r\n        return;\r\n    }\r\n    else\r\n    {\r\n        puts(\"Improper hexcode file format\");\r\n        exit(1);\r\n    }\r\n}<\/pre>\n<p>This version of the code requires a few more header files, defines TRUE and FALSE (Lines 6 and 7), and declares the <em>check_file()<\/em> prototype (Line 9).<\/p>\n<p>Line 16 sets the <code>first_line<\/code> variable to TRUE, indicating that the first line must be processed. An <em>if<\/em> test is made at Line 36, so the first line processed is sent off to the <em>check_file()<\/em> function. Then the <code>first_file<\/code> variable is reset to FALSE so that the first line processing doesn&#8217;t take place again.<\/p>\n<blockquote><p>I didn&#8217;t need to write <em>check_file()<\/em> as a function as it&#8217;s called only once. I made it a function to keep the <em>main()<\/em> function&#8217;s <em>while<\/em> loop more readable; it fits on a single screen.<\/p><\/blockquote>\n<p>The <em>check_file()<\/em> function starts at Line 51. An <em>strncmp()<\/em> function (Line 53) compares the first line of standard input to the text that should appear at the start of a hexcode-formatted document. If the test is positive, then the version number could be checked; I&#8217;ve placed comments in the code at Lines 55 and 56 where that process would take place. Otherwise the function returns at Line 57.<\/p>\n<p>When the header doesn&#8217;t match, the <em>else<\/em> statements process. An error message is displayed at Line 61 and the program terminates at Line 62.<\/p>\n<p>At this point, the input isn&#8217;t decoded. Before I do that modification to the code, I&#8217;ll add another tidbit, which is to check for the final line of formatted data. That code is presented in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1656\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Checking the file format. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1644\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1644","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1644"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1644\/revisions"}],"predecessor-version":[{"id":1671,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1644\/revisions\/1671"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}