UTPath.h

Go to the documentation of this file.
00001 //==================================================================================================
00002 // Copyright (C) 2010  Brian Tietz    sdbtietz at yahoo dot com
00003 //
00004 // This program is free software; you can redistribute it and/or modify it under the terms of the
00005 // GNU General Public License as published by the Free Software Foundation, version 2.0 of the
00006 // License.
00007 //
00008 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
00009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00010 // General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU General Public License along with this program; if
00013 // not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00014 // 02110-1301, USA.
00015 //
00016 // For commercial software, the copyright holder (Brian Tietz, email sdbtietz at yahoo dot com)
00017 // reserves the right and is willing to waive the proprietary source code disclosure aspects of that
00018 // license as applied to the UT library in exchange for either substantial contributions to the
00019 // development of the UT library or other forms of compensation.  Any such waiver must be
00020 // established in writing between the copyright holder and the commercial entity obtaining such a
00021 // waiver.
00022 //==================================================================================================
00023 
00024 
00025 #ifndef _UT_PATH_H_
00026 #define _UT_PATH_H_
00027 
00028 
00029 //==================================================================================================
00030 //=== System headers
00031 //==================================================================================================
00032 #include <limits.h>
00033 #include <fcntl.h>
00034 
00035 
00036 //==================================================================================================
00037 //=== Project headers
00038 //==================================================================================================
00039 #include "UTString.h"
00040 
00041 
00042 //==================================================================================================
00043 //=== Forward name declarations
00044 //==================================================================================================
00045 class PathNode_t;
00046 
00047 
00048 //==================================================================================================
00049 class UT_EXPORT Path_t
00050 //==================================================================================================
00051 : public String_t
00060 {
00061     //----------------------------------------------------------------------------------------------
00062     public:
00063     //----------------------------------------------------------------------------------------------
00064     inline                      Path_t();
00066 
00067     inline                      Path_t( byte* buffer, int buffer_size );
00077 
00078     inline                      Path_t(stringliteral* path);
00090 
00091     inline                      Path_t(const utf8* path);
00096 
00097     inline                      Path_t(const Path_t& path);
00100 
00101     inline                      Path_t(const String_t& path);
00106 
00107     // Checked set functions
00108     inline Status_t             Set(stringliteral* path);
00113 
00114     inline Status_t             Set(const utf8* path);
00119 
00120     inline Status_t             Set(const Path_t& path);
00125 
00126     inline Status_t             Set(const String_t& path);
00131 
00132     // Unchecked set operator s
00133     inline const Path_t&        operator =(stringliteral* path);
00138 
00139     inline const Path_t&        operator =(const utf8* path);
00144 
00145     inline const Path_t&        operator =(const Path_t& path);
00150 
00151     inline const Path_t&        operator =(const String_t& path);
00156 
00157     bool                        IsPathValid() const;
00161 
00162     // Checked append functions
00163     inline Status_t             Append(stringliteral* leaf);
00170 
00171     Status_t                    Append(const utf8* leaf);
00178 
00179     inline Status_t             Append(const Path_t& leaf);
00186 
00187     inline Status_t             Append(const String_t& leaf);
00194 
00195     inline Status_t             AppendToLeaf(stringliteral* add_to_leaf);
00200 
00201     inline Status_t             AppendToLeaf(const utf8* add_to_leaf);
00206 
00207     inline Status_t             AppendToLeaf(const String_t& add_to_leaf);
00212 
00213     // Unchecked append operator s
00214     inline const Path_t&        operator +=(stringliteral* path);
00221 
00222     inline const Path_t&        operator +=(const utf8* path);
00229 
00230     inline const Path_t&        operator +=(const Path_t& leaf);
00237 
00238     inline const Path_t&        operator +=(const String_t& leaf);
00245 
00246     inline Path_t               operator +(stringliteral* append) const;
00257 
00258     inline Path_t               operator +(const utf8* append) const;
00269 
00270     inline Path_t               operator +(const Path_t& append) const;
00281 
00282     inline Path_t               operator +(const String_t& append) const;
00293 
00294     Status_t                    GetLeafName(out Path_t* leaf) const;
00301 
00302     Status_t                    ToRelative( const Path_t& relative_to, bool canonical = true );
00307 
00308     Status_t                    ToParent();
00314 
00315     inline Status_t             GetParent(out Path_t* parent) const;
00322 
00323     Status_t                    IsDirectory() const;
00331 
00332     Status_t                    GetDirectory(out Path_t* dir) const;
00336 
00337     Status_t                    IsAbsolute() const;
00344 
00345     Status_t                    MakeAbsolute();
00350 
00351     Status_t                    IsRootDirectory() const;
00357 
00358     Status_t                    IsRegularFile() const;
00366 
00367     Status_t                    RegularFileSize(out fileoff_t* size) const;
00372 
00373     Status_t                    IsSymLink() const;
00380 
00381     Status_t                    MakeDir(    bool recursive = true,
00382                                             mode_t permissions = S_IRWUSR | S_IXUSR) const;
00407 
00408     Status_t                    Delete(bool recursive = false) const;
00414 
00415     static Status_t             GetAppPath(out Path_t* app_path);
00418 
00419     static Status_t             GetAppDir(out Path_t* app_dir);
00423 
00424     static Status_t             GetCurrentWorkingDir(out Path_t* cwd);
00427 
00428     static Status_t             SetCurrentWorkingDir(const Path_t& cwd);
00431 
00432     static Status_t             GetHomeDir(out Path_t* home);
00434 
00435     inline static bool          IsDelimiter(utf8 c);
00443 
00444     inline Status_t             StripCurrAndParentDirs();
00447 
00448     //----------------------------------------------------------------------------------------------
00449     private:
00450     //----------------------------------------------------------------------------------------------
00451     static Status_t             BadPathError( stringliteral* format, const utf8* path_name );
00452                                     // Creates a bad path error status with explanatory text
00453                                     // attached to the returned Status_t.  The explanatory text will
00454                                     // be constructed using String_t::Printf( format, path_name ) so
00455                                     // format should be a printf-style format string with %s where
00456                                     // the path_name should be inserted.
00457 
00458     static Status_t             BadPathError(const utf8* path_name);
00459                                     // Creates a bad path error status with explanatory text
00460                                     // attached to the returned Status_t to indicate the path and
00461                                     // that it was invalid.
00462 
00463     static Status_t             PathNotFoundError( stringliteral* format, const utf8* path_name );
00464                                     // Creates a path not found error status with explanatory text
00465                                     // attached to the returned Status_t.  The explanatory text will
00466                                     // be constructed using String_t::Printf( format, path_name ) so
00467                                     // format should be a printf-style format string with %s where
00468                                     // the path_name should be inserted.
00469 
00470     static Status_t             PathNotFoundError(const utf8* explanation);
00471                                     // Creates a path not found error status with explanatory text
00472                                     // attached to the returned Status_t to indicate the path and
00473                                     // that it was not found.
00474 
00475     static bool                 InitAndCheckAllNodes( out PathNode_t* node, const utf8* path );
00476                                     // Caller provides an uninitialized PathNode_t.  If
00477                                     // CheckAllNodes returns true, all nodes are good and the node
00478                                     // parameter will reference the last node in the path.
00479 
00480     bool                        StripCurrAndParentDirsInternal(bool is_absolute);
00481                                     // Strips current (".") and parent ("Parent/..") elements from
00482                                     // the path.  Returns true if successful.
00483 };
00484 
00485 
00486 //==================================================================================================
00487 class UT_EXPORT PathB_t
00488 //==================================================================================================
00489 : public Path_t
00497 {
00498     //----------------------------------------------------------------------------------------------
00499     public:
00500     //----------------------------------------------------------------------------------------------
00501     inline                      PathB_t();
00503 
00504     inline                      PathB_t(stringliteral* path);
00509 
00510     inline                      PathB_t(const utf8* path);
00515 
00516     inline                      PathB_t(const Path_t& path);
00519 
00520     inline                      PathB_t(const String_t& path);
00525 
00526     inline const Path_t&        operator =(stringliteral* path);
00531 
00532     inline const Path_t&        operator =(const utf8* path);
00537 
00538     inline const Path_t&        operator =(const Path_t& path);
00543 
00544     inline const Path_t&        operator =(const String_t& path);
00549 
00550     //----------------------------------------------------------------------------------------------
00551     private:
00552     //----------------------------------------------------------------------------------------------
00553     byte                        m_buffer[PATH_MAX];
00554 };
00555 
00556 
00557 
00558 
00559 //==================================================================================================
00560 //==================================================================================================
00561 //===
00562 //=== Inline function implementations
00563 //===
00564 //==================================================================================================
00565 //==================================================================================================
00566 inline
00567 Path_t::Path_t()
00568 { }
00569 
00570 
00571 inline
00572 Path_t::Path_t( byte* buffer, int buffer_size )
00573 : String_t( buffer, buffer_size )
00574 { }
00575 
00576 
00577 inline
00578 Path_t::Path_t(stringliteral* path)
00579 : String_t(path)
00580 { }
00581 
00582 
00583 inline
00584 Path_t::Path_t(const utf8* path)
00585 : String_t(path)
00586 { }
00587 
00588 
00589 inline
00590 Path_t::Path_t(const Path_t& path)
00591 : String_t(path)
00592 { }
00593 
00594 
00595 inline
00596 Path_t::Path_t(const String_t& path)
00597 : String_t(path)
00598 { }
00599 
00600 
00601 inline Status_t
00602 Path_t::Set(stringliteral* path)
00603 {
00604     String_t::Set(path);
00605     if( IsPathValid() )
00606         return true;
00607     return BadPathError( reinterpret_cast<const utf8*>(path) );
00608 }
00609 
00610 
00611 inline Status_t
00612 Path_t::Set(const utf8* path)
00613 {
00614     String_t::Set(path);
00615     if( IsPathValid() )
00616         return true;
00617     return BadPathError(path);
00618 }
00619 
00620 
00621 inline Status_t
00622 Path_t::Set(const Path_t& path)
00623 {
00624     String_t::Set(path);
00625     if( IsPathValid() )
00626         return true;
00627     return BadPathError(path);
00628 }
00629 
00630 
00631 inline Status_t
00632 Path_t::Set(const String_t& path)
00633 {
00634     String_t::Set(path);
00635     if( IsPathValid() )
00636         return true;
00637     return BadPathError(path);
00638 }
00639 
00640 
00641 inline const Path_t&
00642 Path_t::operator =(stringliteral* path)
00643 {
00644     String_t::Set(path);
00645     return *this;
00646 }
00647 
00648 
00649 inline const Path_t&
00650 Path_t::operator =(const utf8* path)
00651 {
00652     String_t::Set(path);
00653     return *this;
00654 }
00655 
00656 
00657 inline const Path_t&
00658 Path_t::operator =(const Path_t& path)
00659 {
00660     String_t::Set(path);
00661     return *this;
00662 }
00663 
00664 
00665 inline const Path_t&
00666 Path_t::operator =(const String_t& path)
00667 {
00668     String_t::Set(path);
00669     return *this;
00670 }
00671 
00672 
00673 inline Status_t
00674 Path_t::Append(stringliteral* leaf)
00675 {
00676     return Append( reinterpret_cast<const utf8*>(leaf) );
00677 }
00678 
00679 
00680 inline Status_t
00681 Path_t::Append(const Path_t& leaf)
00682 {
00683     return Append( leaf.UTF8() );
00684 }
00685 
00686 
00687 inline Status_t
00688 Path_t::Append(const String_t& leaf)
00689 {
00690     return Append( leaf.UTF8() );
00691 }
00692 
00693 
00694 inline Status_t
00695 Path_t::AppendToLeaf(stringliteral* add_to_leaf)
00696 {
00697     error_code_t error = String_t::Append(add_to_leaf).StatusCode();
00698     if(error < 0)
00699         return error;
00700     if( IsPathValid() )
00701         return true;
00702     return BadPathError( UTF8() );  
00703 }
00704 
00705 
00706 inline Status_t
00707 Path_t::AppendToLeaf(const utf8* add_to_leaf)
00708 {
00709     error_code_t error = String_t::Append(add_to_leaf).StatusCode();
00710     if(error < 0)
00711         return error;
00712     if( IsPathValid() )
00713         return true;
00714     return BadPathError( UTF8() );  
00715 }
00716 
00717 
00718 inline Status_t
00719 Path_t::AppendToLeaf(const String_t& add_to_leaf)
00720 {
00721     String_t::Append(add_to_leaf);
00722     if( IsPathValid() )
00723         return true;
00724     return BadPathError( UTF8() );  
00725 }
00726 
00727 
00728 inline const Path_t&
00729 Path_t::operator +=(stringliteral* leaf)
00730 {
00731     Append(leaf);
00732     return *this;
00733 }
00734 
00735 
00736 inline const Path_t&
00737 Path_t::operator +=(const utf8* leaf)
00738 {
00739     Append(leaf);
00740     return *this;
00741 }
00742 
00743 
00744 inline const Path_t&
00745 Path_t::operator +=(const Path_t& leaf)
00746 {
00747     Append(leaf);
00748     return *this;
00749 }
00750 
00751 
00752 inline const Path_t&
00753 Path_t::operator +=(const String_t& leaf)
00754 {
00755     Append(leaf);
00756     return *this;
00757 }
00758 
00759 
00760 inline Path_t
00761 Path_t::operator +(stringliteral* leaf) const
00762 {
00763     Path_t local_copy(*this);
00764     local_copy.Append(leaf);
00765     return local_copy;
00766 }
00767 
00768 
00769 inline Path_t
00770 Path_t::operator +(const utf8* leaf) const
00771 {
00772     Path_t local_copy(*this);
00773     local_copy.Append(leaf);
00774     return local_copy;
00775 }
00776 
00777 
00778 inline Path_t
00779 Path_t::operator +(const Path_t& leaf) const
00780 {
00781     Path_t local_copy(*this);
00782     local_copy.Append(leaf);
00783     return local_copy;
00784 }
00785 
00786 
00787 inline Path_t
00788 Path_t::operator +(const String_t& leaf) const
00789 {
00790     Path_t local_copy(*this);
00791     local_copy.Append(leaf);
00792     return local_copy;
00793 }
00794 
00795 
00796 inline Status_t
00797 Path_t::GetParent(out Path_t* parent) const
00798 {
00799     static_cast<String_t*>(parent)->Set(*this);
00800     return parent->ToParent();
00801 }
00802 
00803 
00804 inline bool
00805 Path_t::IsDelimiter(utf8 c)
00806 {
00807     if(c == '/')
00808         return true;
00809     #if NONSTANDARD_PATH_DELIMITER
00810         if(c == NONSTANDARD_PATH_DELIMITER)
00811             return true;
00812     #endif
00813     return false;
00814 }
00815 
00816 
00817 inline Status_t
00818 Path_t::StripCurrAndParentDirs()
00819 {
00820     if( !StripCurrAndParentDirsInternal(false) )
00821         return BadPathError( UTF8() );
00822     return true;
00823 }
00824 
00825 
00826 inline
00827 PathB_t::PathB_t()
00828 : Path_t( m_buffer, sizeof(m_buffer) )
00829 { }
00830 
00831 
00832 inline
00833 PathB_t::PathB_t(stringliteral* path)
00834 : Path_t( m_buffer, sizeof(m_buffer) )
00835 {
00836     Set(path);
00837 }
00838 
00839 
00840 inline
00841 PathB_t::PathB_t(const utf8* path)
00842 : Path_t( m_buffer, sizeof(m_buffer) )
00843 {
00844     Set(path);
00845 }
00846 
00847 
00848 inline
00849 PathB_t::PathB_t(const Path_t& path)
00850 : Path_t( m_buffer, sizeof(m_buffer) )
00851 {
00852     Set(path);
00853 }
00854 
00855 
00856 inline
00857 PathB_t::PathB_t(const String_t& path)
00858 : Path_t( m_buffer, sizeof(m_buffer) )
00859 {
00860     Set(path);
00861 }
00862 
00863 
00864 inline const Path_t&
00865 PathB_t::operator =(stringliteral* path)
00866 {
00867     String_t::Set(path);
00868     return *this;
00869 }
00870 
00871 
00872 inline const Path_t&
00873 PathB_t::operator =(const utf8* path)
00874 {
00875     String_t::Set(path);
00876     return *this;
00877 }
00878 
00879 
00880 inline const Path_t&
00881 PathB_t::operator =(const Path_t& path)
00882 {
00883     String_t::Set(path);
00884     return *this;
00885 }
00886 
00887 
00888 inline const Path_t&
00889 PathB_t::operator =(const String_t& path)
00890 {
00891     String_t::Set(path);
00892     return *this;
00893 }
00894 
00895 
00896 #endif // _UT_PATH_H_

Generated on Tue Dec 14 22:35:05 2010 for UT library by  doxygen 1.6.1