9bb4d4ada8158cd1a2ede0c250b7a69e4299fd2f
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / storage / vm_storage.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: vm_storage.cc /main/4 1996/07/18 16:05:10 drk $ */
24
25 #include "storage/vm_storage.h"
26 #include "utility/mmdb_exception.h"
27
28 vm_storage::vm_storage( char* _path, char* _name, rep_policy* rep_p):
29   abs_storage( _path, _name, MEM_STORAGE_CODE, rep_p ), f_array(20)
30 {
31 }
32
33 vm_storage::~vm_storage()
34 {
35    remove();
36 }
37
38 void vm_storage::remove() 
39 {
40    for ( int i=0; i<f_array.no_elmts(); i++ ) {
41      delete (buffer*)f_array[i];
42      f_array.insert(0, i) ;
43    }
44    f_array.reset_elmts(0);
45 }
46
47 int vm_storage::appendString(mmdb_pos_t loc, const char*, int len,
48                             Boolean flush_opt) 
49 {
50    return -1;
51 }
52
53 int vm_storage::readString(mmdb_pos_t loc, char* base, int len, int str_offset) 
54 {
55 //MESSAGE(cerr, "vm: read");
56 //debug(cerr, loc);
57 //debug(cerr, len);
58 //debug(cerr, str_offset);
59
60    char* x;
61    int y;
62    int ok = get_str_ptr(loc, x, y);
63
64    if ( ok == 0 && str_offset+len <= y ) {
65       memcpy(base, x+str_offset, len);
66 //MESSAGE(cerr, "========");
67       return 0;
68    } else
69       return -1;
70 }
71
72 int vm_storage::insertString(mmdb_pos_t& loc, const char* base, int len, Boolean flush)
73 {
74 //MESSAGE(cerr, "vm: insert");
75 //debug(cerr, len);
76
77
78    char* x;
79    int ok = allocString (loc, len, x, 0);
80 //debug(cerr, loc);
81 //MESSAGE(cerr, "========");
82
83    if ( ok == 0 ) {
84       return updateString(loc, base, len, 0);
85    } else
86       return -1;
87 }
88
89 int vm_storage::get_str_ptr(mmdb_pos_t loc, char*& x, int& len)
90 {
91 //MESSAGE(cerr, "vm: get_str_ptr");
92 //debug(cerr, loc);
93
94    if ( loc >= f_array.no_elmts() ) {
95       debug(cerr, loc);
96       debug(cerr, f_array.no_elmts());
97       return -1;
98    }
99
100    buffer* b = (buffer*)f_array[(int)loc];
101
102    if ( b == 0 ) {
103      MESSAGE(cerr, "null pointer");
104      return -1;
105    }
106
107    x = b -> get_base();
108    len = b -> buf_sz();
109 //debug(cerr, (void*)x);
110 //debug(cerr, len);
111 //MESSAGE(cerr, "========");
112
113    return 0;
114 }
115
116 int vm_storage::updateString(mmdb_pos_t loc, const char* base, int len, 
117                     int string_ofst, Boolean flush) 
118 {
119 //MESSAGE(cerr, "updateString");
120 //debug(cerr, loc);
121 //debug(cerr, len);
122
123    if ( loc >= f_array.no_elmts() ) {
124       debug(cerr, loc);
125       debug(cerr, f_array.no_elmts());
126       return -1;
127    }
128
129    buffer* b = (buffer*)f_array[(int)loc];
130
131    if ( b == 0 ) {
132      MESSAGE(cerr, "null pointer");
133      return -1;
134    }
135
136
137    if ( b -> buf_sz() < len + string_ofst )
138       b -> expand_chunk(len + string_ofst);
139
140 /*
141 for (int i=0; i<len; i++)
142   if ( isascii(base[i]) )
143   cerr << base[i];
144   else
145   cerr << int(base[i]);
146 cerr << "\n";
147 MESSAGE(cerr, "========");
148 */
149
150    char* x = b -> get_base();
151    memcpy(x+string_ofst, base, len);
152    return 0;
153 }
154
155 int vm_storage::deleteString (mmdb_pos_t loc, Boolean flush) 
156 {
157    return 0;
158 }
159
160 int vm_storage::allocString (mmdb_pos_t& loc, int len, char*& x, int mode)
161 {
162 //MESSAGE(cerr, "vm: allocate");
163 //debug(cerr, len);
164
165    int c = f_array.no_elmts();
166
167    if ( c >= f_array.count() ) {
168       f_array.expandWith(10);
169    }
170
171    buffer* b = new buffer(len==0 ? 1 : len);
172    f_array.insert(b, c) ;
173
174    f_array.reset_elmts(c+1);
175
176    x = b -> get_base();
177    loc = c;
178 //debug(cerr, (void*)x);
179 //debug(cerr, loc);
180 //MESSAGE(cerr, "========");
181
182    return 0;
183 }
184
185 Boolean vm_storage::io_mode(int mode)
186 {
187    return true;
188 }
189