22#ifndef __LIBMSWRITE_DEFS_H__
23#define __LIBMSWRITE_DEFS_H__
29#include "config.libmswrite.h"
35 extern const char *Version;
45 typedef unsigned char Byte;
46 typedef unsigned short Word;
47 typedef signed short Short;
53 typedef unsigned int DWord;
54 typedef signed int Long;
56 typedef unsigned long DWord;
57 typedef signed long Long;
67 static const int Ok = 0;
68 static const int Warn = 1;
69 static const int InvalidFormat = 2;
70 static const int OutOfMemory = 3;
71 static const int InternalError = 4;
72 static const int Unsupported = 5;
73 static const int FileError = 6;
75 static const int LastOne = 255;
92 #define Twip2Point(val) ((val) / 20)
93 #define Point2Twip(val) ((val) * 20)
95 #define Twip2Inch(val) ((val) / 1440)
96 #define Inch2Twip(val) ((val) * 1440)
98 #define Point2Inch(val) ((val) / 72)
99 #define Inch2Point(val) ((val) * 72)
101 #define Twip2Milli(val) ((val) / 56.6929)
102 #define Milli2Twip(val) ((val) * 56.6929)
104 #define Point2Milli(val) ((val) / 2.83465)
105 #define Milli2Point(val) ((val) * 2.83465)
107 #define Inch2Mill(val) ((val) * 25.4)
108 #define Milli2Inch(val) ((val) / 25.4)
115 inline Byte ReadByte (Byte &dest, const Byte *src)
120 inline Word ReadWord (Word &dest, const Byte *src)
122 return dest = ((Word) src [0]) |
123 (((Word) src [1]) << 8);
126 inline Short ReadShort (Short &dest, const Byte *src)
128 return (Short) ReadWord ((Word &) dest, src);
131 inline DWord ReadDWord (DWord &dest, const Byte *src)
133 return dest = ((DWord) src [0]) |
134 (((DWord) src [1]) << 8) |
135 (((DWord) src [2]) << 16) |
136 (((DWord) src [3]) << 24);
139 inline Long ReadLong (Long &dest, const Byte *src)
141 return (Long) ReadDWord ((DWord &) dest, src);
144 inline Byte WriteByte ( const Byte &src, Byte *dest)
150 inline Word WriteWord ( const Word &src, Byte *dest)
152 dest [0] = (Byte) (src & 255);
153 dest [1] = (Byte) (src >> 8);
157 inline Short WriteShort ( const Short &src, Byte *dest)
159 WriteWord ((Word) src, dest);
163 inline DWord WriteDWord ( const DWord &src, Byte *dest)
165 dest [0] = (Byte) (src & 255);
166 dest [1] = (Byte) ((src >> 8) & 255);
167 dest [2] = (Byte) ((src >> 16) & 255);
168 dest [3] = (Byte) (src >> 24);
172 inline Long WriteLong ( const Long &src, Byte *dest)
174 WriteDWord ((DWord) src, dest);
194 #define ReadBitsFromByte(destbits,srcbyte,start,len) \
195 ((destbits)=((srcbyte)>>(start))&(BitMask##len))
197 #define WriteBitsToByte(srcbits,destbyte,start,len) \
198 ((destbyte)|=((Byte(srcbits)&(BitMask##len))<<(start)))
209 static const int MaxCacheDepth = 32;
211 Byte *m_cache [MaxCacheDepth];
214 static const int MaxDebugLen = 1024;
215 char m_debugTemp [MaxDebugLen];
221 Device () : m_posInternal (0), m_cacheCount (0), m_error (0)
251 m_cache [m_cacheCount] = cache;
253 if (m_cacheCount > MaxCacheDepth)
255 error (Error::InternalError, "too many caches\n");
262 if (m_cacheCount < 0)
264 error (Error::InternalError, "too few caches\n");
272 bool readInternal (Byte *buf, const long numBytes)
278 memcpy (buf, m_cache [m_cacheCount - 1], numBytes);
279 m_cache [m_cacheCount - 1] += numBytes;
284 ret = read (buf, numBytes);
285 if (ret) m_posInternal += numBytes;
291 bool writeInternal ( const Byte *buf, const long numBytes)
297 memcpy (m_cache [m_cacheCount - 1], buf, numBytes);
298 m_cache [m_cacheCount - 1] += numBytes;
303 ret = write (buf, numBytes);
304 if (ret) m_posInternal += numBytes;
310 bool seekInternal ( const long offset, const int whence)
312 bool ret = seek (offset, whence);
319 m_posInternal = offset;
322 m_posInternal += offset;
325 m_posInternal = tell ();
333 long tellInternal ( void) const
336 return m_posInternal;
344 void debug ( const char *s, const int i)
346 snprintf (m_debugTemp, MaxDebugLen - 1, "%s%i\n", s, i);
347 m_debugTemp [MaxDebugLen - 1] = 0;
350 void debug ( const char *s1, const char *s2)
352 snprintf (m_debugTemp, MaxDebugLen - 1, "%s%s\n", s1, s2);
353 m_debugTemp [MaxDebugLen - 1] = 0;
357 void debug ( const char *s1, const Byte *s2)
359 debug (s1, ( const char *) s2);
362 #define Dump(name) m_device->debug("\t" #name ": ", m_##name)
364 bool good ( void) const
385 virtual bool read (Byte *buf, const DWord numBytes) = 0;
386 virtual bool write ( const Byte *buf, const DWord numBytes) = 0;
387 virtual bool seek ( const long offset, const int whence) = 0;
388 virtual long tell ( void) = 0;
389 virtual void debug ( const char *s)
391 fprintf (stderr, "%s", s);
393 virtual void debug ( const int i)
395 fprintf (stderr, "%i", i);
397 static const DWord NoToken = DWord (0xABCD1234);
398 virtual void error ( const int errorCode, const char *message,
399 const char *file = "", const int lineno = 0,
400 DWord token = NoToken)
403 if (errorCode != Error::Warn)
407 fprintf (stderr, "%s:%i:", file, lineno);
410 if (token == NoToken)
411 fprintf (stderr, "%s\n", message);
413 fprintf (stderr, "%s (val=%li)\n", message, long (token));
416 #define ErrorAndQuit(errorCode,message) \
418 m_device->error(errorCode,message); \
422 #define Verify(errorCode,expr,token) \
423 ((expr)? true : (m_device->error(errorCode, "check \'" #expr "\' failed",__FILE__,__LINE__,token),m_device->good ()))
445 bool read (Byte * , const DWord )
447 error (Error::InternalError, "memory device not reading from memory?\n");
450 bool write ( const Byte * , const DWord )
452 error (Error::InternalError, "memory device not writing to memory?\n");
455 bool seek ( const long , const int )
457 error (Error::InternalError, "memory device cannot seek full stop!\n");
462 error (Error::InternalError, "memory device not accessing memory?\n");
473 NeedsDevice (Device *device = NULL)
478 virtual ~NeedsDevice ()
482 void setDevice (Device * const device)
488 #ifdef CHECK_INTERNAL
489 #define CHECK_DEVICE_ERROR fprintf (stderr, "%s:%i: INTERNAL ERROR - device not set\n", __FILE__, __LINE__)
490 #define CHECK_DEVICE \
493 CHECK_DEVICE_ERROR; \
497 #define CHECK_DEVICE_ERROR
502 class UseThisMuchPrefixSize
508 UseThisMuchPrefixSize ( const int val = 0)
513 ~UseThisMuchPrefixSize ()
517 bool operator== ( const UseThisMuchPrefixSize &rhs)
519 return m_val == rhs.m_val;
522 UseThisMuchPrefixSize &operator= ( const UseThisMuchPrefixSize &rhs)
531 int getVal ( void) const { return m_val; }
532 void setVal ( const int val) { m_val = val; }
539 List <UseThisMuchPrefixSize> m_notDefaultBits;
542 UseThisMuch &operator= ( const UseThisMuch &rhs)
547 this->m_notDefaultBits = rhs.m_notDefaultBits;
551 void signalHaveSetData ( const bool isDefault, const int needNumBits)
556 List <UseThisMuchPrefixSize>::Iterator it = m_notDefaultBits.search (needNumBits);
557 if (it != m_notDefaultBits.end ()) m_notDefaultBits.erase (it);
562 List <UseThisMuchPrefixSize>::Iterator it = m_notDefaultBits.search (needNumBits);
565 if (it == m_notDefaultBits.end ())
567 UseThisMuchPrefixSize utmps (needNumBits);
568 m_notDefaultBits.addToBack (utmps);
574 int getNeedNumDataBytes ( void) const
577 List <UseThisMuchPrefixSize>::Iterator it;
578 for (it = m_notDefaultBits.begin (); it != m_notDefaultBits.end (); it++)
580 if ((*it).getVal () > biggest)
581 biggest = (*it).getVal ();
585 return biggest / 8 + 1;
595 virtual ~UseThisMuch ()
Device that either reads or writes from/to .WRI files.
virtual bool read(Byte *buf, const DWord numBytes)=0 Functions that the user must implement.
bool setCache(Byte *const cache) setCache specifies whether or not Device should start reading from memory block specified by
Device capable only of reading/writing to memory blocks (used internally by LibMSWrite)
bool read(Byte *, const DWord) Functions that the user must implement.
|