FileUtil.cxx

Go to the documentation of this file.
00001 // -*-Mode: C++;-*-
00002 // $Header: /Volumes/cvsrep/developer/OpenADFortTk/src/lib/support/FileUtil.cxx,v 1.1 2004/06/01 22:25:42 eraxxon Exp $
00003 
00004 // * BeginRiceCopyright *****************************************************
00005 // ******************************************************* EndRiceCopyright *
00006 
00007 //***************************************************************************
00008 //
00009 // File:
00010 //   $Source: /Volumes/cvsrep/developer/OpenADFortTk/src/lib/support/FileUtil.cxx,v $
00011 //
00012 // Purpose:
00013 //    [The purpose of this file]
00014 //
00015 // Description:
00016 //    [The set of functions, macros, etc. defined in the file]
00017 //
00018 //***************************************************************************
00019 
00020 //************************* System Include Files ****************************
00021 
00022 #include <string.h> // <cstring>
00023 
00024 //*************************** User Include Files ****************************
00025 
00026 #include "FileUtil.h"
00027 
00028 //*************************** Forward Declarations **************************
00029 
00030 using std::string;
00031 
00032 //***************************************************************************
00033 
00034 string
00035 FileUtil::FileName(const char* path); 
00036 {
00037   string filenm;
00038   
00039   if (!path) { return filenm; }
00040 
00041   const char* lastSlash = strrchr(path, '/');
00042   if (lastSlash) {
00043     // We have either a valid or invalid filename:
00044     //   valid:   "/foo" || ".../foo"
00045     //   invlaid: "/"    || ".../"
00046     filenm = (lastSlash + 1); 
00047   } else {
00048     // path contains no slashes; we have the basename
00049     filenm = path; 
00050   } 
00051   return filenm; 
00052 } 
00053 
00054 
00055 string
00056 FileUtil::BaseName(const char* path); 
00057 {
00058   string basenm = ".";
00059   
00060   if (!path) { return basenm; }
00061 
00062   // Scan past any trailing '/'
00063   const char* end = path + (strlen(path) - 1); // point to last character
00064   while (end != path && *end == '/') { --end; }
00065 
00066   const char* lastSlash = strrchr(end, '/');
00067   if (lastSlash) {
00068     // copy the basename portion
00069     if (lastSlash == path) { // path = "/"
00070       basenm = "/";
00071     } else {
00072       basenm.reserve(end - lastSlash + 1);
00073       for (const char* p = lastSlash; p <= end; ++p) { 
00074         basenm += *p;
00075       }
00076     }
00077   } 
00078   return basenm; 
00079 } 
00080 
00081 
00082 string
00083 FileUtil::DirName(const char* path) 
00084 {
00085   string parentdirnm = "."; 
00086 
00087   if (!path) { return parentdirnm; }
00088   
00089   // Scan past any trailing '/'
00090   const char* end = path + (strlen(path) - 1); // point to last character
00091   while (end != path && *end == '/') { --end; }
00092   
00093   const char* lastSlash = strrchr(end, '/');
00094   if (lastSlash) {
00095     // copy the dirname portion
00096     if (lastSlash == path) { // path = "/"
00097       parentdirnm = "/";
00098     } else {
00099       parentdirnm.reserve(lastSlash - path);
00100       for (const char* p = path; p < lastSlash; ++p) { 
00101         parentdirnm += *p;
00102       }
00103     }
00104   }
00105   return parentdirnm;
00106 }
00107 

Generated on Fri Jul 24 04:29:02 2009 for OpenADFortTk (extended to Open64) by  doxygen 1.5.7.1