crashanalysercmd/PerfToolsSharedLibraries/Engine/SymBuildParsingLib/Utils/SymFileSystemUtils.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2004-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 using System;
       
    19 using System.IO;
       
    20 
       
    21 namespace SymBuildParsingLib.Utils
       
    22 {
       
    23 	public class SymFileSystemUtils
       
    24 	{
       
    25 		public static bool FileExists( string aFileName )
       
    26 		{
       
    27 			FileInfo info = new FileInfo( aFileName );
       
    28 			return info.Exists;
       
    29 		}
       
    30 
       
    31 		public static bool IsDriveValid( string aDrive )
       
    32 		{
       
    33 			bool valid = false;
       
    34 			//
       
    35 			if	( aDrive.Length > 0 )
       
    36 			{
       
    37 				valid = IsDriveValid( aDrive[ 0 ] );
       
    38 			}
       
    39 			//
       
    40 			return valid;
       
    41 		}
       
    42 
       
    43 		public static bool IsDriveValid( char aDrive )
       
    44 		{
       
    45 			string driveAsLowerCaseString = new string( aDrive, 1 ).ToLower();
       
    46 			int drive = (int) driveAsLowerCaseString[ 0 ];
       
    47 			//
       
    48 			int firstDrive = (int) 'a';
       
    49 			int lastDrive = (int) 'z';
       
    50 			//
       
    51 			bool valid = ( drive >= firstDrive && drive <= lastDrive );
       
    52 			return valid;
       
    53 		}
       
    54 
       
    55 		public static string AddPathSeparator( string aDir )
       
    56 		{
       
    57 			string ret = aDir.Trim();
       
    58 			//
       
    59 			int length = ret.Length;
       
    60 			if	( length > 0 )
       
    61 			{
       
    62 				if	( ret[ length - 1 ] != Path.DirectorySeparatorChar )
       
    63 				{
       
    64 					ret += Path.DirectorySeparatorChar;
       
    65 				}
       
    66 			}
       
    67 			//
       
    68 			return ret;
       
    69 		}
       
    70 
       
    71 		public static string DirectoryFromPath( string aName )
       
    72 		{
       
    73 			// C:\system\data\wibble.txt
       
    74 			// Returns C:\System\Data\
       
    75 			string ret = Path.GetDirectoryName( aName ) + Path.DirectorySeparatorChar;
       
    76 			return ret;
       
    77 		}
       
    78 
       
    79 		public static string DriveFromPath( string aName )
       
    80 		{
       
    81 			// C:\\system\\data\\wibble.txt => C:
       
    82 			string drive = "C:";
       
    83 			//
       
    84 			if	( Path.IsPathRooted( aName ) == true )
       
    85 			{
       
    86 				drive = Path.GetPathRoot( aName );
       
    87 				
       
    88 				// Check its got a drive
       
    89 				if	( drive.Length < 2 || drive[ 1 ] != ':' )
       
    90 				{
       
    91 					throw new ArgumentException( "Path \'" + aName + "\' does not have a drive as its root" );
       
    92 				}
       
    93 
       
    94 				drive = drive.Substring( 0, 2 );
       
    95 			}
       
    96 			else
       
    97 			{
       
    98 				throw new ArgumentException( "Path \'" + aName + "\' does not contain a root" );
       
    99 			}
       
   100 			//
       
   101 			return drive;
       
   102 		}
       
   103 
       
   104 		public static string PopDir( string aDir )
       
   105 		{
       
   106 			string ret = DirectoryFromPath( aDir );
       
   107 			ret = AddPathSeparator( ret );
       
   108 
       
   109 			DirectoryInfo info = new DirectoryInfo( ret );
       
   110 			ret = info.Parent.FullName;
       
   111 			ret = AddPathSeparator( ret );
       
   112 
       
   113 			return ret;
       
   114 		}
       
   115 
       
   116 		public static string MergePaths( string aBasePath, string aOtherPath )
       
   117 		{
       
   118 			#region Examples
       
   119 			// "X:\Wibble\"                 +   "+Build\Generated"      => "X:\<epocroot>\EPOC32\Build\Generated";
       
   120 			// "X:\Wibble\"                 +   "..\something\"         => "X:\something\"
       
   121 			// "X:\Wibble\"                 +   "..something\"          => "X:\Wibble\something"
       
   122 			// "X:\Wibble\"                 +   "\something\"           => "X:\something\"
       
   123 			// "X:\Wibble\"                 +   ".\something\"          => "X:\wibble\something\"
       
   124 			// "X:\Wibble\"                 +   "something\"            => "X:\wibble\something\"
       
   125 			// "X:\Wibble\"                 +   "."                     => "X:\Wibble\"
       
   126 			// "X:\Wibble\"                 +   "\"                     => "X:\Wibble\"
       
   127 			// "X:\Wibble\"                 +   "a"                     => "X:\Wibble\a"
       
   128 			// "X:\Wibble\Whatsit.txt"      +   "X:\Wibble\"            => "X:\Wibble\"
       
   129 			// "X:\Wibble\Whatsit.txt"      +   "NextWibble.txt"        => "X:\Wibble\NextWibble.txt"
       
   130 			#endregion
       
   131 			string otherPath = aOtherPath;
       
   132 			//
       
   133 			string ret = aBasePath.Trim();
       
   134 			ret = DirectoryFromPath(ret);
       
   135 			ret = AddPathSeparator(ret);
       
   136 			//
       
   137 			int length = otherPath.Length;
       
   138 			if	(length >= 1)
       
   139 			{
       
   140 				char firstChar = otherPath[0];
       
   141 				if  ( firstChar == '.' )
       
   142 				{
       
   143 					bool needToPushRemainder = false;
       
   144 					bool finishedCheckingForDirPops = false;
       
   145 					//
       
   146 					while( finishedCheckingForDirPops == false )
       
   147 					{
       
   148 						// Assume we only go around once
       
   149 						finishedCheckingForDirPops = true;
       
   150 						firstChar = otherPath[0];
       
   151 						length = otherPath.Length;
       
   152 						//
       
   153 						if	( firstChar == '.' )
       
   154 						{
       
   155 							if	( length >= 2 )
       
   156 							{
       
   157 								char secondChar = otherPath[1];
       
   158 								if	( secondChar == '.' )
       
   159 								{
       
   160 									// Keep popping directories from the base path (now stored in 'ret')
       
   161 									// while there are "..\" sub-strings remaining at the start of the 
       
   162 									// "other path"
       
   163 									ret = PopDir(ret);
       
   164 
       
   165 									// Tidy up the "otherPath" to remove the two dots which we just parsed
       
   166 									int subStringPos = 2;
       
   167 									if	(length >= 3 && otherPath[2] == '\\')
       
   168 									{
       
   169 										subStringPos = 3;
       
   170 									}
       
   171 									//
       
   172 									needToPushRemainder = true;
       
   173 									otherPath = otherPath.Substring( subStringPos );
       
   174 									finishedCheckingForDirPops = false;
       
   175 								}
       
   176 							}
       
   177 						}
       
   178 					}
       
   179 
       
   180 					if  ( needToPushRemainder )
       
   181 					{
       
   182 						ret += otherPath;
       
   183 					}
       
   184 				}
       
   185 				else if ( firstChar == '\\' )
       
   186 				{
       
   187 					if	(length > 1)
       
   188 					{
       
   189 						// Root path
       
   190 						ret = DriveFromPath( ret );
       
   191 						ret += otherPath;
       
   192 					}
       
   193 				}
       
   194 				else if ( firstChar == '+' && otherPath.Length > 0 )
       
   195 				{
       
   196 					// Need to add epoc root and then EPOC32, then the path without the +
       
   197 					string actualPathPart = otherPath.Substring(1);
       
   198 					if	( actualPathPart.Length > 0 && actualPathPart[0] == '\\' )
       
   199 					{
       
   200 						actualPathPart = actualPathPart.Substring(1);
       
   201 					}
       
   202 
       
   203 					string epocRootValue = SymEnvironment.EpocRoot;
       
   204 					ret = DriveFromPath(ret);
       
   205 					ret += AddPathSeparator( epocRootValue );
       
   206 					ret += AddPathSeparator( SymEnvironment.Epoc32Path );
       
   207 					ret += AddPathSeparator( actualPathPart );
       
   208 				}
       
   209 				else
       
   210 				{
       
   211 					if  ( otherPath[ length-1 ] == '\\' )
       
   212 					{
       
   213 						// Assume we already have a complete path
       
   214 						ret = otherPath;
       
   215 					}
       
   216 					else
       
   217 					{
       
   218 						ret += otherPath;
       
   219 					}
       
   220 				}
       
   221 			}
       
   222 
       
   223 			/*
       
   224 			const string nameAndExt = FileNameAndExtension(ret);
       
   225 			const bool containsDot = (nameAndExt.find_last_of('.') != string::npos);
       
   226 			if  (!FileExists(ret) && ret.length() && ret[ret.length()-1] != '\\' && containsDot)
       
   227 				{
       
   228 				cout << "WARNING - possible file merge error:" << endl;
       
   229 				cout << "\tMerging: " << aBasePath << " with: " << aOtherPath << endl;
       
   230 				cout << "\t\t -> " << ret << endl << endl;
       
   231 				}
       
   232 			*/
       
   233 			return ret;
       
   234 		}
       
   235 	}
       
   236 }