Changeset 1242


Ignore:
Timestamp:
09/30/09 10:10:05 (5 years ago)
Author:
lowjoel
Message:

Use cli::array<Byte> instead of std::vector<char> as a buffer type for I/O operations.

Location:
trunk/eraser6/Eraser.Util.FileSystem
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/eraser6/Eraser.Util.FileSystem/FatApi.cpp

    r1239 r1242  
    2121 
    2222#include <stdafx.h> 
     23#include <vector> 
    2324#include <windows.h> 
    2425 
     
    5051    } 
    5152 
    52     FatApi::FatApi(VolumeInfo^ info, Stream^ stream) 
     53    FatApi::FatApi(VolumeInfo^, Stream^ stream) 
    5354    { 
    5455        BootSector = new FatBootSector(); 
     
    9697    } 
    9798 
    98     std::vector<char> FatApi::GetFileContents(unsigned cluster) 
     99    array<Byte>^ FatApi::GetFileContents(unsigned cluster) 
    99100    { 
    100101        if (!IsClusterAllocated(cluster)) 
    101102            throw gcnew ArgumentException(L"The specified cluster is not used."); 
    102103 
    103         std::vector<char> result; 
    104         result.reserve(FileSize(cluster)); 
    105         array<Byte>^ buffer = gcnew array<Byte>(ClusterSizeToSize(1)); 
     104        array<Byte>^ result = gcnew array<Byte>(FileSize(cluster)); 
     105        const int clusterSize = ClusterSizeToSize(1); 
     106        int nextIndex = 0; 
    106107 
    107108        do 
    108109        { 
    109110            VolumeStream->Seek(ClusterToOffset(cluster), SeekOrigin::Begin); 
    110             VolumeStream->Read(buffer, 0, buffer->Length); 
    111  
    112             result.insert(result.end(), buffer->Length, 0); 
    113             Marshal::Copy(buffer, 0, static_cast<IntPtr>(&result.back() - buffer->Length + 1), 
    114                 buffer->Length); 
     111            VolumeStream->Read(result, nextIndex, clusterSize); 
     112            nextIndex += clusterSize; 
    115113        } 
    116114        while ((cluster = GetNextCluster(cluster)) != 0xFFFFFFFF); 
     
    119117    } 
    120118 
    121     void FatApi::SetFileContents(const void* data, size_t length, unsigned cluster) 
     119    void FatApi::SetFileContents(array<Byte>^ buffer, unsigned cluster) 
    122120    { 
    123121        if (!IsClusterAllocated(cluster)) 
    124122            throw gcnew ArgumentException(L"The specified cluster is not used."); 
    125         if (length != FileSize(cluster)) 
     123        if (static_cast<unsigned>(buffer->Length) != FileSize(cluster)) 
    126124            throw gcnew ArgumentException(L"The provided file contents will not fit in the " + 
    127125                gcnew String(L"allocated file.")); 
    128126 
    129127        size_t clusterSize = ClusterSizeToSize(1); 
    130         array<Byte>^ buffer = gcnew array<Byte>(clusterSize); 
    131         for (size_t i = 0; i < length; i += clusterSize) 
    132         { 
    133             Marshal::Copy(static_cast<IntPtr>(reinterpret_cast<intptr_t>(static_cast<const char*>(data) + i)), 
    134                 buffer, 0, clusterSize); 
     128        for (int i = 0; i < buffer->Length; i += clusterSize) 
     129        { 
    135130            VolumeStream->Seek(ClusterToOffset(cluster), SeekOrigin::Begin); 
    136             VolumeStream->Write(buffer, 0, clusterSize); 
     131            VolumeStream->Write(buffer, i, clusterSize); 
    137132            cluster = GetNextCluster(cluster); 
    138133        } 
     
    351346    void FatDirectory::ReadDirectory() 
    352347    { 
    353         std::vector<char> dir = Api->GetFileContents(Cluster); 
    354         DirectorySize = dir.size() / sizeof(::FatDirectoryEntry); 
     348        array<Byte>^ dir = Api->GetFileContents(Cluster); 
     349        DirectorySize = dir->Length / sizeof(::FatDirectoryEntry); 
    355350        Directory = new ::FatDirectoryEntry[DirectorySize]; 
    356         memcpy(Directory, &dir.front(), dir.size()); 
     351        Marshal::Copy(dir, 0, static_cast<IntPtr>(Directory), dir->Length); 
    357352 
    358353        ParseDirectory(); 
     
    361356    void FatDirectory::WriteDirectory() 
    362357    { 
    363         Api->SetFileContents(Directory, Api->FileSize(Cluster), Cluster); 
     358        array<Byte>^ buffer = gcnew array<Byte>(DirectorySize * sizeof(::FatDirectoryEntry)); 
     359        Marshal::Copy(static_cast<IntPtr>(Directory), buffer, 0, buffer->Length); 
     360        Api->SetFileContents(buffer, Cluster); 
    364361    } 
    365362} 
  • trunk/eraser6/Eraser.Util.FileSystem/FatApi.h

    r1239 r1242  
    2222#pragma once 
    2323 
    24 #include <vector> 
    2524#include "Fat.h" 
    2625 
     
    9594 
    9695        /// Gets the contents of the file starting at the given cluster. 
    97         std::vector<char> GetFileContents(unsigned cluster); 
     96        array<Byte>^ GetFileContents(unsigned cluster); 
    9897 
    9998        /// Set the contents of the file starting at the given cluster. The length 
     
    101100        ///  
    102101        /// \param[in] buffer  The data to write. 
    103         /// \param[in] length  The amount of data to write. 
    104102        /// \param[in] cluster The cluster to begin writing to. 
    105         void SetFileContents(const void* buffer, size_t length, unsigned cluster); 
     103        void SetFileContents(array<Byte>^ buffer, unsigned cluster); 
    106104 
    107105        /// Resolves a directory to the position on-disk 
  • trunk/eraser6/Eraser.Util.FileSystem/Stdafx.h

    r1226 r1242  
    2121 
    2222#pragma once 
     23#include <vector> 
     24 
    2325#include <windows.h> 
Note: See TracChangeset for help on using the changeset viewer.