Back to index

nux  3.0.0
FileName.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "NuxCore.h"
00024 #include "FileName.h"
00025 #include "Math/MathUtility.h"
00026 
00027 namespace nux
00028 {
00029 
00030   NString NFileName::GetDrive() const
00031   {
00032     size_t Pos = FindFirstOccurence (NUX_BACKSLASH_CHAR);
00033 
00034     if (Pos == tstring::npos)
00035     {
00036       Pos = FindFirstOccurence ("\\");
00037 
00038       if (Pos != tstring::npos)
00039       {
00040         return GetSubString (Pos);
00041       }
00042     }
00043 
00044     return TEXT ("");
00045   }
00046 
00047   NString NFileName::GetExtension() const
00048   {
00049     size_t Pos = FindLastOccurence (TEXT (".") );
00050 
00051     if (Pos != tstring::npos)
00052     {
00053       return GetSubString (Pos + 1, Length() - Pos - 1);
00054     }
00055 
00056     return TEXT ("");
00057   }
00058 
00059 // Returns the base filename without the path
00060   NString NFileName::GetCleanFilename() const
00061   {
00062     size_t Pos = FindLastOccurence (NUX_BACKSLASH_CHAR);
00063     Pos = Max<size_t> (Pos, FindLastOccurence (TEXT ("/") ) ); // in case we are using slash and the NUX_BACKSLASH_CHAR is different.
00064     Pos = Max<size_t> (Pos, FindLastOccurence (TEXT ("\\") ) ); // in case we are using backslash and the NUX_BACKSLASH_CHAR is different.
00065 
00066     if (Pos != tstring::npos)
00067     {
00068       return GetSubString (Pos + 1, Length() - Pos - 1);
00069     }
00070 
00071     return *this;
00072   }
00073 
00074   NString NFileName::GetFilenameNoExtension() const
00075   {
00076     size_t Pos = FindLastOccurence (TEXT (".") );
00077 
00078     if (Pos != tstring::npos)
00079     {
00080       return GetSubString (Pos);
00081     }
00082 
00083     return *this;
00084   }
00085 
00086 // Returns the base filename without the path and without the extension
00087   NString NFileName::GetBaseFilename() const
00088   {
00089     NString Wk = GetCleanFilename();
00090 
00091     size_t Pos = Wk.FindLastOccurence (TEXT (".") );
00092 
00093     if (Pos != tstring::npos)
00094     {
00095       return Wk.GetSubString (Pos);
00096     }
00097 
00098     return Wk;
00099   }
00100 
00101 // Returns the path in front of the filename
00102 
00103   NString NFileName::GetDirectoryPath() const
00104   {
00105     size_t Pos = FindLastOccurence (NUX_BACKSLASH_CHAR);
00106     Pos = Max<size_t> (Pos, FindLastOccurence (TEXT ("/") ) ); // in case we are using slash and the NUX_BACKSLASH_CHAR is different.
00107     Pos = Max<size_t> (Pos, FindLastOccurence (TEXT ("\\") ) ); // in case we are using backslash and the NUX_BACKSLASH_CHAR is different.
00108 
00109     if (Pos != tstring::npos)
00110     {
00111       return GetSubString (Pos);
00112     }
00113 
00114     return *this;
00115   }
00116 
00117   void NFileName::ChangeFileExtension (const TCHAR *ext)
00118   {
00119     size_t Pos = FindLastOccurence (TEXT (".") );
00120 
00121     if (Pos != tstring::npos)
00122     {
00123       (*this) = GetSubString (Pos) + NString (TEXT (".") ) + NString (ext);
00124     }
00125     else
00126     {
00127       (*this) = (*this) + NString (TEXT (".") ) + NString (ext);
00128     }
00129   }
00130 
00131   void NFileName::ConvertSlashToBackslash()
00132   {
00133     size_t Pos = tstring::npos;
00134     Pos = FindFirstOccurence (TEXT ('/') );
00135 
00136     while (Pos != tstring::npos)
00137     {
00138       Replace (Pos, 1, 1, TEXT ('\\') );
00139     }
00140   }
00141 
00142 
00143   void NFileName::ConvertBackslashToSlash()
00144   {
00145     size_t Pos = tstring::npos;
00146     Pos = FindFirstOccurence (TEXT ('\\') );
00147 
00148     while (Pos != tstring::npos)
00149     {
00150       Replace (Pos, 1, 1, TEXT ('/') );
00151     }
00152   }
00153 
00154   void NFileName::AddSlashAtEnd()
00155   {
00156     if (GetLastChar() != TEXT ('/') )
00157       *this += TEXT ('/');
00158   }
00159 
00160   void NFileName::AddBackSlashAtEnd()
00161   {
00162     if (GetLastChar() != TEXT ('\\') )
00163       *this += TEXT ('\\');
00164   }
00165 
00166   void NFileName::AddSlashAtStart()
00167   {
00168     if (GetFirstChar() != TEXT ('/') )
00169       Insert (0, 1, TEXT ('/') );
00170   }
00171 
00172   void NFileName::AddBackSlashAtStart()
00173   {
00174     if (GetFirstChar() != TEXT ('\\') )
00175       Insert (0, 1, TEXT ('\\') );
00176   }
00177 
00178   void NFileName::RemoveSlashAtEnd()
00179   {
00180     RemoveSuffix (TEXT ('/') );
00181   }
00182 
00183   void NFileName::RemoveBackSlashAtEnd()
00184   {
00185     RemoveSuffix (TEXT ('\\') );
00186   }
00187 
00188   void NFileName::RemoveSlashAtStart()
00189   {
00190     RemovePrefix (TEXT ('/') );
00191   }
00192 
00193   void NFileName::RemoveBackSlashAtStart()
00194   {
00195     RemovePrefix (TEXT ('\\') );
00196   }
00197 
00198   void NFileName::ConvertToCleanSlash()
00199   {
00200     ConvertBackslashToSlash();
00201 
00202     size_t size = Size();
00203 
00204     for (size_t i = 0; i < size; )
00205     {
00206       if ( (i < size - 1) && (operator[] (i) == TEXT ('/') ) && (operator[] (i + 1) == TEXT ('/') ) )
00207       {
00208         Erase (i + 1, 1);
00209         --size;
00210       }
00211       else
00212       {
00213         i++;
00214       }
00215     }
00216   }
00217 
00218   void NFileName::ConvertToCleanBackslash()
00219   {
00220     ConvertSlashToBackslash();
00221 
00222     size_t size = Size();
00223 
00224     for (size_t i = 0; i < size; )
00225     {
00226       if ( (i < size - 1) && (operator[] (i) == TEXT ('\\') ) && (operator[] (i + 1) == TEXT ('\\') ) )
00227       {
00228         Erase (i + 1, 1);
00229       }
00230       else
00231       {
00232         i++;
00233       }
00234     }
00235   }
00236 
00237 }