vcglib/apps/nexus/mfile.cpp

193 lines
5.7 KiB
C++
Raw Normal View History

2005-02-08 13:43:03 +01:00
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
****************************************************************************/
2004-10-21 14:14:02 +02:00
#include "mfile.h"
#include <assert.h>
#include <iostream>
using namespace std;
using namespace nxs;
bool MFile::Create(const string &fname, unsigned int mxs) {
2004-11-28 02:23:26 +01:00
Close();
2004-10-21 14:14:02 +02:00
filename = fname;
2005-01-14 16:25:29 +01:00
_size = 0;
2004-10-21 14:14:02 +02:00
readonly = false;
2004-10-21 17:05:39 +02:00
assert(mxs <= MFILE_MAX_SIZE);
2004-10-21 14:14:02 +02:00
max_size = mxs;
2004-10-21 15:40:16 +02:00
return AddFile();
2004-10-21 14:14:02 +02:00
}
bool MFile::Load(const string &fname, bool ronly) {
2004-11-28 02:23:26 +01:00
Close();
2004-10-21 14:14:02 +02:00
filename = fname;
readonly = ronly;
2004-10-21 17:05:39 +02:00
max_size = MFILE_MAX_SIZE;
2005-01-14 16:25:29 +01:00
_size = 0;
2004-10-21 14:14:02 +02:00
while(1) {
string name = Name(files.size());
2004-11-28 02:23:26 +01:00
File *file = new File;
files.push_back(file);
if(!file->Load(name, ronly)) {
2004-10-21 14:14:02 +02:00
files.pop_back();
break;
}
2005-01-14 16:25:29 +01:00
_size += file->Length();
2004-10-21 14:14:02 +02:00
}
if(files.size() == 0) return false;
if(files.size() == 1) {
2005-01-14 16:25:29 +01:00
assert(_size <= max_size);
2004-10-21 14:14:02 +02:00
} else {
//SANITY TEST
for(unsigned int i = 0; i < files.size() -2; i++) {
2004-11-28 02:23:26 +01:00
if(files[i]->Length() != files[i++]->Length()) {
2004-10-21 14:14:02 +02:00
//"Inconsistent file size for some file.\n";
return false;
}
2004-11-28 02:23:26 +01:00
max_size = files[0]->Length();
2004-10-21 14:14:02 +02:00
}
}
return true;
}
void MFile::Close() {
2004-11-28 02:23:26 +01:00
for(unsigned int i = 0; i < files.size(); i++)
delete files[i];
2004-10-21 14:14:02 +02:00
files.clear();
}
2004-11-18 19:30:15 +01:00
void MFile::Delete() {
while(files.size())
RemoveFile();
}
2004-10-21 15:40:16 +02:00
void MFile::Redim(int64 sz) {
2004-10-21 14:14:02 +02:00
assert(!readonly);
2005-01-14 16:25:29 +01:00
if(sz > _size) {
2004-10-21 15:40:16 +02:00
unsigned int totfile = (unsigned int)(sz/max_size);
//TODO test rhis!!!!
2004-10-21 17:05:39 +02:00
while(files.size() <= totfile) {
2004-10-21 14:14:02 +02:00
RedimLast(max_size);
2005-01-14 16:25:29 +01:00
assert(_size == (int64)max_size * (int64)(files.size()));
2004-10-21 14:14:02 +02:00
AddFile();
}
2005-01-14 16:25:29 +01:00
assert(_size <= sz);
assert(sz - _size < max_size);
assert(files.back()->Length() + (unsigned int)(sz - _size) < max_size);
RedimLast(files.back()->Length() + (unsigned int)(sz - _size));
2004-10-21 14:14:02 +02:00
} else {
2005-01-14 16:25:29 +01:00
while(_size - files.back()->Length() > sz)
2004-10-21 14:14:02 +02:00
RemoveFile();
2005-01-14 16:25:29 +01:00
assert(sz <= _size);
RedimLast(files.back()->Length() - (unsigned int)(_size - sz));
2004-10-21 14:14:02 +02:00
}
2005-01-14 16:25:29 +01:00
assert(sz == _size);
2004-10-21 14:14:02 +02:00
}
2004-10-21 15:40:16 +02:00
void MFile::SetPosition(int64 pos) {
2005-01-14 16:25:29 +01:00
assert(pos <= _size);
2004-10-21 15:40:16 +02:00
curr_fp = (unsigned int)(pos/(int64)max_size);
2004-10-21 17:05:39 +02:00
curr_pos = (unsigned int)(pos - (int64)max_size * (int64)curr_fp);
assert(curr_pos < max_size);
2004-10-21 14:14:02 +02:00
assert(curr_fp < files.size());
2004-11-28 02:23:26 +01:00
files[curr_fp]->SetPosition(curr_pos);
2004-10-21 14:14:02 +02:00
}
void MFile::ReadBuffer(void *data, unsigned int sz) {
while(sz + curr_pos > max_size) {
unsigned int n = max_size - curr_pos;
2004-11-28 02:23:26 +01:00
files[curr_fp]->ReadBuffer(data, n);
2004-10-21 14:14:02 +02:00
data = ((char *)data) + n;
sz -= n;
curr_fp++;
2004-12-02 18:07:34 +01:00
assert(curr_fp < files.size());
2004-10-21 14:14:02 +02:00
curr_pos = 0;
2004-11-28 02:23:26 +01:00
files[curr_fp]->SetPosition(curr_pos);
2004-10-21 14:14:02 +02:00
}
2004-11-28 02:23:26 +01:00
files[curr_fp]->ReadBuffer(data, sz);
2004-10-21 14:14:02 +02:00
}
void MFile::WriteBuffer(void *data, unsigned int sz) {
assert(!readonly);
while(sz + curr_pos > max_size) {
unsigned int n = max_size - curr_pos;
2004-11-28 02:23:26 +01:00
files[curr_fp]->WriteBuffer(data, n);
2004-10-21 14:14:02 +02:00
data = ((char *)data) + n;
sz -= n;
curr_fp++;
2004-12-02 18:07:34 +01:00
assert(curr_fp < files.size());
2004-10-21 14:14:02 +02:00
curr_pos = 0;
2004-11-28 02:23:26 +01:00
files[curr_fp]->SetPosition(curr_pos);
2004-10-21 14:14:02 +02:00
}
2004-11-28 02:23:26 +01:00
files[curr_fp]->WriteBuffer(data, sz);
2004-10-21 14:14:02 +02:00
}
2004-10-21 15:40:16 +02:00
bool MFile::AddFile() {
2004-10-21 14:14:02 +02:00
string name = Name(files.size());
2004-11-28 02:23:26 +01:00
File *file = new File;
files.push_back(file);
return file->Create(name);
2004-10-21 14:14:02 +02:00
}
void MFile::RemoveFile() {
assert(files.size());
2004-11-18 19:30:15 +01:00
string name = Name(files.size()-1);
2004-11-28 02:23:26 +01:00
File *file = files.back();
unsigned int last_size = file->Length();
delete file;
2004-10-21 14:14:02 +02:00
files.pop_back();
2005-01-14 16:25:29 +01:00
_size -= last_size;
2004-11-18 19:30:15 +01:00
cerr << "Removing file: " << name << endl;
2004-10-21 14:14:02 +02:00
#ifdef WIN32
DeleteFile(name.c_str());
#else
unlink(name.c_str());
#endif
}
2004-10-21 17:05:39 +02:00
void MFile::RedimLast(unsigned int sz) {
assert(sz <= max_size);
2004-11-28 02:23:26 +01:00
File &file = *files.back();
2004-12-02 18:07:34 +01:00
unsigned int last_size = (int64)file.Length();
2004-10-21 14:14:02 +02:00
file.Redim(sz);
2005-01-14 16:25:29 +01:00
_size += sz - (int64)last_size;
2004-10-21 14:14:02 +02:00
}
std::string MFile::Name(unsigned int n) {
char buffer[1024];
if(n == 0)
sprintf(buffer, "%s", filename.c_str());
else
sprintf(buffer, "%s%d", filename.c_str(), n);
return string(buffer);
}