2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
24 * $XConsortium: object_dict.C /main/7 1996/09/13 20:48:20 cde-hal $
26 * Copyright (c) 1993 HAL Computer Systems International, Ltd.
27 * All rights reserved. Unpublished -- rights reserved under
28 * the Copyright Laws of the United States. USE OF A COPYRIGHT
29 * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
32 * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33 * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
34 * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35 * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
38 * RESTRICTED RIGHTS LEGEND
39 * Use, duplication, or disclosure by the Government is subject
40 * to the restrictions as set forth in subparagraph (c)(l)(ii)
41 * of the Rights in Technical Data and Computer Software clause
42 * at DFARS 252.227-7013.
44 * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
51 #include "schema/object_dict.h"
52 #include "utility/randomize.h"
53 #include "utility/db_version.h"
54 #include "misc/unique_id.h"
57 desc* last_desc_ptr = 0;
59 char replace_string[PATHSIZ];
60 int replace_string_len;
62 //extern int yyparse();
63 //extern int yyrestart(FILE*);
66 extern int schemaparse();
67 extern int schemarestart(FILE*);
68 extern FILE *schemain;
70 object_dict::object_dict() :
71 v_dict(desc_name_eq, desc_name_ls), v_desc_ptr(0)
76 object_dict::~object_dict()
78 quit_a_base(v_desc_ptr, 0, true);
81 void object_dict::quit_a_base(desc* b, desc* e, Boolean sync)
83 _quit_stored_objects(b, e);
84 _quit_stores(b, e, sync);
88 void object_dict::_quit_descs(desc* begin_ptr, desc* end_ptr)
90 desc *ptr = begin_ptr;
93 while ( ptr != end_ptr ) {
95 ptr = ptr -> next_desc;
100 extern int g_mode_8_3;
103 desc* object_dict::init_a_base(char* db_path, char* db_name)
105 //MESSAGE(cerr, "object_dict::init_a_base()");
106 //debug(cerr, db_path);
108 int len = MIN(strlen(db_path), PATHSIZ - 1);
109 *((char *) memcpy(v_db_path, db_path, len) + len) = '\0';
113 char* schema_path = 0;
116 schema_path = form("%s/%s.%s", v_db_path, db_name, SCHEMA_FILE_SUFFIX);
118 schema_path = form("%s/%s", v_db_path, SCHEMA_FILE);
120 fstream in(schema_path, ios::in);
123 throw(streamException(in.rdstate()));
125 char schema_header[LBUFSIZ];
127 if ( !in.getline(schema_header, LBUFSIZ) ) {
128 throw(streamException(in.rdstate()));
131 if ( strncmp(schema_header+1, "MMDB", 4) != 0 ) {
133 x = parse(schema_path);
136 unsigned int sz = bytes(schema_path) - strlen(schema_header) - 1;
137 char* buf = new char[sz];
139 if ( !in.read(buf, sz) )
140 throw(streamException(in.rdstate()));
145 orig.set_chunk(buf, sz);
146 orig.set_content_sz(sz);
151 //fprintf(stderr, "buf=%s\n", buf);
168 desc* object_dict::init_a_base(char* define_desc_path, char* db_path,
171 //MESSAGE(cerr, "object_dict::init a base (define case)");
172 //debug(cerr, define_desc_path);
173 //debug(cerr, db_path);
174 //debug(cerr, db_name);
178 len = MIN(strlen(db_path), PATHSIZ - 1);
179 *((char *) memcpy(v_db_path, db_path, len) + len) = '\0';
182 len = MIN(strlen(db_name), PATHSIZ - 1);
183 *((char *) memcpy(replace_string, db_name, len) + len) = '\0';
184 replace_string_len = strlen(replace_string);
186 replace_string[0] = 0;
187 replace_string_len = 0;
191 fstream in_test(define_desc_path, ios::in);
194 throw(stringException(form("%s does not exist.", define_desc_path)));
197 unsigned long llen = bytes(define_desc_path)*4;
201 if ( disk_space(v_db_path) < llen ) {
202 throw(stringException(form("no enough space on %s", v_db_path)));
205 ///////////////////////////////////
206 // define and init the dictionary
207 ///////////////////////////////////
208 desc* x = parse(define_desc_path);
211 ////////////////////////////
212 // randomize the schema.mmdb
213 ////////////////////////////
215 fstream in(define_desc_path, ios::in);
218 debug(cerr, define_desc_path);
219 throw(streamException(in.rdstate()));
222 unsigned int sz = bytes(define_desc_path);
225 char* schema_buf = new char[sz*3];
227 ostringstream* string_out = new ostringstream(schema_buf);
228 if ( !(*string_out) ) {
229 throw(streamException(string_out -> rdstate()));
232 x -> asciiOutList(*string_out);
234 len = MIN((unsigned int) string_out->str().size(), sz*3 - 1);
235 *((char *) memcpy(schema_buf, string_out->str().c_str(), len) + len) = '\0';
238 sz = strlen(schema_buf);
241 orig.set_chunk(schema_buf, sz);
242 orig.set_content_sz(sz);
247 /////////////////////////////
248 // save the output to db_path
249 /////////////////////////////
250 fstream out(form("%s/%s.%s", db_path, db_name, SCHEMA_FILE_SUFFIX), ios::out);
251 // fstream out(form("%s/%s.%s", db_path, db_name, SCHEMA_FILE_SUFFIX), ios::out, open_file_prot());
254 MESSAGE(cerr, form("bad file name: %s", db_path));
255 throw(streamException(out.rdstate()));
258 char* desc_header = form("#MMDB\t%d\t%d\n", MAJOR, MINOR);
259 if ( !out.write(desc_header, strlen(desc_header)) ) {
260 MESSAGE(cerr, form("write descirption head in %s failed", db_path));
261 throw(streamException(out.rdstate()));
264 if ( !out.write(schema_buf, strlen(schema_buf)) ) {
265 MESSAGE(cerr, form("write descirption in %s failed", db_path));
266 throw(streamException(out.rdstate()));
277 desc* object_dict::parse(buffer& desc_buf)
279 char unique_nm[PATHSIZ];
281 Boolean ok = writeToTmpFile (
284 desc_buf.content_sz()
287 //debug(cerr, desc_buf.get_base());
288 //fprintf(stderr, "get_base=%s\n", desc_buf.get_base());
289 //fprintf(stderr, "sz=%d\n", desc_buf.content_sz());
290 //fprintf(stderr, "tmpnm=%s\n", unique_nm);
293 //fprintf(stderr, "write to temp failed\n");
294 throw(stringException("can't prepare the object dictionary."));
300 x = parse(unique_nm);
303 mcatch (mmdbException &,e) {
313 desc* object_dict::parse(char* define_desc_path)
315 schemain = fopen(define_desc_path, "r");
317 if ( schemain == NULL )
318 throw(stringException("open desc file failed"));
323 schemarestart(schemain);
328 if ( schemaparse() != 0 ) {
330 throw(stringException("Parsing input failed"));
334 mcatch (mmdbException &,e) {
345 void object_dict::_init(desc* x)
350 mtry { // init all stores
352 ptr -> init_store(this -> v_db_path);
353 ptr = ptr -> next_desc;
357 mcatch (mmdbException &,e) {
358 _quit_stores(x, ptr);
365 while ( ptr ) { // add to the dict
366 if ( ptr -> get_store() )
368 ptr = ptr -> next_desc;
371 //////////////////////////////
372 // init stored objects
373 //////////////////////////////
375 //MESSAGE(cerr, "init stored obj:");
379 ptr -> init_handler(*this);
381 ptr = ptr -> next_desc;
384 if ( v_desc_ptr == 0 ) {
387 v_last_desc_ptr -> next_desc = x;
389 v_last_desc_ptr = last_desc_ptr;
392 void object_dict::_quit_stores(desc* start_ptr, desc* end_ptr, Boolean sync)
394 desc *ptr = start_ptr;
396 if ( sync == true ) {
397 while ( ptr != end_ptr ) {
402 mcatch (mmdbException &,e)
405 fprintf(stderr, "mmdbException caught @ %s line:%d.\n",
412 ptr = ptr -> next_desc;
418 while ( ptr != end_ptr ) {
423 mcatch (mmdbException &,e)
426 fprintf(stderr, "mmdbException caught @ %s line:%d.\n",
433 ptr = ptr -> next_desc;
437 void object_dict::_quit_stored_objects(desc* start_ptr, desc* end_ptr)
439 desc *ptr = start_ptr;
441 while ( ptr != end_ptr ) {
444 ptr -> quit_handler();
448 mcatch (mmdbException &,e)
451 fprintf(stderr, "mmdbException caught @ %s line:%d.\n",
458 ptr = ptr -> next_desc;
462 handler* object_dict::get_handler(const char* obj_name)
464 stored_object_desc key((char*)obj_name);
466 stored_object_desc* x = (stored_object_desc*)v_dict.member(&key);
469 return x -> get_handler();
471 throw(stringException(form("handler %s not in dict", obj_name)));
476 abs_storage* object_dict::get_store(const char* obj_name)
478 store_desc key((char*)obj_name);
480 store_desc* x = (store_desc*)v_dict.member(&key);
483 return x -> get_store();
485 throw(stringException(form("store %s not in dict", obj_name)));
490 void schemaerror( char* errorstr )
492 extern int linecount;
493 extern char schematext[];
495 if ( strlen( schematext ) > 0 )
496 throw(stringException(form("line %d %s at or before \"%s\"",
497 linecount, errorstr, schematext
502 throw(stringException(form("line %d %s illegal-symbol",
517 //void yyerror( char* errorstr )
519 // extern int linecount;
520 // extern char yytext[];
522 // if ( strlen( yytext ) > 0 )
523 // MESSAGE(cerr, form("line %d %s at or before \"%s\"",
524 // linecount, errorstr, yytext
529 // MESSAGE(cerr, form("line %d %s illegal-symbol",
530 // linecount, errorstr
539 // extern int linecount;
540 // extern char *pname;
543 // MESSAGE(cerr, form("Number of input lines %6d", linecount));
546 //cerr << "Calling yywrap()\n";