Skip to content

Commit

Permalink
Merge pull request e8tools#198 from andreevlex/feature/issue-37
Browse files Browse the repository at this point in the history
Создан класс V8Time
  • Loading branch information
dmpas authored Feb 8, 2018
2 parents 14cd465 + 0d3e0a3 commit f58619d
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 95 deletions.
14 changes: 9 additions & 5 deletions src/tool1cd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ project (tool1cd)
set (TOOL1CD_SOURCES MessageRegistration.cpp Class_1CD.cpp
Common.cpp ConfigStorage.cpp Parse_tree.cpp TempStream.cpp Base64.cpp UZLib.cpp Messenger.cpp
V8Object.cpp Field.cpp Index.cpp Table.cpp TableFiles.cpp TableFileStream.cpp
MemBlock.cpp CRC32.cpp Packdata.cpp PackDirectory.cpp FieldType.cpp DetailedException.cpp BinaryDecimalNumber.cpp save_depot_config.cpp save_part_depot_config.cpp
MemBlock.cpp CRC32.cpp Packdata.cpp PackDirectory.cpp FieldType.cpp DetailedException.cpp
BinaryDecimalNumber.cpp save_depot_config.cpp save_part_depot_config.cpp
SupplierConfig.cpp TableRecord.cpp BinaryGuid.cpp TableIterator.cpp)

set (TOOL1CD_HEADERS MessageRegistration.h Class_1CD.h
Common.h ConfigStorage.h Parse_tree.h TempStream.h Base64.h UZLib.h Messenger.h
db_ver.h NodeTypes.h V8Object.h Constants.h Field.h Index.h Table.h TableFiles.h
TableFileStream.h MemBlock.h CRC32.h Packdata.h PackDirectory.h FieldType.h DetailedException.h BinaryDecimalNumber.h
SupplierConfig.h TableRecord.h BinaryGuid.h TableIterator.h)
TableFileStream.h MemBlock.h CRC32.h Packdata.h PackDirectory.h FieldType.h DetailedException.h
BinaryDecimalNumber.h SupplierConfig.h TableRecord.h BinaryGuid.h TableIterator.h)

# .CF API
set (TOOL1CD_SOURCES ${TOOL1CD_SOURCES} cfapi/V8File.cpp cfapi/V8Catalog.cpp cfapi/TV8FileStream.cpp cfapi/APIcfBase.cpp)
set (TOOL1CD_HEADERS ${TOOL1CD_HEADERS} cfapi/V8File.h cfapi/V8Catalog.h cfapi/TV8FileStream.h cfapi/APIcfBase.h)
set (TOOL1CD_SOURCES ${TOOL1CD_SOURCES} cfapi/V8File.cpp cfapi/V8Catalog.cpp cfapi/TV8FileStream.cpp
cfapi/APIcfBase.cpp cfapi/V8Time.cpp)
set (TOOL1CD_HEADERS ${TOOL1CD_HEADERS} cfapi/V8File.h cfapi/V8Catalog.h cfapi/TV8FileStream.h
cfapi/APIcfBase.h cfapi/V8Time.h)

add_library (tool1cd STATIC ${TOOL1CD_SOURCES} ${TOOL1CD_HEADERS})

Expand Down
4 changes: 2 additions & 2 deletions src/tool1cd/ConfigStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ bool ConfigStorageTable::save_config(const boost::filesystem::path &file_name)
if(tf->ropen())
{
V8File* f = catalog->createFile(tf->name);
f->SetTimeCreate(&tf->file->ft_create);
f->SetTimeModify(&tf->file->ft_modify);
f->time_create(tf->file->ft_create);
f->time_modify(tf->file->ft_modify);
f->WriteAndClose(tf->rstream);

tf->close();
Expand Down
40 changes: 0 additions & 40 deletions src/tool1cd/cfapi/APIcfBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,3 @@ unsigned WindowsTickToUnixSeconds(long long windowsTicks)
{
return (unsigned)(windowsTicks / WINDOWS_TICK - SEC_TO_UNIX_EPOCH);
}

//---------------------------------------------------------------------------
//преобразование времени
void V8timeToFileTime(const int64_t* v8t, FILETIME* ft) {

FILETIME lft;

int64_t t = *v8t;
t -= EPOCH_START_WIN; //504911232000000 = ((365 * 4 + 1) * 100 - 3) * 4 * 24 * 60 * 60 * 10000
t *= 1000;
*(int64_t*)&lft = t;
LocalFileTimeToFileTime(&lft, ft);

}

//---------------------------------------------------------------------------
// обратное преобразование времени
void FileTimeToV8time(const FILETIME* ft, int64_t* v8t) {

FILETIME lft;

FileTimeToLocalFileTime(ft, &lft);
int64_t t = *(int64_t*)&lft;
t /= 1000;
t += EPOCH_START_WIN; //504911232000000 = ((365 * 4 + 1) * 100 - 3) * 4 * 24 * 60 * 60 * 10000
*v8t = t;
}

//---------------------------------------------------------------------------
// установка текущего времени
void setCurrentTime(int64_t* v8t)
{
SYSTEMTIME st;
FILETIME ft;

GetSystemTime(&st);
SystemTimeToFileTime(&st, &ft);
FileTimeToV8time(&ft, v8t);

}
3 changes: 0 additions & 3 deletions src/tool1cd/cfapi/APIcfBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ enum class block_header: int {
};

unsigned WindowsTickToUnixSeconds(long long windowsTicks);
void V8timeToFileTime(const int64_t* v8t, FILETIME* ft);
void FileTimeToV8time(const FILETIME* ft, int64_t* v8t);
void setCurrentTime(int64_t* v8t);

#endif

11 changes: 8 additions & 3 deletions src/tool1cd/cfapi/V8Catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ void V8Catalog::initialize()
_temp_buf = new char[_file_header->GetSize()];
_file_header->Read(_temp_buf, _file_header->GetSize());
_name = (WCHART*)(_temp_buf + 20);
_file = new V8File(this, _name, _prev, _fi.data_start, _fi.header_start, (int64_t*)_temp_buf, (int64_t*)(_temp_buf + 8));
int64_t time_create = *(int64_t*)_temp_buf;
int64_t time_modify = *(int64_t*)_temp_buf +8;
_file = new V8File(this, _name, _prev, _fi.data_start, _fi.header_start, time_create, time_modify);
delete[] _temp_buf;
if(!_prev) first = _file;
_prev = _file;
Expand Down Expand Up @@ -435,8 +437,11 @@ V8File* V8Catalog::createFile(const String& FileName, bool _selfzipped){
f = GetFile(FileName);
if(!f)
{
setCurrentTime(&v8t);
f = new V8File(this, FileName, last, 0, 0, &v8t, &v8t);
V8Time v8t = V8Time::current_time();

f = new V8File(this, FileName, last, 0, 0, 0, 0);
f->time_create(v8t);
f->time_modify(v8t);
f->self_zipped(_selfzipped);
last = f;
_fatmodified = true;
Expand Down
71 changes: 37 additions & 34 deletions src/tool1cd/cfapi/V8File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern Registrator msreg_g;

using namespace System;

V8File::V8File(V8Catalog* _parent, const String& _name, V8File* _previous, int _start_data, int _start_header, int64_t* _time_create, int64_t* _time_modify)
V8File::V8File(V8Catalog* _parent, const String& _name, V8File* _previous, int _start_data, int _start_header, int64_t time_create, int64_t time_modify)
{
Lock = new TCriticalSection();
_destructed = false;
Expand All @@ -28,49 +28,47 @@ V8File::V8File(V8Catalog* _parent, const String& _name, V8File* _previous, int _
iscatalog = FileIsCatalog::unknown;
self = nullptr;
is_opened = false;
time_create = *_time_create;
time_modify = *_time_modify;
_time_create = V8Time(time_create);
_time_modify = V8Time(time_modify);
_selfzipped = false;
if(parent) {
V8Catalog::V8Files& files = parent->v8files();
files[name.UpperCase()] = this;
}
}

//---------------------------------------------------------------------------
// получить время создания
void V8File::GetTimeCreate(FILETIME* ft)
{
V8timeToFileTime(&time_create, ft);
System::FILETIME V8File::get_time_create() const {
return _time_create.to_file_time();
}

//---------------------------------------------------------------------------
// получить время модификации
void V8File::GetTimeModify(FILETIME* ft)
{
V8timeToFileTime(&time_modify, ft);
System::FILETIME V8File::get_time_modify() const {
return _time_modify.to_file_time();
}

//---------------------------------------------------------------------------
// установить время создания
void V8File::SetTimeCreate(FILETIME* ft)
{
FileTimeToV8time(ft, &time_create);
void V8File::time_create(const System::FILETIME &file_time) {
_time_create.from_file_time(file_time);
}

void V8File::time_create(const V8Time &time) {
_time_create = time;
}

//---------------------------------------------------------------------------
// установить время модификации
void V8File::SetTimeModify(FILETIME* ft)
{
FileTimeToV8time(ft, &time_modify);
void V8File::time_modify(const System::FILETIME &file_time) {
_time_modify.from_file_time(file_time);
}

void V8File::time_modify(const V8Time &time) {
_time_modify = time;
}

//---------------------------------------------------------------------------
// сохранить в файл
void V8File::SaveToFile(const boost::filesystem::path &FileName)
{
FILETIME create, modify;

#ifdef _MSC_VER

struct _utimbuf ut;
Expand All @@ -91,8 +89,8 @@ void V8File::SaveToFile(const boost::filesystem::path &FileName)
fs.Close();
Lock->Release();

GetTimeCreate(&create);
GetTimeModify(&modify);
FILETIME create = get_time_create();
FILETIME modify = get_time_create();

time_t RawtimeCreate = FileTime_to_POSIX(&create);
struct tm * ptm_create = localtime(&RawtimeCreate);
Expand Down Expand Up @@ -183,7 +181,8 @@ int64_t V8File::Write(const void* Buffer, int Start, int Length) // дозапи
if (!try_open()){
return ret;
}
setCurrentTime(&time_modify);
_time_modify = V8Time::current_time();

is_headermodified = true;
_datamodified = true;
data->Seek(Start, soFromBeginning);
Expand All @@ -202,7 +201,8 @@ int64_t V8File::Write(std::vector<uint8_t> Buffer, int Start, int Length) // д
if (!try_open()){
return ret;
}
setCurrentTime(&time_modify);
_time_modify = V8Time::current_time();

is_headermodified = true;
_datamodified = true;
data->Seek(Start, soFromBeginning);
Expand All @@ -221,7 +221,8 @@ int64_t V8File::Write(const void* Buffer, int Length) // перезапись ц
if (!try_open()) {
return ret;
}
setCurrentTime(&time_modify);
_time_modify = V8Time::current_time();

is_headermodified = true;
_datamodified = true;
if (data->GetSize() > Length) data->SetSize(Length);
Expand All @@ -241,7 +242,8 @@ int64_t V8File::Write(TStream* Stream, int Start, int Length) // дозапис
if (!try_open()) {
return ret;
}
setCurrentTime(&time_modify);
_time_modify = V8Time::current_time();

is_headermodified = true;
_datamodified = true;
data->Seek(Start, soFromBeginning);
Expand All @@ -260,7 +262,8 @@ int64_t V8File::Write(TStream* Stream) // перезапись целиком
if (!try_open()) {
return ret;
}
setCurrentTime(&time_modify);
_time_modify = V8Time::current_time();

is_headermodified = true;
_datamodified = true;
if (data->GetSize() > Stream->GetSize()) data->SetSize(Stream->GetSize());
Expand Down Expand Up @@ -500,8 +503,8 @@ void V8File::Close(){
if(is_headermodified)
{
TMemoryStream* hs = new TMemoryStream();
hs->Write(&time_create, 8);
hs->Write(&time_modify, 8);
_time_create.write_to_stream(hs);
_time_modify.write_to_stream(hs);
hs->Write(&_t, 4);
#ifndef _DELPHI_STRING_UNICODE // FIXME: определится используем WCHART или char
int ws = name.WideCharBufSize();
Expand Down Expand Up @@ -562,8 +565,8 @@ int64_t V8File::WriteAndClose(TStream* Stream, int Length)
parent->Acquire();
start_data = parent->write_datablock(Stream, start_data, _selfzipped, Length);
TMemoryStream hs;
hs.Write(&time_create, 8);
hs.Write(&time_modify, 8);
_time_create.write_to_stream(&hs);
_time_modify.write_to_stream(&hs);
hs.Write(&_4bzero, 4);
hs.Write(wname, name.Length() * sizeof(WCHART));
hs.Write(&_4bzero, 4);
Expand Down Expand Up @@ -664,8 +667,8 @@ void V8File::Flush()
if(is_headermodified)
{
TMemoryStream* hs = new TMemoryStream();
hs->Write(&time_create, 8);
hs->Write(&time_modify, 8);
_time_create.write_to_stream(hs);
_time_modify.write_to_stream(hs);
hs->Write(&_t, 4);
#ifndef _DELPHI_STRING_UNICODE
int ws = name.WideCharBufSize();
Expand Down
22 changes: 14 additions & 8 deletions src/tool1cd/cfapi/V8File.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#define V8FILEH

#include <System.Classes.hpp>
#include <boost/filesystem.hpp>
#include <vector>
#include <set>

#include "V8Catalog.h"
#include "TV8FileStream.h"
#include "../Parse_tree.h"
#include <boost/filesystem.hpp>
#include "V8Time.h"

class V8Catalog;
class TV8FileStream;
Expand All @@ -23,7 +24,7 @@ class V8File {
public:
typedef std::set<TV8FileStream*> TV8FileStreams;

V8File(V8Catalog* _parent, const String& _name, V8File* _previous, int _start_data, int _start_header, int64_t* _time_create, int64_t* _time_modify);
V8File(V8Catalog* _parent, const String& _name, V8File* _previous, int _start_data, int _start_header, int64_t time_create, int64_t time_modify);

~V8File();
bool IsCatalog();
Expand Down Expand Up @@ -51,10 +52,14 @@ class V8File {

int64_t WriteAndClose(TStream* Stream, int Length = -1); // перезапись целиком и закрытие файла (для экономии памяти не используется data файла)

void GetTimeCreate(System::FILETIME* ft);
void GetTimeModify(System::FILETIME* ft);
void SetTimeCreate(System::FILETIME* ft);
void SetTimeModify(System::FILETIME* ft);
System::FILETIME get_time_create() const;
void time_create(const System::FILETIME &file_time);
void time_create(const V8Time &time);

System::FILETIME get_time_modify() const;
void time_modify(const System::FILETIME &file_time);
void time_modify(const V8Time &time);

void SaveToFile(const boost::filesystem::path &FileName);
void SaveToStream(TStream* stream);
TV8FileStream* get_stream(bool own = false);
Expand All @@ -80,8 +85,9 @@ class V8File {

private:
String name;
int64_t time_create;
int64_t time_modify;

V8Time _time_create;
V8Time _time_modify;
TCriticalSection *Lock;
TStream* data;
V8Catalog* parent;
Expand Down
Loading

0 comments on commit f58619d

Please sign in to comment.