{"id":724,"date":"2014-05-31T00:01:31","date_gmt":"2014-05-31T07:01:31","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=724"},"modified":"2014-05-24T08:12:35","modified_gmt":"2014-05-24T15:12:35","slug":"word-wrap-filter-repair","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=724","title":{"rendered":"Word Wrap Filter Repair"},"content":{"rendered":"<p>As a programmer, always be thinking: What can go wrong? What would be an exception? Where have I made assumptions? You can&#8217;t avoid bugs by making such challenges, but you can diminish their possibilities.<br \/>\n<!--more--><br \/>\nA case in point is the word wrap filter, presented in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=702\">last week&#8217;s Lesson<\/a>. As I pointed out, that code has two major flaws. These are discovered either through extensive testing or by reviewing the code in your noggin and trying to figure out ways to break it.<\/p>\n<p>The first flaw occurs when a newline is encountered in the buffer, before the wrapping point. In that case, you can end up with filter output that looks jagged and sloppy:<\/p>\n<pre><code>He has\r\nabdicated Government here, by declaring\r\nus out of his Protection and waging War\r\nagainst us.\r\nHe has plundered our seas,\r\nravaged our Coasts, burnt our towns, and\r\ndestroyed the lives of our people.\r\nHe is\r\nat this time transporting large Armies<\/code><\/pre>\n<p>Instead of:<\/p>\n<pre><code>He has abdicated Government here, by\r\ndeclaring us out of his Protection and\r\nwaging War against us.\r\nHe has plundered our seas, ravaged our\r\nCoasts, burnt our towns, and destroyed\r\nthe lives of our people.\r\nHe is at this time transporting large\r\nArmies<\/code><\/pre>\n<p>The second flaw happens when no whitespace characters are encountered before the right margin. In that case, the code would overflow and you might see one of those notorious Segmentation Fault errors.<\/p>\n<p>One fix at a time!<\/p>\n<p>To deal with a newline character encountered before the <code>right_margin<\/code>, the following code can be added after Line 31. That&#8217;s where the character received is placed into the <code>buffer<\/code>.<\/p>\n<pre class=\"screen\">\r\n \t\t<span class=\"comments\">\/* check for newline before margin *\/<\/span>\r\n \t\tif(*space == '\\n')\r\n \t\t{\r\n \t\t\t*space = '\\0';\r\n \t\t\tx = 0;\r\n \t\t\twhile(*(buffer+x))\r\n \t\t\t{\r\n \t\t\t\tif( *(buffer+x)==EOF)\r\n \t\t\t\t\texit(0);\r\n \t\t\t\tputchar(*(buffer+x));\r\n \t\t\t\tx++;\r\n \t\t\t}\r\n \t\t\tputchar('\\n');\r\n \t\t\tposition=0;\r\n \t\t}<\/pre>\n<p>When a newline is encountered, the <code>buffer<\/code> is immediately capped with a null character, <code>\\0<\/code>. Then the buffer is displayed, again confirming that an <em>EOF<\/em> isn&#8217;t lurking somewhere. The program then continues looking for input.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/0531a.c\">Click here<\/a> to view the full version of this code.<\/p>\n<p>The fix to determine whether any whitespace character was encountered &mdash; such as when a very, very long word appears in the text &mdash; is more extensive: It involves a structural change the program. That&#8217;s because when no place to wrap exists, then the text must be output as one chunk, without wrapping. That may not be the best way to handle such a situation, but it&#8217;s my choice as programmer.<\/p>\n<p>In the original code, a decision must be made when the <code>position<\/code> pointer passes the <code>right_margin<\/code>, indicating that the <code>buffer<\/code> is full and word wrap is necessary:<\/p>\n<pre class=\"screen\">\r\n        <span class=\"comments\">\/* check to see if the margin has been reached *\/<\/span>\r\n        if(position &gt; right_margin)\r\n        {\r\n            <span class=\"comments\">\/* Check to see whether any words found *\/<\/span>\r\n            if(buffer == space)<\/pre>\n<p>The <em>if<\/em> test above determines whether the <code>space<\/code> pointer has moved forward or not. If not, then the <code>buffer<\/code> contains one huge word that cannot be wrapped. Therefore the statements belonging to <em>if<\/em> merely display the text-chunk, no wrapping:<\/p>\n<pre class=\"screen\">\r\n            if(buffer == space)\r\n            {\r\n                for(x=0;x&lt;right_margin;x++)\r\n                {\r\n                    if( *(buffer+x)==EOF)\r\n                        exit(0);\r\n                    putchar(*(buffer+x));\r\n                }\r\n                *buffer = *(buffer+x);\r\n                position=1;\r\n            }<\/pre>\n<p>This code spits out the <code>buffer<\/code>&#8216;s contents to standard output. If an <em>EOF<\/em> is encountered, then output terminates and the program quits with an <em>exit()<\/em> function. Otherwise, the final character in the <code>buffer<\/code> is copied back to the start, so that the process can continue.<\/p>\n<p>An <em>else<\/em> follows the <em>if<\/em> test. Its statements basically process the <code>buffer<\/code>, displaying characters up until the <code>space<\/code> pointer, as was shown in the original code.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/0531b.c\">Click here<\/a> to view the final version of the word wrap filter.<\/p>\n<p>Is this filter perfect? I dunno.<\/p>\n<p>I&#8217;m not being silly: On your own you can guess which parts of the code might fail or you can experiment on a variety of interesting situations. My goal is not to create perfect code as much as I want to create well-running code. If it needs to be fixed later, then that&#8217;s part of the process. The better you can anticipate problems and bugs, and the more elegant your code, the better it is to fix problems in the future.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a programmer, you need to be constantly on the lookout for code that may not work as intended. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=724\">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-724","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\/724","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=724"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/724\/revisions"}],"predecessor-version":[{"id":745,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/724\/revisions\/745"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}