Subversion Repositories spk

Rev

Rev 1 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "bob_dom_cut.h"
#include "../common/indian.h"

#define max(a, b) (a > b ? a : b)
//---------------------------------------------------------------------------------
bool bob_name::toFile(otextfilestream& os)
{
        if(m_text && *m_text!=0) {
                int oldf=os.flags();
                os << noSemicolons << "N " << autoSemicolons << m_text;
                os.flags(oldf);
        }
        return os.good();
}
//---------------------------------------------------------------------------------
// PATH
bool bob_path::load(ibinaryfilestream& is, int version)
{
        int hdr;
        is >> hdr;
        if(hdr!=HDR_BEGIN){
                error(e_badHeader);
                return false;
        }
        is >> partIdx;
        if(version < 6) {
                int bid;
                is >> bid;
                m_bodyId=new char[20];
                _itoa(bid, m_bodyId, 10);
        }
        else
                is >> m_bodyId;

        is >> cockpitIdx >> parentIdx >> bodyFlags;

        if(peek(is)==bob_name::HDR_BEGIN){
                if(name.load(is)==false){
                        for(ErrorIterator &it=name.errors.begin(); it!=name.errors.end(); ++it){
                                error(it->code, "name: %s", it->text);
                        }
                        return false;
                }
        }

        if(peek(is)==bob_constants::HDR_BEGIN){
                if(constants.load(is)==false){
                        for(ErrorIterator &it=name.errors.begin(); it!=name.errors.end(); ++it){
                                error(it->code, "constants: %s", it->text);
                        }
                        return false;
                }
        }

        if(peek(is)==bob_dom_bob::HDR_BEGIN){
                bob=new bob_dom_bob(m_settings);
                if(bob->load(is)==false){
                        for(ErrorIterator &it=bob->errors.begin(); it!=bob->errors.end(); ++it){
                                error(it->code, "bob->%s", it->text);
                        }
                        return false;
                }
        }
        int noteLineCount;
        is >> noteLineCount;
        if(noteLineCount > 0){
                if(m_notes.load(is, noteLineCount)==false){
                        for(ErrorIterator &it=m_notes.errors.begin(); it!=m_notes.errors.end(); ++it){
                                error(it->code, "notes->%s", it->text);
                        }
                        return false;
                }
        }

        int statCount;
        is >> statCount;
        if(loadStatValues(is, statCount)==false) return false;

        is >> hdr;
        if(is.fail())
                error(e_notEnoughData);
        else if(hdr!=HDR_END)
                error(e_badEndHeader);

        /////////////////
        //if(hdr==HDR_END) formatStats();
        ////////////////
        return hdr==HDR_END && !is.fail();
}
//---------------------------------------------------------------------------------
bool bob_path::loadStatValues(ibinaryfilestream& is, int count)
{
        int hdr;
        is >> hdr;
        if(hdr!=HDR_STAT_BEGIN){
                error(e_badHeader, "Expected STAT header.");
                return false;
        }

        child_type *ch;
        bool bRes;

        for(int i=0; i < count; i++){
                ch=createChild();
                bRes=ch->load(is);

                for(ErrorIterator &it=ch->errors.begin(); it!=ch->errors.end(); ++it){
                        error(it->severity, it->code, "stat->frame[%d]: %s", i, it->text);
                }
                if(bRes==false) break;
        }

        is >> hdr;
        if(hdr!=HDR_STAT_END){
                error(s_error, e_moreData, "More data in STAT section than expected - skipping");
                do{
                        is >> hdr;
                }
                while(hdr!=HDR_STAT_END && is.good());
        }

        if(hdr!=HDR_STAT_END)
                error(e_notEnoughData);
        return hdr==HDR_STAT_END;
}
//---------------------------------------------------------------------------------
bool bob_path::toFile(obinaryfilestream& os, int cutVersion)
{
        os << HDR_BEGIN;
        os << partIdx;
        if(cutVersion >= 6)
                os << bodyId();
        else
                os << atoi(bodyId());

        os << cockpitIdx;
        os << parentIdx;
        os << bodyFlags;
        os << name;
        constants.toFile(os);
        if(bob) bob->toFile(os);
        os << (int)m_notes.size();
        m_notes.toFile(os);
        os << (int)size();

        os << HDR_STAT_BEGIN;
        for(iterator &it=begin(); it!=end(); ++it){
                it->toFile(os);
        }
        os << HDR_STAT_END << HDR_END;
        return true;
}
//---------------------------------------------------------------------------------
bool bob_path::toFile(otextfilestream& os, int idx)
{
        os << noSemicolons << "P " << partIdx << "; B " << bodyId() << "; ";
        if(parentIdx!=-1)
                os << "F " << parentIdx << "; ";

        if(cockpitIdx > 0)
                os << "C " << cockpitIdx << "; ";

        os << name;

        constants.toFile(os);

        /*
                we've got old x2 flags and new x3 flags
                these 2 can be mixed together

                for example body || light

                only the body (64) and scene (128) can be mixed with anything else imho
        */
        if(bodyFlags!=0) {
                if(bodyFlags & fBody)
                        os << "b ";
                if(bodyFlags & fScene)
                        os << "j ";
                int i=bodyFlags & 0x3F;
                if(i)
                        os << (char) ('b' + i) << ' ';
        }

        os << "// idx " << idx << ", flags: " << bodyFlags << endl;

        if(m_notes.size())
                m_notes.toFile(os);

        if(bob){
                os << noSemicolons << "L { // beginning of embedded BOB" << endl;
                os << *bob << "} // end of embedded BOB" << endl << endl;
        }

        int i=0;
        for(iterator &it=begin(); it!=end(); ++it, ++i){
                it->toFile(os);

                os << " // " << i << endl;
        }
        os << endl;

        return os.good();
}
//---------------------------------------------------------------------------------
// CUT file
bool bob_dom_cut::load(ibinaryfilestream& is)
{
        int hdr;
        is >> hdr;
        if(hdr!=HDR_BEGIN){
                error(e_badHeader);
                return false;
        }

        if(peek(is)==bob_info::HDR_BEGIN){
                if(!info.load(is)){
                        for(ErrorIterator &it=info.errors.begin(); it!=info.errors.end(); ++it){
                                error(it->code, "info: %s", it->text);
                        }
                        return false;
                }
        }

        is >> version;

        if(version!=supported_version)
                error(s_warning, e_badVersion, "Unsupported CUT1 version, loading might fail");

        is >> m_storedPathCount;

        child_type *ch;
        for(int i=0; i < m_storedPathCount; i++){
                ch=createChild();
                bool bRes=ch->load(is, version);
                for(ErrorIterator &it=ch->errors.begin(); it!=ch->errors.end(); ++it){
                        error(it->code, "path[%d]->%s", i, it->text);
                }
                if(bRes==false) return false;
        }

        is >> hdr;
        if(hdr!=HDR_END)
                error(e_badEndHeader);
        return hdr==HDR_END;
}
//---------------------------------------------------------------------------------
bool bob_dom_cut::convert(ibinaryfilestream& is, otextfilestream& os)
{
        int hdr;
        is >> hdr;
        if(hdr!=HDR_BEGIN){
                error(e_badHeader);
                return false;
        }

        if(peek(is)==bob_info::HDR_BEGIN){
                if(!info.load(is)){
                        for(ErrorIterator &it=info.errors.begin(); it!=info.errors.end(); ++it){
                                error(it->code, "info: %s", it->text);
                        }
                        return false;
                }
        }

        is >> version;

        os << info << endl << "VER: " << autoSemicolons << version << endl << endl;

        is >> m_storedPathCount;

        child_type *ch;
        for(int i=0; i < m_storedPathCount; i++){
                ch=new bob_path(m_settings);
                bool bRes=ch->load(is, version);

                for(ErrorIterator &it=ch->errors.begin(); it!=ch->errors.end(); ++it){
                        error(it->code, "path[%d]->%s", i, it->text);
                }

                if(bRes)
                        ch->toFile(os, i);

                delete ch;
                if(bRes==false) return false;
        }

        is >> hdr;
        if(hdr!=HDR_END)
                error(e_badEndHeader);
        return hdr==HDR_END;
}
//---------------------------------------------------------------------------------
bool bob_dom_cut::toFile(obinaryfilestream& os)
{
        os << HDR_BEGIN << info << version << (int)size();

        for(iterator &it=begin(); it!=end(); ++it){
                it->toFile(os, version);
        }
        os << HDR_END;

        return os.good();
}
//---------------------------------------------------------------------------------
bool bob_dom_cut::toFile(otextfilestream& os)
{
        os << noSemicolons << info << endl;
        os << "VER: " << autoSemicolons << version << endl << endl;

        int idx=0;
        for(iterator &it=begin(); it!=end(); ++it){
                it->toFile(os, idx++);
                os << endl;
        }

        return os.good();
}
//---------------------------------------------------------------------------------
// NOTES - section NOTE
bool bob_notes::load(ibinaryfilestream& is, int lineCount)
{
        int hdr;

        is >> hdr;
        if(hdr!=HDR_BEGIN){
                error(e_badHeader);
                return false;
        }
        child_type *ch;
        for(int i=0; i < lineCount; i++){
                ch=createChild();
                if(ch->load(is)==false){
                        error(ch->errorCode, "line[%d]: %s", i, bob_traslate_error(ch->errorCode));
                        return false;
                }
        }
        is >> hdr;
        if(hdr!=HDR_END)
                error(e_badEndHeader);

        return hdr==HDR_END && !is.fail();
}
//---------------------------------------------------------------------------------
bool bob_notes::toFile(obinaryfilestream& os)
{
        if(size()==0) return true;

        os << HDR_BEGIN;
        for(iterator &it=begin(); it!=end(); ++it){
                it->toFile(os);
        }
        os << HDR_END;
        return os.good();
}
//---------------------------------------------------------------------------------
bool bob_notes::toFile(otextfilestream& os)
{
 os << noSemicolons << "T // Notes" << endl;
        for(iterator &it=begin(); it!=end(); ++it){
                it->toFile(os);
                os << endl;
        }
        os << -1 << " // end of Notes" << endl;
        return os.good();
}
//---------------------------------------------------------------------------------
// NOTE - object
bool bob_note::load(ibinaryfilestream& is)
{
        is >> value;
        is >> text;
        if(is.fail())
                errorCode=e_notEnoughData;
        return !is.fail();
}
//---------------------------------------------------------------------------------
bool bob_note::toFile(obinaryfilestream& os)
{
        os << value << text;
        return os.good();
}
//---------------------------------------------------------------------------------
bool bob_note::toFile(otextfilestream& os)
{
        os << autoSemicolons << value << text;
        return os.good();
}
//---------------------------------------------------------------------------------
bool bob_constants::load(ibinaryfilestream& is)
{
        int hdr, count;
        is >> hdr;
        if(hdr!=HDR_BEGIN){
                error(e_badHeader);
                return false;
        }

        is >> count;

        constant c;
        for(int i=0; i < count; i++){
                c.load(is);
                values.push_back(c);
        }
        is >> hdr;
        if(hdr!=HDR_END){
                error(e_badEndHeader);
                return false;
        }
        return is.good();
}
//---------------------------------------------------------------------------------
bool bob_constants::toFile(obinaryfilestream& os)
{
        if(values.size()==0) return true;

        os << HDR_BEGIN << (int)values.size();

        for(iterator &it=values.begin(); it!=values.end(); ++it){
                (*it).toFile(os);
        }
        os << HDR_END;
        return os.good();
}
//---------------------------------------------------------------------------------
bool bob_constants::toFile(otextfilestream& os)
{
        if(values.size()==0) return true;

        os << noSemicolons << "K " << autoSemicolons << (int)values.size();
        for(iterator &it=values.begin(); it!=values.end(); ++it){
                (*it).toFile(os);
        }
        os << noSemicolons;
        return os.good();
}
//---------------------------------------------------------------------------------
bool bob_constants::constant::load(ibinaryfilestream& is)
{
        is >> a >> b;
        return is.good();
}
//---------------------------------------------------------------------------------
bool bob_constants::constant::toFile(obinaryfilestream& os)
{
        os << a << b;
        return os.good();
}
//---------------------------------------------------------------------------------
bool bob_constants::constant::toFile(otextfilestream& os)
{
        os << a << b;
        return os.good();
}
//---------------------------------------------------------------------------------