Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

func.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <stdexcept>
00003 #include "func.h"
00004 #include "element.h"
00005 
00006 static enum ByteOrder byteOrder = LittleEndian;
00007 
00008 // set byte order for following structure
00009 void setByteOrder(enum ByteOrder bo)
00010 {
00011         byteOrder = bo;
00012 }
00013 
00014 //enum ByteOrder { LittleEndian, BigEndian };
00015 //enum ComplexType { Union, Struct };
00016 
00017 // create a list of element adding a single element
00018 Elements* createElements(Element* element)
00019 {
00020         Elements* e = new Elements();
00021         e->Add(element);
00022         return e;
00023 }
00024 
00025 // add an element to a list of elements
00026 Elements* addElement(Elements* elements,Element* element)
00027 {
00028         elements->Add(element);
00029         return elements;
00030 }
00031 
00032 // create a complex element
00033 Element* createElement(enum ComplexType type,Elements* elements,char* name)
00034 {
00035         Element *e = new ComplexElement(name,type,elements);
00036         free(name);
00037         return e;
00038 }
00039 
00040 // read a number and free it
00041 // return 0 if NULL pointer
00042 static int readNum(char* number)
00043 {
00044         int n = 0;
00045         if (number)
00046         {
00047                 n = atoi(number);
00048                 free(number);
00049         }
00050         return n;
00051 }
00052 
00053 // create an element given an already defined complex type
00054 // number can be NULL
00055 Element* createDefinedElement(char* type,char* name,char* number)
00056 {
00057         int n = readNum(number);
00058         
00059         // find complex element
00060         ComplexElement *ce = ComplexElement::Find(type);
00061         free(type);
00062         
00063         // !!! give some error
00064         if (!ce)
00065                 throw std::runtime_error("");
00066 
00067         return new DefinedElement(name, ce, n);
00068 }
00069 
00070 // create a simple element given size (1,2,4,8 negative if 
00071 // signed and 0 padding)
00072 // number can be NULL
00073 Element* createSimpleElement(int type,char* name,char* number)
00074 {
00075         int n = readNum(number);
00076         
00077         Element* res;
00078         if (type==0)
00079                 res = new PaddingElement(name,n==0?1:n);
00080         else
00081                 res = new SimpleElement(name,type<0,type<0?-type:type,n);
00082         free(name);
00083         return res;
00084 }
00085 
00086 // process a complex type an convert in C
00087 void processElement(enum ComplexType type,char* name,Elements* elements)
00088 {
00089         ComplexElement *ce = new ComplexElement(name,type,elements);
00090         free(name);
00091         
00092         ce->Process(cout);
00093 }
00094 

Generated on Mon Jan 13 22:20:34 2003 for perdr by doxygen1.2.15