📄 basicexcel.cpp
字号:
int CompoundFile::ReadFile(const wchar_t* path, vector<char>& data)
// PURPOSE: Read a file's data in the compound file.
// PROMISE: Returns the small blocks of data stored by the Root Entry if path = "\".
// PROMISE: data will not be set if file is not present in the compound file.
{
data.clear();
int dataSize;
int ret = FileSize(path, dataSize);
if (ret != SUCCESS) return ret;
data.resize(dataSize);
return ReadFile(path, &*(data.begin()));
}
int CompoundFile::WriteFile(const wchar_t* path, const char* data, int size)
// PURPOSE: Write data to a file in the compound file.
// PROMISE: The file's original data will be replaced by the new data.
{
PropertyTree* property = FindProperty(path);
if (property == 0) return FILE_NOT_FOUND;
if (property->self_->size_ >= 4096)
{
if (size >= 4096) property->self_->startBlock_ = WriteData(data, size, property->self_->startBlock_, true);
else
{
property->self_->startBlock_ = WriteData(0, 0, property->self_->startBlock_, true);
property->self_->startBlock_ = WriteData(data, size, property->self_->startBlock_, false);
}
}
else
{
if (size < 4096) property->self_->startBlock_ = WriteData(data, size, property->self_->startBlock_, false);
else
{
property->self_->startBlock_ = WriteData(0, 0, property->self_->startBlock_, false);
property->self_->startBlock_ = WriteData(data, size, property->self_->startBlock_, true);
}
}
property->self_->size_ = size;
SaveHeader();
SaveBAT();
SaveProperties();
return SUCCESS;
}
int CompoundFile::WriteFile(const wchar_t* path, const vector<char>& data, int size)
// PURPOSE: Write data to a file in the compound file.
// PROMISE: The file's original data will be replaced by the new data.
{
return WriteFile(path, &*(data.begin()), size);
}
/*************ANSI char compound file, directory and file functions******************/
bool CompoundFile::Create(const char* filename)
{
int filenameLength = strlen(filename);
wchar_t* wname = new wchar_t[filenameLength+1];
mbstowcs(wname, filename, filenameLength);
wname[filenameLength] = 0;
bool ret = Create(wname);
delete[] wname;
return ret;
}
bool CompoundFile::Open(const char* filename, ios_base::openmode mode)
{
int filenameLength = strlen(filename);
wchar_t* wname = new wchar_t[filenameLength+1];
mbstowcs(wname, filename, filenameLength);
wname[filenameLength] = 0;
bool ret = Open(wname, mode);
delete[] wname;
return ret;
}
int CompoundFile::ChangeDirectory(const char* path)
{
int pathLength = strlen(path);
wchar_t* wpath = new wchar_t[pathLength+1];
mbstowcs(wpath, path, pathLength);
wpath[pathLength] = 0;
int ret = ChangeDirectory(wpath);
delete[] wpath;
return ret;
}
int CompoundFile::MakeDirectory(const char* path)
{
int pathLength = strlen(path);
wchar_t* wpath = new wchar_t[pathLength+1];
mbstowcs(wpath, path, pathLength);
wpath[pathLength] = 0;
int ret = MakeDirectory(wpath);
delete[] wpath;
return ret;
}
int CompoundFile::PresentWorkingDirectory(char* path)
{
int pathLength = strlen(path);
wchar_t* wpath = new wchar_t[pathLength+1];
int ret = PresentWorkingDirectory(wpath);
if (ret == SUCCESS)
{
pathLength = wcslen(wpath);
wcstombs(path, wpath, pathLength);
path[pathLength] = 0;
}
delete[] wpath;
return ret;
}
int CompoundFile::PresentWorkingDirectory(vector<char>& path)
{
vector<wchar_t> wpath;
int ret = PresentWorkingDirectory(wpath);
if (ret == SUCCESS)
{
int pathLength = wpath.size();
path.resize(pathLength);
wcstombs(&*(path.begin()), &*(wpath.begin()), pathLength);
path[pathLength] = 0;
}
return ret;
}
int CompoundFile::RemoveDirectory(const char* path)
{
int pathLength = strlen(path);
wchar_t* wpath = new wchar_t[pathLength+1];
mbstowcs(wpath, path, pathLength);
wpath[pathLength] = 0;
int ret = RemoveDirectory(wpath);
delete[] wpath;
return ret;
}
int CompoundFile::DelTree(const char* path)
{
int pathLength = strlen(path);
wchar_t* wpath = new wchar_t[pathLength+1];
mbstowcs(wpath, path, pathLength);
wpath[pathLength] = 0;
int ret = DelTree(wpath);
delete[] wpath;
return ret;
}
int CompoundFile::MakeFile(const char* path)
{
int pathLength = strlen(path);
wchar_t* wpath = new wchar_t[pathLength+1];
mbstowcs(wpath, path, pathLength);
wpath[pathLength] = 0;
int ret = MakeFile(wpath);
delete[] wpath;
return ret;
}
int CompoundFile::RemoveFile(const char* path)
{
int pathLength = strlen(path);
wchar_t* wpath = new wchar_t[pathLength+1];
mbstowcs(wpath, path, pathLength);
wpath[pathLength] = 0;
int ret = RemoveFile(wpath);
delete[] wpath;
return ret;
}
int CompoundFile::FileSize(const char* path, int& size)
{
int pathLength = strlen(path);
wchar_t* wpath = new wchar_t[pathLength+1];
mbstowcs(wpath, path, pathLength);
wpath[pathLength] = 0;
int ret = FileSize(wpath, size);
delete[] wpath;
return ret;
}
int CompoundFile::ReadFile(const char* path, char* data)
{
int pathLength = strlen(path);
wchar_t* wpath = new wchar_t[pathLength+1];
mbstowcs(wpath, path, pathLength);
wpath[pathLength] = 0;
int ret = ReadFile(wpath, data);
delete[] wpath;
return ret;
}
int CompoundFile::ReadFile(const char* path, vector<char>& data)
{
int pathLength = strlen(path);
wchar_t* wpath = new wchar_t[pathLength+1];
mbstowcs(wpath, path, pathLength);
wpath[pathLength] = 0;
int ret = ReadFile(wpath, data);
delete[] wpath;
return ret;
}
int CompoundFile::WriteFile(const char* path, char* data, int size)
{
int pathLength = strlen(path);
wchar_t* wpath = new wchar_t[pathLength+1];
mbstowcs(wpath, path, pathLength);
wpath[pathLength] = 0;
int ret = WriteFile(wpath, data, size);
delete[] wpath;
return ret;
}
int CompoundFile::WriteFile(const char* path, vector<char>& data, int size)
{
int pathLength = strlen(path);
wchar_t* wpath = new wchar_t[pathLength+1];
mbstowcs(wpath, path, pathLength);
wpath[pathLength] = 0;
int ret = WriteFile(wpath, data, size);
delete[] wpath;
return ret;
}
/*********************** Inaccessible General Functions ***************************/
void CompoundFile::IncreaseLocationReferences(vector<int> indices)
// PURPOSE: Increase block location references in header, BAT indices and properties,
// PURPOSE: which will be affected by the insertion of new indices contained in indices.
// PROMISE: Block location references which are smaller than all the new indices
// PROMISE: will not be affected.
// PROMISE: SBAT location references will not be affected.
// PROMISE: Changes will not be written to compound file.
{
int maxIndices = indices.size();
// Change BAT Array references
{for (int i=0; i<109 && header_.BATArray_[i]!=-1; ++i)
{
int count = 0;
for (int j=0; j<maxIndices; ++j)
{
if (header_.BATArray_[i] >= indices[j] &&
header_.BATArray_[i] != -1) ++count;
}
header_.BATArray_[i] += count;
}}
// Change XBAT start block if any
if (header_.XBATCount_)
{
int count = 0;
for (int j=0; j<maxIndices; ++j)
{
if (header_.XBATStart_ >= indices[j] &&
header_.XBATStart_ != -2) ++count;
}
header_.XBATStart_ += count;
}
// Change SBAT start block if any
if (header_.SBATCount_)
{
int count = 0;
for (int j=0; j<maxIndices; ++j)
{
if (header_.SBATStart_ >= indices[j] &&
header_.SBATStart_ != -2) ++count;
}
header_.SBATStart_ += count;
}
// Change BAT block indices
int maxBATindices = blocksIndices_.size();
{for (int i=0; i<maxBATindices && blocksIndices_[i]!=-1; ++i)
{
int count = 0;
for (int j=0; j<maxIndices; ++j)
{
if (blocksIndices_[i] > indices[j] &&
blocksIndices_[i] != -2 &&
blocksIndices_[i] != -3) ++count;
}
blocksIndices_[i] += count;
}}
// Change properties start block
int count = 0;
{for (int i=0; i<maxIndices; ++i)
{
if (header_.propertiesStart_ >= indices[i] &&
header_.propertiesStart_ != -2) ++count;
}}
header_.propertiesStart_ += count;
// Change individual properties start block if their size is more than 4096
int maxProperties = properties_.size();
if (!properties_.empty())
{
int count = 0;
for (int j=0; j<maxIndices; ++j)
{
if (properties_[0]->startBlock_ >= indices[j] &&
properties_[0]->startBlock_ != -2) ++count;
}
properties_[0]->startBlock_ += count;
}
{for (int i=1; i<maxProperties; ++i)
{
if (properties_[i]->size_ >= 4096)
{
int count = 0;
for (int j=0; j<maxIndices; ++j)
{
if (properties_[i]->startBlock_ >= indices[j] &&
properties_[i]->startBlock_ != -2) ++count;
}
properties_[i]->startBlock_ += count;
}
}}
}
void CompoundFile::DecreaseLocationReferences(vector<int> indices)
// PURPOSE: Decrease block location references in header, BAT indices and properties,
// PURPOSE: which will be affected by the deletion of indices contained in indices.
// PROMISE: BAT indices pointing to a deleted index will be redirected to point to
// PROMISE: the location where the deleted index original points to.
// PROMISE: Block location references which are smaller than all the new indices
// PROMISE: will not be affected.
// PROMISE: SBAT location references will not be affected.
// PROMISE: Changes will not be written to compound file.
{
int maxIndices = indices.size();
// Change BAT Array references
{for (int i=0; i<109 && header_.BATArray_[i]!=-1; ++i)
{
int count = 0;
for (int j=0; j<maxIndices; ++j)
{
if (header_.BATArray_[i] > indices[j] &&
header_.BATArray_[i] != -1) ++count;
}
header_.BATArray_[i] -= count;
}}
// Change XBAT start block if any
if (header_.XBATCount_)
{
int count = 0;
for (int j=0; j<maxIndices; ++j)
{
if (header_.XBATStart_ > indices[j] &&
header_.XBATStart_ != -2) ++count;
}
header_.XBATStart_ -= count;
}
// Change SBAT start block if any
if (header_.SBATCount_)
{
int count = 0;
for (int j=0; j<maxIndices; ++j)
{
if (header_.SBATStart_ > indices[j] &&
header_.SBATStart_ != -2) ++count;
}
header_.SBATStart_ -= count;
}
// Change BAT block indices
// Redirect BAT indices pointing to a deleted index to point to
// the location where the deleted index original points to.
int maxBATindices = blocksIndices_.size();
{for (int i=0; i<maxBATindices && blocksIndices_[i]!=-1; ++i)
{
bool end;
do
{
end = true;
for (int j=0; j<maxIndices; ++j)
{
if (blocksIndices_[i] == indices[j])
{
blocksIndices_[i] = blocksIndices_[indices[j]];
end = false;
break;
}
}
} while (!end);
}}
// Erase indices to be deleted from the block indices
sort (indices.begin(), indices.end(), greater<int>());
{for (int i=0; i<maxIndices; ++i)
{
blocksIndices_.erase(blocksIndices_.begin()+indices[i]);
blocksIndices_.push_back(-1);
}}
// Decrease block location references for affected block indices.
{for (int i=0; i<maxBATindices && blocksIndices_[i]!=-1; ++i)
{
int count = 0;
for (int j=0; j<maxIndices; ++j)
{
if (blocksIndices_[i] > indices[j] &&
blocksIndices_[i] != -2 &&
blocksIndices_[i] != -3) ++count;
}
blocksIndices_[i] -= count;
}}
// Change properties start block
int count = 0;
{for (int i=0; i<maxIndices; ++i)
{
if (header_.propertiesStart_ > indices[i] &&
header_.propertiesStart_ != -2) ++count;
}}
header_.propertiesStart_ -= count;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -