Ignore:
Timestamp:
6/18/2010 6:53:20 AM (2 years ago)
Author:
lowjoel
Message:

Refactor a function which parses null-delimited string arrays.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/eraser/Eraser.Util/VolumeInfo.cs

    r2178 r2179  
    9292        private List<string> GetLocalVolumeMountPoints() 
    9393        { 
    94             List<string> result = new List<string>(); 
    95  
    96             //Get the paths of the said volume 
    97             string pathNames; 
    98             { 
    99                 uint returnLength = 0; 
    100                 char[] pathNamesBuffer = new char[NativeMethods.MaxPath]; 
    101                 while (!NativeMethods.GetVolumePathNamesForVolumeName(VolumeId, 
    102                     pathNamesBuffer, (uint)pathNamesBuffer.Length, out returnLength)) 
    103                 { 
    104                     int errorCode = Marshal.GetLastWin32Error(); 
    105                     switch (errorCode) 
    106                     { 
    107                         case Win32ErrorCode.NotReady: 
    108                             //The drive isn't ready yet: just return an empty list. 
    109                             return result; 
    110                         case Win32ErrorCode.MoreData: 
    111                             pathNamesBuffer = new char[pathNamesBuffer.Length * 2]; 
    112                             break; 
    113                         default: 
    114                             throw Win32ErrorCode.GetExceptionForWin32Error(errorCode); 
    115                     } 
    116                 } 
    117  
    118                 pathNames = new string(pathNamesBuffer, 0, (int)returnLength); 
    119             } 
    120  
    121             //OK, the marshalling is complete. Convert the pathNames string into a list 
    122             //of strings containing all of the volumes mountpoints; because the 
    123             //GetVolumePathNamesForVolumeName function returns a convoluted structure 
    124             //containing the path names. 
    125             for (int lastIndex = 0, i = 0; i != pathNames.Length; ++i) 
    126             { 
    127                 if (pathNames[i] == '\0') 
    128                 { 
    129                     //If there are no mount points for this volume, the string will only 
    130                     //have one NULL 
    131                     if (i - lastIndex == 0) 
    132                         break; 
    133  
    134                     result.Add(pathNames.Substring(lastIndex, i - lastIndex)); 
    135  
    136                     lastIndex = i + 1; 
    137                     if (pathNames[lastIndex] == '\0') 
    138                         break; 
    139                 } 
    140             } 
    141  
    142             return result; 
     94            return new List<string>(NativeMethods.GetVolumePathNamesForVolumeName(VolumeId)); 
    14395        } 
    14496 
Note: See TracChangeset for help on using the changeset viewer.