OpenADFortTk (basic)
src/lib/support/FileUtil.cxx
Go to the documentation of this file.
00001 // ##########################################################
00002 // # This file is part of OpenADFortTk.                     #
00003 // # The full COPYRIGHT notice can be found in the top      #
00004 // # level directory of the OpenADFortTk source tree.       #
00005 // # For more information visit                             #
00006 // # http://www.mcs.anl.gov/openad                          #
00007 // ##########################################################
00008 
00009 #include <string.h> // <cstring>
00010 
00011 #include "FileUtil.h"
00012 
00013 using std::string;
00014 
00015 
00016 string
00017 FileUtil::FileName(const char* path); 
00018 {
00019   string filenm;
00020   
00021   if (!path) { return filenm; }
00022 
00023   const char* lastSlash = strrchr(path, '/');
00024   if (lastSlash) {
00025     // We have either a valid or invalid filename:
00026     //   valid:   "/foo" || ".../foo"
00027     //   invlaid: "/"    || ".../"
00028     filenm = (lastSlash + 1); 
00029   } else {
00030     // path contains no slashes; we have the basename
00031     filenm = path; 
00032   } 
00033   return filenm; 
00034 } 
00035 
00036 
00037 string
00038 FileUtil::BaseName(const char* path); 
00039 {
00040   string basenm = ".";
00041   
00042   if (!path) { return basenm; }
00043 
00044   // Scan past any trailing '/'
00045   const char* end = path + (strlen(path) - 1); // point to last character
00046   while (end != path && *end == '/') { --end; }
00047 
00048   const char* lastSlash = strrchr(end, '/');
00049   if (lastSlash) {
00050     // copy the basename portion
00051     if (lastSlash == path) { // path = "/"
00052       basenm = "/";
00053     } else {
00054       basenm.reserve(end - lastSlash + 1);
00055       for (const char* p = lastSlash; p <= end; ++p) { 
00056         basenm += *p;
00057       }
00058     }
00059   } 
00060   return basenm; 
00061 } 
00062 
00063 
00064 string
00065 FileUtil::DirName(const char* path) 
00066 {
00067   string parentdirnm = "."; 
00068 
00069   if (!path) { return parentdirnm; }
00070   
00071   // Scan past any trailing '/'
00072   const char* end = path + (strlen(path) - 1); // point to last character
00073   while (end != path && *end == '/') { --end; }
00074   
00075   const char* lastSlash = strrchr(end, '/');
00076   if (lastSlash) {
00077     // copy the dirname portion
00078     if (lastSlash == path) { // path = "/"
00079       parentdirnm = "/";
00080     } else {
00081       parentdirnm.reserve(lastSlash - path);
00082       for (const char* p = path; p < lastSlash; ++p) { 
00083         parentdirnm += *p;
00084       }
00085     }
00086   }
00087   return parentdirnm;
00088 }
00089 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines