{"id":2791,"date":"2017-11-08T00:01:23","date_gmt":"2017-11-08T08:01:23","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2791"},"modified":"2017-11-04T11:23:01","modified_gmt":"2017-11-04T18:23:01","slug":"filename-extractor-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2791","title":{"rendered":"Filename Extractor &#8211; Solution"},"content":{"rendered":"<p>When I see a problem such as finding a filename in a pathname, one of the first things I think of are regular expressions. For <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2776\">this month&#8217;s Exercise<\/a>, however, that&#8217;s not the solution I coded.<br \/>\n<!--more--><br \/>\nA <em>regular expression<\/em> is a text-matching string. It&#8217;s a melange of various special characters, such as the <code>*<\/code> and <code>?<\/code> wildcards you&#8217;re probably familiar with. The variety of characters and the complexity of how they&#8217;re used drives computer nerds nuts. Books and cheat sheets abound on regular expressions, and you could argue that some computer programming languages are simply bizarre extensions of regular expressions.<\/p>\n<p>Due to the complexity of regular expressions, I didn&#8217;t even bother with that approach for my solution. Instead, I decided to work backwards through a pathname. Here&#8217;s my logic:<\/p>\n<ul>\n<li>If the string is empty, no filename (or pathname) exists.<\/li>\n<li>Anything to the right of the final \/ (path separator) is a filename.<\/li>\n<li>If the \/ isn&#8217;t found, the pathname is the filename.<\/li>\n<li>If the \/ is the last character of the pathname, no filename is available.<\/li>\n<\/ul>\n<p>To keep my solution consistent, I define the pathname separator character as <code>SEPARATOR<\/code>. For Unix:<\/p>\n<p><code>#define SEPARATOR '\/'<\/code><\/p>\n<p>And DOS, er, Windows:<\/p>\n<p><code>#define SEPARATOR '\\\\'<\/code><\/p>\n<p>Remember, two backslashes must be specified as <code>\\<\/code> is an escape character.<\/p>\n<p>Here is my solution for the <em>filename()<\/em> function:<\/p>\n<pre class=\"screen\">\r\nchar *filename(char *path)\r\n{\r\n    char *p;\r\n    int len;\r\n\r\n<span class=\"comments\">\/* find the end of the string *\/<\/span>\r\n    len = strlen(path);\r\n\r\n<span class=\"comments\">\/* if empty string, return empty string *\/<\/span>\r\n    if( len == 0 )\r\n        return(\"[Empty String]\");\r\n\r\n<span class=\"comments\">\/* find the terminating separator *\/<\/span>\r\n    p = path+len;\r\n    while( *p != SEPARATOR)\r\n    {\r\n        p--;\r\n        <span class=\"comments\">\/* don't go too far! *\/<\/span>\r\n        if( p == path )\r\n            return(path);\r\n    }\r\n    p++;    <span class=\"comments\">\/* increment beyond the separator *\/<\/span>\r\n\r\n<span class=\"comments\">\/* catch no filename condition *\/<\/span>\r\n    if( *p == '\\0')\r\n        return(\"[No filename]\");\r\n    else\r\n        return(p);\r\n}<\/pre>\n<p>The <em>strlen()<\/em> function locates the end of the string <code>path<\/code>. Immediately an <em>if<\/em> statement compares that length with zero, and if true, the text <code>[Empty String]<\/code> is returned.<\/p>\n<p>The statement <code>p = path+len<\/code> sets pointer <code>p<\/code> to the end of the string. Next, a <em>while<\/em> loop backtracks through string <code>path<\/code> until the <code>SEPARATOR<\/code> character is found. If <code>p<\/code> backs up to the start of the string, condition <code>p == path<\/code>, the pathname is the filename, and it&#8217;s returned.<\/p>\n<p>After the separator is located, then <em>while<\/em> loop stops and <code>p<\/code> is incremented to point at the start of the filename (after the separator character). At this point, if <code>*p=='\\0'<\/code>, the <code>SEPARATOR<\/code> terminates the path, meaning no file is present and the text <code>[No filename]<\/code> is returned. Otherwise, the location in <code>p<\/code> is returned, which is the filename string.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/11\/11exercise.c\">Click here<\/a> to view the full code.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/11\/11exercise-dos.c\">Click here<\/a> to view the Windows version.<\/p>\n<p>I can think of a few other ways to solve this Exercise, and my solution isn&#8217;t the best or most perfect. If your solution works and extracts the filenames properly, great!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I see a problem such as finding a filename in a pathname, one of the first things I think of are regular expressions. For this month&#8217;s Exercise, however, that&#8217;s not the solution I coded.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2791","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2791","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=2791"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2791\/revisions"}],"predecessor-version":[{"id":2811,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2791\/revisions\/2811"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}