Advertisement
Ember

normalize path

Oct 28th, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. String NormalizePath(const String& path)
  2. {
  3.     Int first, last, length, i;
  4.     length = path.length();
  5.  
  6.     // Locate the first directory separator
  7.     for(i = 0; i < length; ++i)
  8.     {
  9.         Char letter = path[i];
  10.  
  11.         if((letter == '/') || (letter == '\\'))
  12.         {
  13.             first = last = i;
  14.             break;
  15.         }
  16.     }
  17.  
  18.     // Locate the last directory separator
  19.     for(++i; i < length; ++i)
  20.     {
  21.         Char letter = path[i];
  22.  
  23.         if((letter == '/') || (letter == '\\'))
  24.         {
  25.             last = i;
  26.             break;
  27.         }
  28.     }
  29.  
  30.     // Exit if the path is already normalized
  31.     if(first == last) { return path; }
  32.  
  33.     Int filenameLength = length - last;
  34.     Char normalized[MAX_PATH];
  35.     ZeroMemory(normalized, MAX_PATH);
  36.     // Copy the characters into the normalized string
  37.     // Front
  38.     for(i = 0; i < first; ++i)
  39.     {
  40.         normalized[i] = path[i];
  41.     }
  42.     // Back
  43.     for(i = 0; i < filenameLength; ++i)
  44.     {
  45.         normalized[first + i] = path[last + i];
  46.     }
  47.  
  48.     return normalized;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement