📄 codec.c
字号:
// ==========================================================================// // @(#) $Id: Codec.C,v 1.3 2000/10/13 15:45:51 brian Exp $// // --------------------------------------------------------------------------// // Copyright (C) 1997-2000 Brian Bidulock <bidulock@dallas.net>// // All Rights Reserved.// // This library is free software; you can redistribute it and/or modify it// under the terms of the GNU Lesser General Public License as published by// the Free Software Foundation; either version 2.1 of the License, or (at// your option) any later version.// // This library 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 Lesser Public License// for more details.// // You should have received a copy of the GNU Lesser General Public License// along with this library; if not, write to the Free Software Foundation,// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA// // Last Modified $Date: 2000/10/13 15:45:51 $ by $Author: brian $// // --------------------------------------------------------------------------// // $Log: Codec.C,v $// Revision 1.3 2000/10/13 15:45:51 brian// First public release.//// ==========================================================================static char const ident[] = "$Id: Codec.C,v 1.3 2000/10/13 15:45:51 brian Exp $";#pragma implementation#include "head.h"#include "Codec.H"#include <iostream.h>#include <stdarg.h>#include <string.h>Error::Error(char* d,char* l) { desc = d; label = l;};voidError::errout() { cout.form("ERROR at %s: %s\n",label,desc);};Invalid::Invalid(char* l) : Error("Unexpected value." ,l) { errout(); };Bounds ::Bounds (char* l) : Error("Out of bounds." ,l) { errout(); };Repeats::Repeats(char* l) : Error("Non-integer repeat." ,l) { errout(); };SpecErr::SpecErr(char* l) : Error("Specification error." ,l) { errout(); };NullPtr::NullPtr(char* l) : Error("Null parm pointer." ,l) { errout(); };int NewFlag::crumb = 1; // start non-zeroNewFlag::NewFlag() { value = 0;};NewFlag::NewFlag(bool b) { if (b) value = crumb; else value = 0;};NewFlag::NewFlag(int i) { if (i) value = crumb; else value = 0;};NewFlag::operator bool() { if (value>crumb) value=0; // clear really old flags return (value==crumb);};NewFlag::operator int() { if (value>crumb) value=0; // clear really old flags return (value==crumb);};voidNewFlag::clearFlags() { crumb++; if (!crumb) crumb++;};voidFramePointer::set(char* p, int l) { pointer = p; length = l*8; offset = 0; size = length; end = 0;};FramePointer::FramePointer(char* p, int l) { pointer = p; length = l*8; offset = 0; size = length; end = 0;};FramePointer::FramePointer() { pointer = 0; length = 0; offset = 0; size = 0; end = 0;};FramePointer::operator int() { return (size+7)>>3;};FramePointer::operator char*() { return pointer;};FramePointer::operator void*() { return pointer;};char*FramePointer::point() { return pointer + (offset>>3);};int Codec::variant = Codec::ansi;NewFlag Codec::pass2 = false;NewFlag Codec::pass2req = false;int Codec::bldseq = 0;boolCodec::encode(Codec* c,char* p,int& l) { FramePointer f(p,l); bldseq = 0; // reset build sequence number try { c->encode(f); } catch (...) { bldseq = 0; return false; } if (pass2req) { cout << "Starting pass 2...\n"; pass2 = true; f.offset = 0; try { c->encode(f); } catch (Error E) { E.errout(); bldseq = 0; return false; } }; l = (f.end+7)>>3; bldseq = 0; return true;};boolCodec::decode(Codec* c,char* p,int l) { NewFlag::clearFlags(); FramePointer f(p,l); bldseq = 0; // reset build sequence number try { c->decode(f); } catch (Error E) { E.errout(); bldseq = 0; return false; } bldseq = 0; return true;};voidCodec::report(Codec* c) { c->report(); cout.width(78); cout.fill('-'); cout << "-" << '\n';};voidCodec::rebuild() { NewFlag::clearFlags();};Codec::Codec(char* l, char* t, int s, int x) : frame(NULL,0) { label = l; title = t; size = s; spare = x; exists = false; built = false; bound = false; dfltval = 0; mnem = NULL; text = NULL; master = true; bldord = 0;};Codec::Codec(const Codec& original) { label = original.label; title = original.title; size = original.size; spare = original.spare; exists = false; built = false; bound = false; dfltval = original.dfltval; mnem = NULL; text = NULL; master = false; bldord = 0;};Codec*Codec::copy() { return new Codec(*this);};Codec*Codec::newcopy(char* l,char *t) { return new Codec(l,t,size,spare);};Codec::~Codec() {};voidCodec::decode(FramePointer& p) { frame = p; frame.size = size; exists = true;};voidCodec::encode(FramePointer& p) { frame = p; frame.size = size; if (!(pass2&&exists)) { if (built) bldseq = bldord; built = false; exists = true; }};voidCodec::bits(int j) { int siz, btm, top, bit, i; cout.width(3); cout.setf(ios::right,ios::adjustfield); if ((j==0)&&(frame.offset&0x7)) { cout.fill(' '); cout << "|" << ' '; } else { cout.fill('0'); cout << (frame.offset>>3)+j+1 << ' '; } siz = frame.size+(j?(frame.offset&0x7):0)-j*8; btm = 8-(j?0:frame.offset&0x7); top = btm-siz; bit = 0x1 << ((top>0?btm-1-top:btm-1)+j*8-(j?(frame.offset&0x7):0)); for (i=0;i<8;i++) { if (i<top) { if (i<top-spare) cout << '.'; else cout << 'x'; continue; } if (i>=btm) { cout << '.'; continue; } if (value&bit) cout << '1'; else cout << '0'; bit = bit >> 1; }};voidCodec::report() { bits(0); cout << ' '; cout.width(22); cout.setf(ios::left,ios::adjustfield); cout.fill(' '); cout << title; cout.width(0); cout << " : ";};voidCodec::morebits() { if (frame.size+(frame.offset&0x7)+spare>24) { cout.form("--- ******** | . :\n"); } else { if (frame.size+(frame.offset&0x7)+spare>8) { bits(1); cout << " | . :\n"; } if (frame.size+(frame.offset&0x7)+spare>16) { bits(2); cout << " | . :\n"; } }};voidCodec::build() { bldord = bldseq++; built = true;};boolCodec::set(int v) { value = v; bldord = bldseq++; built = true; bound = true; return built;};boolCodec::set(char* m) { return false;};boolCodec::set(char* p, int l) { frame.set(p,l); bldord = bldseq++; built = true; bound = true; return built;};boolCodec::get(int& v) { if (exists) v = value; return exists;};boolCodec::get(char*& m) { return false;};boolCodec::get(char*& p, int& l) { if (exists) { p = frame.point(); l = (frame.size+7)/8; } return exists;};boolCodec::test(int v) { if (!exists) return false; return value == v;};boolCodec::test(char* m) { return false;};boolCodec::test(char* p, int l) { if (!exists) return false; if (l!=frame.size/8) return false; return memcmp(p,frame.point(),l)==0;}; List::List(char* l, char* t, int s, int c, int b,Codec** a) : Codec(l,t,s,0) { contents = c; branches = b; content = a;};List::List(const List& original) : Codec(original) { contents = original.contents; branches = original.branches;};List*List::newcopy(char* l,char* t) { return new List(l,t,size,contents,branches,content);};List::~List() { int i; for (i=0;i<contents+branches;i++) if (content[i]) delete content[i];};voidList::report() { if (!exists&&!invalid) return; if ((label)&&(title)) { cout.width(12); cout.fill('-'); cout.setf(ios::left,ios::adjustfield); if (invalid) cout << "---BAD"; else cout << "-"; cout.width(25); cout << label; cout.width(41); cout << title << '\n'; } int i; for (i=0;i<contents;i++) content[i]->report();};voidList::decode(FramePointer& p) { if (p.length<p.offset+size) { invalid = true; throw Bounds(label); } frame = p; int i; try { for (i=0;i<contents;i++) content[i]->decode(p); } catch (...) { invalid = true; for (;i<contents;i++) content[i]->invalid = true; frame.size = p.offset - frame.offset; throw; } frame.size = p.offset - frame.offset; exists = true;};voidList::encode(FramePointer& p) { if (p.length<p.offset+size) { invalid = true; throw Bounds(label); } frame = p; int i; for (i=0;i<contents;i++) content[i]->encode(p); frame.size = p.offset - frame.offset; if (!(pass2&&exists)) { if (built) bldseq = bldord; built = false; exists = true; }};Header::Header(char* l, char* t, int s, int c,int b, Codec** a) : List(l,t,s,c,b,a) {};Content::Content(char* l, char* t, int s, int c,int b, Codec** a) : List(l,t,s,c,b,a) {};Parameter::Parameter(char* l, char* t, int s, int c, int b, Codec** a) : List(l,t,s,c,b,a) {};Parameter::Parameter(const Parameter& original) : List(original) {};ParameterR::ParameterR(char* l,char* t,int s,int c,int b,Codec** a) :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -