| 1 | 
 | 
|---|
| 2 | // translate.h -- data translation classes for StateIn and StateOut
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // Copyright (C) 1997 Limit Point Systems, Inc.
 | 
|---|
| 5 | //
 | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
 | 
|---|
| 7 | // Maintainer: LPS
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // This file is part of the SC Toolkit.
 | 
|---|
| 10 | //
 | 
|---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
 | 
|---|
| 12 | // it under the terms of the GNU Library General Public License as published by
 | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option)
 | 
|---|
| 14 | // any later version.
 | 
|---|
| 15 | //
 | 
|---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful,
 | 
|---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 19 | // GNU Library General Public License for more details.
 | 
|---|
| 20 | //
 | 
|---|
| 21 | // You should have received a copy of the GNU Library General Public License
 | 
|---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
 | 
|---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 | 
|---|
| 24 | //
 | 
|---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7.
 | 
|---|
| 26 | //
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #ifndef _util_state_translate_h
 | 
|---|
| 29 | #define _util_state_translate_h
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <scconfig.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #if defined(WORDS_BIGENDIAN)
 | 
|---|
| 36 | #define BIGENDIAN 1
 | 
|---|
| 37 | #else
 | 
|---|
| 38 | #define BIGENDIAN 0
 | 
|---|
| 39 | #endif
 | 
|---|
| 40 | 
 | 
|---|
| 41 | namespace sc {
 | 
|---|
| 42 | 
 | 
|---|
| 43 | /** Generic data translation.
 | 
|---|
| 44 |  */
 | 
|---|
| 45 | class TranslateData {
 | 
|---|
| 46 |   public:
 | 
|---|
| 47 |     TranslateData();
 | 
|---|
| 48 |     virtual ~TranslateData();
 | 
|---|
| 49 | 
 | 
|---|
| 50 |     /// Returns a code for the type of format for the external data.
 | 
|---|
| 51 |     virtual char format_code();
 | 
|---|
| 52 | 
 | 
|---|
| 53 |     /** A virtual constructor that choses a specialization based on
 | 
|---|
| 54 |         the format code. */
 | 
|---|
| 55 |     static TranslateData *vctor(char code);
 | 
|---|
| 56 | 
 | 
|---|
| 57 |     /** Translates to native format in-place.
 | 
|---|
| 58 |         Similar routines exist for all the basic types. */
 | 
|---|
| 59 |     virtual void to_native  (char *,   int n);
 | 
|---|
| 60 |     /** Translates to external format in-place.
 | 
|---|
| 61 |         Similar routines exist for all the basic types. */
 | 
|---|
| 62 |     virtual void to_external(char *,   int n);
 | 
|---|
| 63 |     virtual void to_native  (short *,  int n);
 | 
|---|
| 64 |     virtual void to_external(short *,  int n);
 | 
|---|
| 65 |     virtual void to_native  (unsigned int *, int n);
 | 
|---|
| 66 |     virtual void to_external(unsigned int *, int n);
 | 
|---|
| 67 |     virtual void to_native  (int *,    int n);
 | 
|---|
| 68 |     virtual void to_external(int *,    int n);
 | 
|---|
| 69 |     virtual void to_native  (long *,   int n);
 | 
|---|
| 70 |     virtual void to_external(long *,   int n);
 | 
|---|
| 71 |     virtual void to_native  (float *,  int n);
 | 
|---|
| 72 |     virtual void to_external(float *,  int n);
 | 
|---|
| 73 |     virtual void to_native  (double *, int n);
 | 
|---|
| 74 |     virtual void to_external(double *, int n);
 | 
|---|
| 75 | 
 | 
|---|
| 76 |     /** Translates to native format.
 | 
|---|
| 77 |         Similar routines exist for all the basic types. */
 | 
|---|
| 78 |     virtual void to_native  (char *target,   const void *source,   int n);
 | 
|---|
| 79 |     /** Translates to external format.
 | 
|---|
| 80 |         Similar routines exist for all the basic types. */
 | 
|---|
| 81 |     virtual void to_external(void *target,   const char *source,   int n);
 | 
|---|
| 82 |     virtual void to_native  (short *,  const void *,   int n);
 | 
|---|
| 83 |     virtual void to_external(void *,   const short *,  int n);
 | 
|---|
| 84 |     virtual void to_native  (unsigned int *,    const void *,   int n);
 | 
|---|
| 85 |     virtual void to_external(void *,   const unsigned int *,    int n);
 | 
|---|
| 86 |     virtual void to_native  (int *,    const void *,   int n);
 | 
|---|
| 87 |     virtual void to_external(void *,   const int *,    int n);
 | 
|---|
| 88 |     virtual void to_native  (long *,   const void *,   int n);
 | 
|---|
| 89 |     virtual void to_external(void *,   const long *,   int n);
 | 
|---|
| 90 |     virtual void to_native  (float *,  const void *,   int n);
 | 
|---|
| 91 |     virtual void to_external(void *,   const float *,  int n);
 | 
|---|
| 92 |     virtual void to_native  (double *, const void *,   int n);
 | 
|---|
| 93 |     virtual void to_external(void *,   const double *, int n);
 | 
|---|
| 94 | };
 | 
|---|
| 95 | 
 | 
|---|
| 96 | /** Data translation to an external representation with bytes swapped.
 | 
|---|
| 97 |  */
 | 
|---|
| 98 | class TranslateDataByteSwap: public TranslateData {
 | 
|---|
| 99 |   public:
 | 
|---|
| 100 |     TranslateDataByteSwap();
 | 
|---|
| 101 |     virtual ~TranslateDataByteSwap();
 | 
|---|
| 102 | 
 | 
|---|
| 103 |     /// Returns a code for the type of format for the external data.
 | 
|---|
| 104 |     virtual char format_code();
 | 
|---|
| 105 | 
 | 
|---|
| 106 |     /// Overridden translation routines exist for all the basic types.
 | 
|---|
| 107 |     virtual void to_native  (char *,   int n);
 | 
|---|
| 108 |     /// Overridden translation routines exist for all the basic types.
 | 
|---|
| 109 |     virtual void to_external(char *,   int n);
 | 
|---|
| 110 |     virtual void to_native  (short *,  int n);
 | 
|---|
| 111 |     virtual void to_external(short *,  int n);
 | 
|---|
| 112 |     virtual void to_native  (unsigned int *, int n);
 | 
|---|
| 113 |     virtual void to_external(unsigned int *, int n);
 | 
|---|
| 114 |     virtual void to_native  (int *,    int n);
 | 
|---|
| 115 |     virtual void to_external(int *,    int n);
 | 
|---|
| 116 |     virtual void to_native  (long *,   int n);
 | 
|---|
| 117 |     virtual void to_external(long *,   int n);
 | 
|---|
| 118 |     virtual void to_native  (float *,  int n);
 | 
|---|
| 119 |     virtual void to_external(float *,  int n);
 | 
|---|
| 120 |     virtual void to_native  (double *, int n);
 | 
|---|
| 121 |     virtual void to_external(double *, int n);
 | 
|---|
| 122 | 
 | 
|---|
| 123 |     /// Overridden translation routines exist for all the basic types.
 | 
|---|
| 124 |     virtual void to_native  (char *,   const void *,   int n);
 | 
|---|
| 125 |     /// Overridden translation routines exist for all the basic types.
 | 
|---|
| 126 |     virtual void to_external(void *,   const char *,   int n);
 | 
|---|
| 127 |     virtual void to_native  (short *,  const void *,   int n);
 | 
|---|
| 128 |     virtual void to_external(void *,   const short *,  int n);
 | 
|---|
| 129 |     virtual void to_native  (unsigned int *,    const void *,   int n);
 | 
|---|
| 130 |     virtual void to_external(void *,   const unsigned int *,    int n);
 | 
|---|
| 131 |     virtual void to_native  (int *,    const void *,   int n);
 | 
|---|
| 132 |     virtual void to_external(void *,   const int *,    int n);
 | 
|---|
| 133 |     virtual void to_native  (long *,   const void *,   int n);
 | 
|---|
| 134 |     virtual void to_external(void *,   const long *,   int n);
 | 
|---|
| 135 |     virtual void to_native  (float *,  const void *,   int n);
 | 
|---|
| 136 |     virtual void to_external(void *,   const float *,  int n);
 | 
|---|
| 137 |     virtual void to_native  (double *, const void *,   int n);
 | 
|---|
| 138 |     virtual void to_external(void *,   const double *, int n);
 | 
|---|
| 139 | };
 | 
|---|
| 140 | 
 | 
|---|
| 141 | #if BIGENDIAN
 | 
|---|
| 142 | typedef TranslateDataByteSwap TranslateDataLittleEndian;
 | 
|---|
| 143 | typedef TranslateData TranslateDataBigEndian;
 | 
|---|
| 144 | #else
 | 
|---|
| 145 | typedef TranslateDataByteSwap TranslateDataBigEndian;
 | 
|---|
| 146 | typedef TranslateData TranslateDataLittleEndian;
 | 
|---|
| 147 | #endif
 | 
|---|
| 148 | 
 | 
|---|
| 149 | class StateOut;
 | 
|---|
| 150 | 
 | 
|---|
| 151 | /** Convert data to other formats.
 | 
|---|
| 152 |     The generated data is inserted into a StateOut object.
 | 
|---|
| 153 |  */
 | 
|---|
| 154 | class TranslateDataOut {
 | 
|---|
| 155 |   private:
 | 
|---|
| 156 |     StateOut *so_;
 | 
|---|
| 157 |     TranslateData *translate_;
 | 
|---|
| 158 |     // the translation buffer
 | 
|---|
| 159 |     enum { bufsize = 8192 };
 | 
|---|
| 160 |     char buf_[bufsize];
 | 
|---|
| 161 |   protected:
 | 
|---|
| 162 |     int putv(const void*d,int s);
 | 
|---|
| 163 |   public:
 | 
|---|
| 164 |     /** Write to s using the translation defined by t.
 | 
|---|
| 165 |         The t argument will be deleted by this. */
 | 
|---|
| 166 |     TranslateDataOut(StateOut*s, TranslateData*t);
 | 
|---|
| 167 |     virtual ~TranslateDataOut();
 | 
|---|
| 168 | 
 | 
|---|
| 169 |     /** Translate and write the data. A similar member exists for
 | 
|---|
| 170 |         each basic type. */
 | 
|---|
| 171 |     virtual int put(const char*,int);
 | 
|---|
| 172 |     virtual int put(const short*,int);
 | 
|---|
| 173 |     virtual int put(const unsigned int*,int);
 | 
|---|
| 174 |     virtual int put(const int*,int);
 | 
|---|
| 175 |     virtual int put(const long*,int);
 | 
|---|
| 176 |     virtual int put(const float*,int);
 | 
|---|
| 177 |     virtual int put(const double*,int);
 | 
|---|
| 178 | 
 | 
|---|
| 179 |     /// Returns the translator.
 | 
|---|
| 180 |     TranslateData *translator() { return translate_; }
 | 
|---|
| 181 | };
 | 
|---|
| 182 | 
 | 
|---|
| 183 | class StateIn;
 | 
|---|
| 184 | 
 | 
|---|
| 185 | /** Convert data from other formats.
 | 
|---|
| 186 |     The data is taken from a StateIn object.
 | 
|---|
| 187 |  */
 | 
|---|
| 188 | class TranslateDataIn {
 | 
|---|
| 189 |   private:
 | 
|---|
| 190 |     StateIn *si_;
 | 
|---|
| 191 |     TranslateData *translate_;
 | 
|---|
| 192 |   protected:
 | 
|---|
| 193 |     int getv(void*d,int s);
 | 
|---|
| 194 |   public:
 | 
|---|
| 195 |     /** Input data will come from s.  The t argument will be deleted by this.
 | 
|---|
| 196 |      */
 | 
|---|
| 197 |     TranslateDataIn(StateIn*s, TranslateData *t);
 | 
|---|
| 198 |     virtual ~TranslateDataIn();
 | 
|---|
| 199 | 
 | 
|---|
| 200 |     /** Read and translate data.  A similar member exists for each basic
 | 
|---|
| 201 |         type. */
 | 
|---|
| 202 |     virtual int get(char*,int);
 | 
|---|
| 203 |     virtual int get(short*,int);
 | 
|---|
| 204 |     virtual int get(unsigned int*,int);
 | 
|---|
| 205 |     virtual int get(int*,int);
 | 
|---|
| 206 |     virtual int get(long*,int);
 | 
|---|
| 207 |     virtual int get(float*,int);
 | 
|---|
| 208 |     virtual int get(double*,int);
 | 
|---|
| 209 | 
 | 
|---|
| 210 |     /// Return the translator.
 | 
|---|
| 211 |     TranslateData *translator() { return translate_; }
 | 
|---|
| 212 | };
 | 
|---|
| 213 | 
 | 
|---|
| 214 | }
 | 
|---|
| 215 | 
 | 
|---|
| 216 | #endif
 | 
|---|
| 217 | 
 | 
|---|
| 218 | // Local Variables:
 | 
|---|
| 219 | // mode: c++
 | 
|---|
| 220 | // c-file-style: "CLJ"
 | 
|---|
| 221 | // End:
 | 
|---|