158 lines
4.6 KiB
C++
158 lines
4.6 KiB
C++
#include "StdAfx.h"
|
|
#include "mywt_file.h"
|
|
|
|
/************************************************************\
|
|
* Een variant op WT_File die ook EPlotSections uit DWF-
|
|
* packages kan lezen of schrijven
|
|
* of om precies te zijn: Onze CEPlotSectionImpl
|
|
* Gebruik dan set_eplotsection ipv set_filename
|
|
\************************************************************/
|
|
|
|
myWT_File::myWT_File(void): m_color(0x00ffffff),
|
|
m_EPlotSection(NULL)
|
|
{
|
|
}
|
|
|
|
myWT_File::~myWT_File(void)
|
|
{
|
|
}
|
|
|
|
WT_Result myWT_File::set_eplotsection(CEPlotSectionImpl *EPlotSection)
|
|
{
|
|
m_EPlotSection = EPlotSection;
|
|
return WT_Result::Success;
|
|
}
|
|
|
|
|
|
WT_Result myWT_File::open()
|
|
{
|
|
m_current_file_position = 0;
|
|
if (m_EPlotSection) // Deze heeft altijd voorkeur
|
|
{
|
|
CString src;
|
|
m_EPlotSection->get_SourceDescription(src);
|
|
myTRACE("\nmyWT_File::open %s in this 0x%x", src, this);
|
|
set_stream_user_data(this);
|
|
set_stream_open_action(my_WSopen);
|
|
set_stream_close_action(my_WSclose);
|
|
set_stream_read_action(my_WSread);
|
|
set_stream_seek_action(my_WSseek);
|
|
#ifndef DWFTK_READ_ONLY
|
|
set_stream_write_action (my_WSwrite);
|
|
#endif
|
|
//void set_stream_end_seek_action (WT_Stream_End_Seek_Action action)
|
|
//void set_stream_tell_action (WT_Stream_Tell_Action action)
|
|
|
|
}
|
|
else // moet/zal het een oldstyle DWF zijn
|
|
ATLASSERT(this->filename().length()>0);
|
|
return WT_File::open();
|
|
}
|
|
|
|
|
|
WT_Result myWT_File::my_WSopen (WT_File & file)
|
|
{
|
|
myWT_File *mine = (myWT_File *)file.stream_user_data();
|
|
mine->m_EPlotSection->Open(file.file_mode() == File_Write);
|
|
return WT_Result::Success;
|
|
}
|
|
|
|
WT_Result myWT_File::my_WSclose (WT_File & file)
|
|
{
|
|
myWT_File *mine = (myWT_File *)file.stream_user_data();
|
|
HRESULT res = mine->m_EPlotSection->Close();
|
|
return WT_Result::Success;
|
|
};
|
|
|
|
WT_Result myWT_File::my_WSread (WT_File & file, int desired_bytes,
|
|
int &bytes_read, void * buffer)
|
|
{
|
|
myWT_File *mine = (myWT_File *)file.stream_user_data();
|
|
ULONG bread; // IStream
|
|
HRESULT res = mine->m_EPlotSection->Read(buffer,desired_bytes, &bread);
|
|
bytes_read = bread;
|
|
mine->m_current_file_position += bread;
|
|
return WT_Result::Success;
|
|
};
|
|
|
|
// O.a. gebruikt door WT_Unknown om te skippen
|
|
WT_Result myWT_File::my_WSseek (WT_File & file, int distance, int &amount_seeked)
|
|
{
|
|
myWT_File *mine = (myWT_File *)file.stream_user_data();
|
|
ULONG bread; // IStream
|
|
HRESULT res = mine->m_EPlotSection->Seek(distance, &bread);
|
|
amount_seeked = bread;
|
|
mine->m_current_file_position += bread;
|
|
return WT_Result::Success;
|
|
};
|
|
|
|
#ifndef DWFTK_READ_ONLY
|
|
WT_Result myWT_File::my_WSwrite (WT_File & file,int size,void const * buffer)
|
|
{
|
|
myWT_File *mine = (myWT_File *)file.stream_user_data();
|
|
ULONG bWritten; // IStream
|
|
HRESULT res = mine->m_EPlotSection->Write(buffer,size,&bWritten);
|
|
return WT_Result::Success;
|
|
};
|
|
|
|
bool myWT_File::SaveAsAscii(const CString &destpath)
|
|
{
|
|
try
|
|
{
|
|
set_file_mode(WT_File::File_Read);
|
|
open();
|
|
|
|
WT_File my_output_file;
|
|
my_output_file.set_filename(destpath);
|
|
my_output_file.set_file_mode(WT_File::File_Write);
|
|
|
|
|
|
WT_Result result;
|
|
// Do the actual reading.
|
|
while (result = process_next_object(), result==WT_Result::Success)
|
|
{
|
|
switch(current_object()->object_id())
|
|
{
|
|
case WT_Object::DWF_Header_ID:
|
|
{
|
|
my_output_file.heuristics().set_target_version(rendition().drawing_info().decimal_revision());
|
|
my_output_file.open();
|
|
my_output_file.heuristics().set_allow_binary_data(WD_False);
|
|
break;
|
|
}
|
|
case WT_Object::Origin_ID:
|
|
break; // Mag niet met ASCII
|
|
default:
|
|
switch(current_object()->object_type())
|
|
{
|
|
case WT_Object::Drawable:
|
|
current_object()->serialize(my_output_file);
|
|
break;
|
|
case WT_Object::Attribute:
|
|
{ const WT_Attribute *obj = (WT_Attribute *)current_object();
|
|
// myTRACE("Attribute '%s'\n", my_plan_file.file_stats()->descriptions());
|
|
// Hoewel een Attibute gaat hij toch niet via desired_rendition
|
|
//Omdat we weten dat we net uit uit een DWF komen kunnen we best wel rechtstreeks serializen
|
|
//Let wel: desired_rendition is dan niet meer te vertrouwen. Zie @@@
|
|
obj->serialize(my_output_file);
|
|
}
|
|
break;
|
|
default:
|
|
myTRACE("Skipping '%s'\n", file_stats()->descriptions());
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
close(); // closing Input file.
|
|
my_output_file.close(); // closing Output file.
|
|
}
|
|
catch (DWFException& ex)
|
|
{
|
|
CString err;
|
|
err.Format("%ls\n%ls\n%s\n%ls(%d)",ex.type(),ex.message(),ex.function(),ex.file(),ex.line());
|
|
throw myCString("\nSaveAsAscii(\"%s\")\n%s", destpath, err);
|
|
}
|
|
return true;
|
|
}
|
|
#endif |