FlexWriter.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2004 The Pentagram team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #include "pent_include.h"
00020 
00021 #include "FlexWriter.h"
00022 #include "FlexFile.h"
00023 #include "IDataSource.h"
00024 #include "ODataSource.h"
00025 
00026 DEFINE_RUNTIME_CLASSTYPE_CODE_BASE_CLASS(FlexWriter);
00027 
00028 FlexWriter::FlexWriter(FlexFile * f)
00029 {
00030         if (f)
00031         {
00032                 uint32 i;
00033                 uint32 count = f->getIndexCount();
00034                 for (i = 0; i < count; ++i)
00035                 {
00036                         uint32 size;
00037                         uint8* data = f->getObject(i, &size);
00038                         if (!data) size = 0;
00039                         FlexObject o;
00040                         o.size = size;
00041                         o.obj = data;
00042                         objects.push_back(o);
00043                 }
00044                 delete f;
00045         }
00046 }
00047 
00048 FlexWriter::~FlexWriter()
00049 {
00050         std::vector<FlexObject>::iterator it;
00051         for (it = objects.begin(); it != objects.end(); ++it)
00052         {
00053                 delete [] it->obj;
00054         }
00055         objects.clear();
00056 }
00057 
00058 void FlexWriter::add_object(const uint8* obj, uint32 size)
00059 {
00060         FlexObject o;
00061         o.obj = new uint8[size];
00062         o.size = size;
00063         std::memcpy (o.obj, obj, size);
00064         objects.push_back(o);
00065 }
00066  
00067 void FlexWriter::add_datasource(IDataSource* ds)
00068 {
00069         FlexObject o;
00070         o.size = ds->getSize();
00071         o.obj = new uint8[o.size];
00072         ds->seek(0);
00073         ds->read(o.obj, o.size);
00074         objects.push_back(o);
00075         delete ds;
00076 }
00077 
00078 void FlexWriter::set_object(uint32 index, const uint8* obj, uint32 size)
00079 {
00080         if (index >= objects.size())
00081         { // just add instead
00082                 add_object(obj, size);
00083         }
00084         FlexObject * o = &objects.at(index);
00085 
00086         delete o->obj;
00087         o->obj = new uint8[size];
00088         o->size = size;
00089         std::memcpy (o->obj, obj, size);
00090 }
00091 
00092 void FlexWriter::set_datasource(uint32 index, IDataSource* ds)
00093 {
00094         if (index >= objects.size())
00095         { // just add instead
00096                 add_datasource(ds);
00097         }
00098         FlexObject * o = &objects.at(index);
00099 
00100         delete o->obj;
00101         o->size = ds->getSize();
00102         o->obj = new uint8[o->size];
00103         ds->seek(0);
00104         ds->read(o->obj, o->size);
00105         delete ds;
00106 }
00107 
00108 void FlexWriter::write(ODataSource* ds)
00109 {
00110         writeHead(ds);
00111         std::vector<FlexObject>::iterator it;
00112         for (it = objects.begin(); it != objects.end(); ++it)
00113         {
00114                 if (it->obj && it->size != 0)
00115                         ds->write(it->obj, it->size);
00116         }       
00117 }
00118 
00119 void FlexWriter::writeHead(ODataSource* ds)
00120 {
00121         uint32 i;
00122         ds->seek(0);
00123         for (i = 0; i < (0x50 / 4); i++)
00124         {
00125                 ds->write4(0x1A1A1A1A);
00126         }
00127         ds->write4(0x00001A1A);
00128         ds->write4(objects.size());
00129         // FIXME! This is what many flexes have next, but not all. Find out why.
00130         ds->write4(0x00000001);
00131         // FIXME! Figure out what to write until 0x80.
00132         for (i = ds->getPos(); i < 0x80; i += 4)
00133         {
00134                 ds->write4(0);
00135         }
00136 
00137         ds->seek(0x80);
00138         i = objects.size() * 8 + 0x80; // begining offset
00139         std::vector<FlexObject>::iterator it;
00140         for (it = objects.begin(); it != objects.end(); ++it)
00141         {
00142                 if (!it->obj || it->size == 0)
00143                 {
00144                         ds->write4(0);
00145                 }
00146                 else
00147                 {
00148                         ds->write4(i);
00149                 }
00150                 ds->write4(it->size);
00151                 i += it->size;
00152         }
00153         
00154         //complete file size
00155         ds->seek(0x5c);
00156         ds->write4(i);
00157 
00158         i = objects.size() * 8 + 0x80; // begining offset
00159         ds->seek(i);
00160 }

Generated on Fri Jul 27 22:27:14 2007 for pentagram by  doxygen 1.4.7