common_bundle.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : common bundler definitions                                        *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 2007-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include "common_bundle.h"
00016 
00017 #include <basis/byte_array.h>
00018 #include <basis/packable.h>
00019 #include <basis/set.cpp>
00020 #include <opsystem/byte_filer.h>
00021 
00022 manifest_chunk::~manifest_chunk()
00023 {}
00024 
00025 void manifest_chunk::pack(byte_array &target) const
00026 {
00027   basis::obscure_attach(target, _size);
00028   _target.pack(target);
00029   basis::attach(target, _flags);
00030   _parms.pack(target);
00031   _keywords.pack(target);
00032   target += _timestamp;
00033 }
00034 
00035 bool manifest_chunk::unpack(byte_array &source)
00036 {
00037   if (!basis::obscure_detach(source, _size)) return false;
00038   if (!_target.unpack(source)) return false;
00039   if (!basis::detach(source, _flags)) return false;
00040   if (!_parms.unpack(source)) return false;
00041   if (!_keywords.unpack(source)) return false;
00042   if (source.length() < 8) return false;
00043   _timestamp = source.subarray(0, 7);
00044   source.zap(0, 7);
00045   return true;
00046 }
00047 
00048 bool manifest_chunk::read_an_int(byte_filer &bundle, int &found)
00049 {
00050   FUNCDEF("read_an_int");
00051   byte_array temp;
00052   if (bundle.read(temp, sizeof(int)) != sizeof(int)) return false;
00053   if (!basis::detach(temp, found)) return false;
00054   return true;
00055 }
00056 
00057 bool manifest_chunk::read_an_obscured_int(byte_filer &bundle, int &found)
00058 {
00059   FUNCDEF("read_an_obscured_int");
00060   byte_array temp;
00061   if (bundle.read(temp, 2 * sizeof(int)) != 2 * sizeof(int)) return false;
00062   if (!basis::obscure_detach(temp, found)) return false;
00063   return true;
00064 }
00065 
00066 bool manifest_chunk::read_a_timestamp(byte_filer &bundle, byte_array &found)
00067 {
00068   FUNCDEF("read_a_timestamp");
00069   byte_array temp;
00070   if (bundle.read(temp, 8) != 8) return false;
00071   found = temp;
00072   return true;
00073 }
00074 
00075 istring manifest_chunk::read_a_string(byte_filer &bundle)
00076 {
00077   FUNCDEF("read_a_string");
00078   istring found;
00079   byte_array temp;
00080   // read in the zero-terminated character string.
00081   while (!bundle.eof()) {
00082     // read a single byte out of the file.
00083     if (bundle.read(temp, 1) <= 0)
00084       break;
00085     if (temp[0]) {
00086       // add the byte to the string we're accumulating.
00087       found += temp[0];
00088     } else {
00089       // this string is done now.
00090       break;
00091     }
00092   }
00093   return found;
00094 }
00095 
00096 bool manifest_chunk::read_manifest(byte_filer &bundle, manifest_chunk &curr)
00097 {
00098   curr._size = 0;
00099   bool worked = read_an_obscured_int(bundle, curr._size);
00100   if (!worked)
00101     return false;
00102   byte_array temp;
00103   curr._target = read_a_string(bundle);
00104   if (!curr._target) return false;  
00105   worked = read_an_int(bundle, curr._flags);
00106   if (!worked)
00107     return false;
00108   curr._parms = read_a_string(bundle);
00109     // it's valid for the _parms to be empty.
00110 //if (curr._parms.length()) { printf("parms len=%d are: \"%s\"\n", curr._parms.length(), curr._parms.s()); }
00111   // now get the keywords list, if it exists.
00112   int key_elems = 0;  // number of keywords.
00113   worked = read_an_obscured_int(bundle, key_elems);  // get number of elements.
00114   if (!worked)
00115     return false;
00116   curr._keywords.reset();
00117   for (int i = 0; i < key_elems; i++) {
00118     istring found = read_a_string(bundle);
00119     if (!found) return false;  // not allowed an empty keyword.
00120     curr._keywords += found;
00121   }
00122   worked = read_a_timestamp(bundle, curr._timestamp);
00123   return worked;
00124 }
00125 

Generated on Fri Nov 28 04:28:49 2008 for HOOPLE Libraries by  doxygen 1.5.1