1 /**
2  * @namespace   biew
3  * @file        bmfile.c
4  * @brief       This file has developed as Buffered stream Manager and presents
5  *              front end interface to bbio library.
6  * @version     -
7  * @remark      this source file is part of Binary vIEW project (BIEW).
8  *              The Binary vIEW (BIEW) is copyright (C) 1995 Nickols_K.
9  *              All rights reserved. This software is redistributable under the
10  *              licence given in the file "Licence.en" ("Licence.ru" in russian
11  *              translation) distributed in the BIEW archive.
12  * @note        Requires POSIX compatible development system
13  *
14  * @author      Nickols_K
15  * @since       1995
16  * @note        Development, fixes and improvements
17 **/
18 #include <limits.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22 
23 #include "bmfile.h"
24 #include "bconsole.h"
25 #include "tstrings.h"
26 #include "biewlib/bbio.h"
27 
28 unsigned BMFileFlags=0;
29 extern tBool fioUseMMF;
30 BGLOBAL bm_file_handle = NULL,sc_bm_file_handle = NULL;
31 
biewOpenRO(char * fname,unsigned cache_size)32 BGLOBAL __FASTCALL__ biewOpenRO(char *fname,unsigned cache_size)
33 {
34   BGLOBAL fret;
35   fret = bioOpen(fname,FO_READONLY | SO_DENYNONE,cache_size,fioUseMMF ? BIO_OPT_USEMMF : BIO_OPT_DB);
36   if(fret == &bNull)
37     fret = bioOpen(fname,FO_READONLY | SO_COMPAT,cache_size,fioUseMMF ? BIO_OPT_USEMMF : BIO_OPT_DB);
38   return fret;
39 }
40 
biewOpenRW(char * fname,unsigned cache_size)41 BGLOBAL __FASTCALL__ biewOpenRW(char *fname,unsigned cache_size)
42 {
43   BGLOBAL fret;
44   fret = bioOpen(fname,FO_READWRITE | SO_DENYNONE,cache_size,fioUseMMF ? BIO_OPT_USEMMF : BIO_OPT_DB);
45   if(fret == &bNull)
46     fret = bioOpen(fname,FO_READWRITE | SO_COMPAT,cache_size,fioUseMMF ? BIO_OPT_USEMMF : BIO_OPT_DB);
47   return fret;
48 }
49 
BMOpen(char * fname)50 int __FASTCALL__ BMOpen(char * fname)
51 {
52   bm_file_handle = biewOpenRO(fname,BBIO_CACHE_SIZE);
53   if(bm_file_handle == &bNull)
54   {
55     errnoMessageBox(OPEN_FAIL,NULL,errno);
56     return -1;
57   }
58   sc_bm_file_handle = bioDupEx(bm_file_handle,BBIO_SMALL_CACHE_SIZE);
59   if(sc_bm_file_handle == &bNull)
60   {
61     errnoMessageBox(DUP_FAIL,NULL,errno);
62     return -1;
63   }
64   bioSetOptimization(bm_file_handle,BIO_OPT_RANDOM);
65   bioSetOptimization(sc_bm_file_handle,BIO_OPT_RANDOM);
66   if(BMGetFLength() > ULONG_MAX) BMFileFlags |= BMFF_USE64;
67   return 0;
68 }
69 
BMClose(void)70 void __FASTCALL__ BMClose( void )
71 {
72   if(bm_file_handle != &bNull) bioClose(bm_file_handle);
73   bm_file_handle = &bNull;
74   if(sc_bm_file_handle != &bNull) bioClose(sc_bm_file_handle);
75   sc_bm_file_handle = &bNull;
76 }
77 
BMReadByteEx(__fileoff_t pos,int RELATION)78 tUInt8  __FASTCALL__ BMReadByteEx(__fileoff_t pos,int RELATION)
79 {
80  bioSeek(bm_file_handle,pos,RELATION);
81  return bioReadByte(bm_file_handle);
82 }
83 
BMReadWordEx(__fileoff_t pos,int RELATION)84 tUInt16 __FASTCALL__ BMReadWordEx(__fileoff_t pos,int RELATION)
85 {
86  bioSeek(bm_file_handle,pos,RELATION);
87  return bioReadWord(bm_file_handle);
88 }
89 
BMReadDWordEx(__fileoff_t pos,int RELATION)90 tUInt32  __FASTCALL__ BMReadDWordEx(__fileoff_t pos,int RELATION)
91 {
92  bioSeek(bm_file_handle,pos,RELATION);
93  return bioReadDWord(bm_file_handle);
94 }
95 
BMReadQWordEx(__fileoff_t pos,int RELATION)96 tUInt64  __FASTCALL__ BMReadQWordEx(__fileoff_t pos,int RELATION)
97 {
98  bioSeek(bm_file_handle,pos,RELATION);
99  return bioReadQWord(bm_file_handle);
100 }
101 
BMReadBufferEx(void * buffer,unsigned len,__fileoff_t pos,int RELATION)102 tBool __FASTCALL__ BMReadBufferEx(void * buffer,unsigned len,__fileoff_t pos,int RELATION)
103 {
104  bioSeek(bm_file_handle,pos,RELATION);
105  return bioReadBuffer(bm_file_handle,buffer,len);
106 }
107 
BMWriteBuff(void * buff,unsigned len)108 tBool __FASTCALL__ BMWriteBuff(void * buff,unsigned len)
109 {
110   tBool ret;
111   ret = bioWriteBuffer(bm_file_handle,buff,len);
112   bioFlush(bm_file_handle);
113   return ret;
114 }
115 
BMWriteByteEx(__fileoff_t pos,int RELATION,tUInt8 byte)116 tBool __FASTCALL__ BMWriteByteEx(__fileoff_t pos,int RELATION,tUInt8 byte)
117 {
118   bioSeek(bm_file_handle,pos,RELATION);
119   return bioWriteByte(bm_file_handle,byte);
120 }
121 
BMWriteWordEx(__fileoff_t pos,int RELATION,tUInt16 word)122 tBool __FASTCALL__ BMWriteWordEx(__fileoff_t pos,int RELATION,tUInt16 word)
123 {
124   bioSeek(bm_file_handle,pos,RELATION);
125   return bioWriteWord(bm_file_handle,word);
126 }
127 
BMWriteDWordEx(__fileoff_t pos,int RELATION,tUInt32 dword)128 tBool __FASTCALL__ BMWriteDWordEx(__fileoff_t pos,int RELATION,tUInt32 dword)
129 {
130   bioSeek(bm_file_handle,pos,RELATION);
131   return bioWriteDWord(bm_file_handle,dword);
132 }
133 
BMWriteQWordEx(__fileoff_t pos,int RELATION,tUInt64 dword)134 tBool __FASTCALL__ BMWriteQWordEx(__fileoff_t pos,int RELATION,tUInt64 dword)
135 {
136   bioSeek(bm_file_handle,pos,RELATION);
137   return bioWriteQWord(bm_file_handle,dword);
138 }
139 
BMWriteBuffEx(__fileoff_t pos,int RELATION,void * buff,unsigned len)140 tBool  __FASTCALL__ BMWriteBuffEx(__fileoff_t pos,int RELATION,void * buff,unsigned len)
141 {
142   bioSeek(bm_file_handle,pos,RELATION);
143   return bioWriteBuffer(bm_file_handle,buff,len);
144 }
145 
bmReadByteEx(__fileoff_t pos,int RELATION)146 tUInt8 __FASTCALL__ bmReadByteEx(__fileoff_t pos,int RELATION)
147 {
148  bioSeek(sc_bm_file_handle,pos,RELATION);
149  return bioReadByte(sc_bm_file_handle);
150 }
151 
bmReadWordEx(__fileoff_t pos,int RELATION)152 tUInt16 __FASTCALL__ bmReadWordEx(__fileoff_t pos,int RELATION)
153 {
154  bioSeek(sc_bm_file_handle,pos,RELATION);
155  return bioReadWord(sc_bm_file_handle);
156 }
157 
bmReadDWordEx(__fileoff_t pos,int RELATION)158 tUInt32 __FASTCALL__ bmReadDWordEx(__fileoff_t pos,int RELATION)
159 {
160  bioSeek(sc_bm_file_handle,pos,RELATION);
161  return bioReadDWord(sc_bm_file_handle);
162 }
163 
bmReadQWordEx(__fileoff_t pos,int RELATION)164 tUInt64 __FASTCALL__ bmReadQWordEx(__fileoff_t pos,int RELATION)
165 {
166  bioSeek(sc_bm_file_handle,pos,RELATION);
167  return bioReadQWord(sc_bm_file_handle);
168 }
169 
bmReadBufferEx(void * buffer,unsigned len,__fileoff_t pos,int RELATION)170 tBool __FASTCALL__ bmReadBufferEx(void * buffer,unsigned len,__fileoff_t pos,int RELATION)
171 {
172  bioSeek(sc_bm_file_handle,pos,RELATION);
173  return bioReadBuffer(sc_bm_file_handle,buffer,len);
174 }
175 
bmWriteBuff(void * buff,unsigned len)176 tBool __FASTCALL__ bmWriteBuff(void * buff,unsigned len)
177 {
178   tBool ret;
179   ret = bioWriteBuffer(sc_bm_file_handle,buff,len);
180   bioFlush(sc_bm_file_handle);
181   return ret;
182 }
183 
bmWriteByteEx(__fileoff_t pos,int RELATION,tUInt8 byte)184 tBool __FASTCALL__ bmWriteByteEx(__fileoff_t pos,int RELATION,tUInt8 byte)
185 {
186   bioSeek(sc_bm_file_handle,pos,RELATION);
187   return bioWriteByte(sc_bm_file_handle,byte);
188 }
189 
bmWriteWordEx(__fileoff_t pos,int RELATION,tUInt16 word)190 tBool __FASTCALL__ bmWriteWordEx(__fileoff_t pos,int RELATION,tUInt16 word)
191 {
192   bioSeek(sc_bm_file_handle,pos,RELATION);
193   return bioWriteWord(sc_bm_file_handle,word);
194 }
195 
bmWriteDWordEx(__fileoff_t pos,int RELATION,tUInt32 dword)196 tBool __FASTCALL__ bmWriteDWordEx(__fileoff_t pos,int RELATION,tUInt32 dword)
197 {
198   bioSeek(sc_bm_file_handle,pos,RELATION);
199   return bioWriteDWord(sc_bm_file_handle,dword);
200 }
201 
bmWriteQWordEx(__fileoff_t pos,int RELATION,tUInt64 dword)202 tBool __FASTCALL__ bmWriteQWordEx(__fileoff_t pos,int RELATION,tUInt64 dword)
203 {
204   bioSeek(sc_bm_file_handle,pos,RELATION);
205   return bioWriteQWord(sc_bm_file_handle,dword);
206 }
207 
bmWriteBuffEx(__fileoff_t pos,int RELATION,void * buff,unsigned len)208 tBool  __FASTCALL__ bmWriteBuffEx(__fileoff_t pos,int RELATION,void * buff,unsigned len)
209 {
210   bioSeek(sc_bm_file_handle,pos,RELATION);
211   return bioWriteBuffer(sc_bm_file_handle,buff,len);
212 }
213