~ubuntu-branches/debian/experimental/cuneiform/experimental

« back to all changes in this revision

Viewing changes to cuneiform_src/Kern/hh/xmemfile.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2009-07-10 17:58:10 UTC
  • Revision ID: james.westby@ubuntu.com-20090710175810-rqc89d2i3tki9m89
Tags: upstream-0.7.0+dfsg
Import upstream version 0.7.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 1993-2008, Cognitive Technologies
 
3
All rights reserved.
 
4
 
 
5
����������� ��������� ��������������� � ������������� ��� � ���� ��������� ����,
 
6
��� � � �������� �����, � ����������� ��� ���, ��� ���������� ��������� �������:
 
7
 
 
8
      * ��� ��������� ��������������� ��������� ���� ������ ���������� ���������
 
9
        ���� ����������� �� ��������� �����, ���� ������ ������� � �����������
 
10
        ����� �� ��������.
 
11
      * ��� ��������� ��������������� ��������� ���� � ������������ �/��� �
 
12
        ������ ����������, ������������ ��� ���������������, ������ �����������
 
13
        ��������� ���� ���������� �� ��������� �����, ���� ������ ������� �
 
14
        ����������� ����� �� ��������.
 
15
      * �� �������� Cognitive Technologies, �� ����� �� ����������� �� �����
 
16
        ���� ������������ � �������� �������� ��������� �/��� �����������
 
17
        ���������, ���������� �� ���� ��, ��� ���������������� �����������
 
18
        ����������.
 
19
 
 
20
��� ��������� ������������� ����������� ��������� ���� �/��� ������� ������ "���
 
21
��� ����" ��� ������-���� ���� ��������, ���������� ���� ��� ���������������,
 
22
������� �������� ������������ �������� � ����������� ��� ���������� ����, �� ��
 
23
������������� ���. �� �������� ��������� ���� � �� ���� ������ ����, �������
 
24
����� �������� �/��� �������� �������������� ���������, �� � ���� ������ ��
 
25
��Ѩ� ���������������, ������� ����� �����, ���������, ����������� ���
 
26
������������� ������, ��������� � �������������� ��� ���������� ����������
 
27
������������� ������������� ��������� (������� ������ ������, ��� ������,
 
28
������� ���������, ��� ������ �/��� ������ �������, ���������� ��-�� ��������
 
29
������� ��� �/��� ������ ��������� �������� ��������� � ������� �����������,
 
30
�� �� ������������� ����� ��������), �� �� ������������� ���, ���� ���� �����
 
31
�������� ��� ������ ���� ���� �������� � ����������� ����� ������� � ������.
 
32
 
 
33
Redistribution and use in source and binary forms, with or without modification,
 
34
are permitted provided that the following conditions are met:
 
35
 
 
36
    * Redistributions of source code must retain the above copyright notice,
 
37
      this list of conditions and the following disclaimer.
 
38
    * Redistributions in binary form must reproduce the above copyright notice,
 
39
      this list of conditions and the following disclaimer in the documentation
 
40
      and/or other materials provided with the distribution.
 
41
    * Neither the name of the Cognitive Technologies nor the names of its
 
42
      contributors may be used to endorse or promote products derived from this
 
43
      software without specific prior written permission.
 
44
 
 
45
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
46
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
47
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
48
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 
49
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
50
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
51
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
52
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
53
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
54
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
55
*/
 
56
 
 
57
/////////////////////////////////////////////////////////////////
 
58
// class XMemFile - utility to put/get misc data into binary pool,
 
59
// read/write it to disk
 
60
//
 
61
// 28.05.97 22:32, Postnikov
 
62
/////////////////////////////////////////////////////////////////
 
63
#ifndef __XMEMFILE_H
 
64
   #define __XMEMFILE_H
 
65
 
 
66
   #ifndef __XPOOL_H
 
67
   #  include "xpool.h"
 
68
   #endif
 
69
 
 
70
   #ifndef __SWABYTES_H
 
71
   #  include "swabytes.h"
 
72
   #endif
 
73
 
 
74
class XMemFile : public XPool
 
75
{
 
76
   Int32    nFileLength;  // count of bytes in use (put into)
 
77
   Word8*   pCur;        // current ptr
 
78
 
 
79
public:
 
80
   XMemFile( Int32 init_size = 0 )
 
81
      : XPool(init_size)
 
82
      { pCur=(Word8*)Data; nFileLength = 0; };
 
83
 
 
84
   Int32    Tell()
 
85
      { return pCur - (Word8*)Data; };
 
86
 
 
87
   Int32    FileLength()
 
88
      { return nFileLength; };
 
89
 
 
90
   void    RestoreFileLength(Int32 len) // Oleg : knot for second recongition
 
91
      { nFileLength=len; };
 
92
 
 
93
   Bool     Seek( Int32 nOffset )
 
94
      {  if ((nOffset > nFileLength)||(nOffset < 0))
 
95
            RET_FALSE;
 
96
         pCur= ((Word8*)Data)+nOffset;
 
97
         return TRUE;
 
98
      };
 
99
 
 
100
   void     SeekToStart( void ) {  pCur= ((Word8*)Data); };
 
101
   void     SeekToFinish( void ) {  pCur= ((Word8*)Data)+nFileLength; };
 
102
 
 
103
   void     Reset() { nFileLength = 0; SeekToStart(); };
 
104
 
 
105
   Bool     Get( void* data, Int32 size )
 
106
      {
 
107
         if (size < 0 || size+Tell() > nFileLength)
 
108
            RET_FALSE;
 
109
         if (size==0)
 
110
            return TRUE;
 
111
         stdMemcpy(data, pCur, size);
 
112
         pCur+=size;
 
113
         return TRUE;
 
114
      };
 
115
 
 
116
   Bool     Writeln( const char* str )
 
117
      {
 
118
         return Put(str, strlen(str)+1);
 
119
      };
 
120
   char*    Readln(void) // be sure that there is zt string!
 
121
      {
 
122
         int nRest = nFileLength - Tell();
 
123
         if (nRest <=0)
 
124
            return NULL;
 
125
         char* pNext = (char*)memchr(pCur, 0, nRest);
 
126
         if (pNext==NULL)
 
127
            return NULL;
 
128
         char* ret = (char*)pCur;
 
129
         pCur = (Word8*)pNext; pCur++;
 
130
         return ret;
 
131
      };
 
132
 
 
133
   Bool     Reserve( Int32 size )
 
134
      {
 
135
         if (size < 0)
 
136
            RET_FALSE; // too strange...
 
137
         Int32 tell = Tell();
 
138
         if (size+tell <= Volume)
 
139
            return TRUE; // there is enough place
 
140
 
 
141
         Int32 new_size = maxi(size+tell, Volume*2);
 
142
         if (!Realloc( new_size ))
 
143
         {
 
144
            if (   size+tell < Volume*2  &&
 
145
                  !Realloc(size+tell )
 
146
               )
 
147
            RET_FALSE;  // can't realloc
 
148
         }
 
149
         Seek(tell);
 
150
         return TRUE;
 
151
      }
 
152
 
 
153
   Bool     Put( const void* data, Int32 size )
 
154
      {
 
155
         if (size==0)
 
156
            return TRUE;
 
157
         if (!Reserve(size)) // prepeare place to put
 
158
            RET_FALSE;
 
159
         // put
 
160
         stdMemcpy(pCur, (void*)data, size);
 
161
         pCur+=size;
 
162
         nFileLength += size;
 
163
         return TRUE;
 
164
      };
 
165
 
 
166
   // TODO: optimize!
 
167
   Bool     Get( Int32& t ) { return Get( &t, sizeof(Int32) ); }
 
168
   Bool     Put( Int32& t ) { return Put( &t, sizeof(Int32) ); }
 
169
   Bool     Get( Int16& t ) { return Get( &t, sizeof(Int16) ); }
 
170
   Bool     Put( Int16& t ) { return Put( &t, sizeof(Int16) ); }
 
171
   Bool     Get( Int8& t ) { return Get( &t, sizeof(Int8) ); }
 
172
   Bool     Put( Int8& t ) { return Put( &t, sizeof(Int8) ); }
 
173
   Bool     Get( Word32& t ) { return Get( &t, sizeof(Word32) ); }
 
174
   Bool     Put( Word32& t ) { return Put( &t, sizeof(Word32) ); }
 
175
   Bool     Get( Word16& t ) { return Get( &t, sizeof(Word16) ); }
 
176
   Bool     Put( Word16& t ) { return Put( &t, sizeof(Word16) ); }
 
177
   Bool     Get( Word8& t ) { return Get( &t, sizeof(Word8) ); }
 
178
   Bool     Put( Word8& t ) { return Put( &t, sizeof(Word8) ); }
 
179
   Bool     Get( Rect16& t ) { return Get( &t, sizeof(Rect16) ); }
 
180
   Bool     Put( Rect16& t ) { return Put( &t, sizeof(Rect16) ); }
 
181
   Bool     Get( Point16& t ) { return Get( &t, sizeof(Point16) ); }
 
182
   Bool     Put( Point16& t ) { return Put( &t, sizeof(Point16) ); }
 
183
   Bool     Get( Rect32& t ) { return Get( &t, sizeof(Rect32) ); }
 
184
   Bool     Put( Rect32& t ) { return Put( &t, sizeof(Rect32) ); }
 
185
   Bool     Get( Point32& t ) { return Get( &t, sizeof(Point32) ); }
 
186
   Bool     Put( Point32& t ) { return Put( &t, sizeof(Point32) ); }
 
187
   // end optimize
 
188
/*
 
189
   template <class T>
 
190
   Bool     Get( T& t ) { return Get( &t, sizeof(T) ); }
 
191
 
 
192
   template <class T>
 
193
   Bool     Put( T& t ) { return Put( &t, sizeof(T) ); }
 
194
*/
 
195
   Bool     GetArray( void* p_start, Int32& nCount, Int32 cbElemSize )
 
196
      {  if (!Get( nCount ))
 
197
            RET_FALSE;
 
198
         if (!Get( p_start, nCount*cbElemSize ))
 
199
            RET_FALSE;
 
200
         return TRUE;
 
201
      };
 
202
 
 
203
   Bool     PutArray( void* p_start, Int32 nCount, Int32 cbElemSize )
 
204
      {  if (!Put( nCount ))
 
205
            RET_FALSE;
 
206
         if (!Put( p_start, nCount*cbElemSize ))
 
207
            RET_FALSE;
 
208
         return TRUE;
 
209
      };
 
210
 
 
211
   void     Destroy() { XPool::Destroy(); pCur = NULL; nFileLength = 0; };
 
212
   /////////////////////////////////////////////////////
 
213
   // Disk IO
 
214
   Err16    Read( XFile & xf, Bool32 swap_bytes = FALSE )
 
215
      {
 
216
         RETIFERR( XPool::Read(xf, swap_bytes) );
 
217
         nFileLength = XPool::GetVolume();
 
218
         SeekToStart();
 
219
         return ER_NONE;
 
220
      };
 
221
 
 
222
   Bool     Write( XFile & xf ) const
 
223
      {
 
224
         if(!XPool::Write(xf, nFileLength))   RET_FALSE;
 
225
         return TRUE;
 
226
      }
 
227
 
 
228
   /////////////////////////////////////////////////////
 
229
   // Exchange
 
230
   Bool CopyFrom( XMemFile& src )
 
231
   {
 
232
      Destroy();
 
233
      if (!XPool::CopyFrom( *(XPool*)&src ))
 
234
         return FALSE;
 
235
 
 
236
      nFileLength = src.FileLength();
 
237
      Seek( src.Tell() );
 
238
      return TRUE;
 
239
   }
 
240
};
 
241
 
 
242
#endif // __XMEMFILE_H
 
243