Changeset 254 for trunk/src/mesh


Ignore:
Timestamp:
Mar 2, 2006 9:07:05 AM (18 years ago)
Author:
cxsong
Message:

modified serialization/deserializaiton functions, added xmlString, print, capacity, etc.

Location:
trunk/src/mesh
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/mesh/grid1d.cc

    r185 r254  
    1313RpGrid1d::RpGrid1d(int size)
    1414{
    15         m_data.resize(size);
     15        m_data.reserve(size);
    1616}
    1717
     
    4141}
    4242
     43//
     44// Add all points to grid1d object at once
     45//
     46RP_ERROR
     47RpGrid1d::addAllPoints(double* val, int nitems)
     48{
     49        if (val == NULL)
     50                return RP_ERR_NULL_PTR;
     51
     52        if (nitems != numPoints())
     53                m_data.resize(nitems);
     54
     55        for (int i=0; i < nitems; i++)
     56                m_data[i] = val[i];
     57
     58        return RP_SUCCESS;
     59}
     60
     61//
     62// add one point to grid1d
     63//
    4364void RpGrid1d::addPoint(double val)
    4465{
     
    5576//
    5677char*
    57 RpGrid1d::serialize(RP_ENCODE_ALG encodeFlag, RP_COMPRESSION compressFlag)
     78RpGrid1d::serialize(int& nb, RP_ENCODE_ALG encodeFlag,
     79                                RP_COMPRESSION compressFlag)
    5880{
    5981        int npts = m_data.size();
     82        int nbytes = npts*sizeof(double) + sizeof(int) + 2;
     83        char* buf;
    6084
    6185        // total length = tagEncode + tagCompress + num + array data
    62         char * buf = (char*) malloc(npts*sizeof(double) + sizeof(int) + 2);
    63         if (buf == NULL) {
     86        if ( (buf = (char*) malloc(nbytes)) == NULL) {
    6487                RpAppendErr("RpGrid1d::serialize: malloc failed");
    6588                RpPrintErr();
     
    6790        }
    6891
     92        serialize(buf, nbytes, encodeFlag, compressFlag);
     93        nb = nbytes;
     94
     95        return buf;
     96}
     97
     98RP_ERROR
     99RpGrid1d::serialize(char* buf, int nbytes, RP_ENCODE_ALG encodeFlag,
     100                RP_COMPRESSION compressFlag)
     101{
     102        int npts = m_data.size();
     103
     104        if (buf == NULL || (unsigned)nbytes < (npts*sizeof(double)+sizeof(int)+2)) {
     105                RpAppendErr("RpGrid1d::serialize: invalid buffer");
     106                RpPrintErr();
     107                return RP_ERR_INVALID_ARRAY;
     108        }
     109       
    69110        char* ptr = buf;
    70        
    71111        ptr[0] = 'N'; // init to no-encoding
    72112        switch(encodeFlag) {
     
    88128                        break;
    89129        }
     130        ptr += 2; // advance pointer
    90131
    91132        // TODO encode, compression
    92         //
     133        // compress()
    93134
    94135        // write to stream buffer
    95         ptr += 2;
    96136        memcpy((void*)ptr, (void*)&npts, sizeof(int));
    97137        ptr += sizeof(int);
    98138        memcpy((void*)ptr, (void*)&(m_data[0]), npts*sizeof(double));
    99139
    100         return buf;
    101 }
    102 
    103 int
     140        return RP_SUCCESS;
     141}
     142
     143RP_ERROR
    104144RpGrid1d::deserialize(const char* buf)
    105145{
     
    134174}
    135175
    136 
    137 //TODO: overload operators [], =
     176void
     177RpGrid1d::xmlString(std::string& textString)
     178{
     179        int i;
     180        int npts = m_data.size();
     181        char cstr[256];
     182        std::string str;
     183
     184        // clear input string
     185        textString.erase();
     186
     187        textString.append("<value>");
     188
     189        for (i=0; i < npts; i++) {
     190                sprintf(cstr, "\t%.15f\n", m_data[i]);
     191                textString.append(cstr);
     192        }
     193        textString.append("</value>\n");
     194}
     195
     196void
     197RpGrid1d::print()
     198{
     199        string str;
     200        xmlString(str);
     201        printf("%s", str.c_str());
     202}
     203
    138204
    139205// TODO
  • trunk/src/mesh/grid1d.h

    r185 r254  
    1717        RpGrid1d(const char* buf); // instantiate with byte stream
    1818
     19        // add all points to grid
     20        RP_ERROR addAllPoints(double* val, int nitems);
     21
     22        // add one point to grid
    1923        void addPoint(double val);
    2024
     
    2226        int numPoints() { return m_data.size(); }
    2327
     28        // max num points that can be stored
     29        int capacity() { return m_data.capacity(); }
     30
    2431        // change the size of the grid after grid is constructed
    2532        void resize(int npoints) { m_data.resize(npoints); }
    2633
    2734        // serialize data
    28         char * serialize(RP_ENCODE_ALG eflag=RP_NO_ENCODING,
     35        // If no input parameters are provided, byte stream will be binary
     36        // without compression.
     37        //
     38        char * serialize(int& nbytes, RP_ENCODE_ALG eflag=RP_NO_ENCODING,
    2939                         RP_COMPRESSION cflag=RP_NO_COMPRESSION);
    30         int deserialize(const char* buf);
     40
     41        RP_ERROR serialize(char * buf, int nbytes,
     42                        RP_ENCODE_ALG eflag=RP_NO_ENCODING,
     43                        RP_COMPRESSION cflag=RP_NO_COMPRESSION);
     44
     45        RP_ERROR deserialize(const char* buf);
     46
     47        void xmlString(std::string& str);
     48        void print();
    3149
    3250        // destructor
Note: See TracChangeset for help on using the changeset viewer.