Initial import of the CDE 2.1.30 sources from the Open Group.

This commit is contained in:
Peter Howkins
2012-03-10 18:21:40 +00:00
commit 83b6996daa
18978 changed files with 3945623 additions and 0 deletions

View File

@@ -0,0 +1,171 @@
/* $XConsortium: SearchStorage.cc /main/4 1996/07/23 18:10:39 cde-hal $
*
* (c) Copyright 1996 Digital Equipment Corporation.
* (c) Copyright 1996 Hewlett-Packard Company.
* (c) Copyright 1996 International Business Machines Corp.
* (c) Copyright 1996 Sun Microsystems, Inc.
* (c) Copyright 1996 Novell, Inc.
* (c) Copyright 1996 FUJITSU LIMITED.
* (c) Copyright 1996 Hitachi.
*/
#include <stdio.h>
#include <stddef.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <stream.h>
/* imported interfaces */
#include <misc/unique_id.h>
#include "FlexBuffer.h"
#include "Task.h"
#include "DataBase.h"
/* exported interfaces */
#include "SearchStorage.h"
//-----------------------------------------------------------------------
static int isdir(const char* filename)
{
int ret = 0;
struct stat sb;
if(stat(filename, &sb) == 0){
if(S_ISDIR(sb.st_mode)){
ret = 1;
}
}
return ret;
}
//-----------------------------------------------------------------------
static void makedir(const char *path) /* throw(PosixError) */
{
if(mkdir((char*)path, 0775) != 0){
throw(PosixError(errno, path));
}
}
//-----------------------------------------------------------------------
static const char *FilteredString( FlexBuffer &f_buf, const char *str )
{
for ( const char *ptr = str; *ptr != 0; ptr++ ) {
if ( *ptr == '\"' ) {
f_buf.writeStr ( "\\\"" );
}
else if ( *ptr == '\\' ) {
f_buf.writeStr ( "\\\\" );
}
else {
f_buf.put ( *ptr );
}
}
return ( f_buf.GetBuffer() );
}
//-----------------------------------------------------------------------
SearchStorage::SearchStorage( const char *path, const char *name )
{
if ( !isdir(path) ) {
makedir(path);
}
filteredPath = new char [ strlen(path) + 1 + strlen("filtered") + 1 ];
/*
* throw(ResourceExhausted)
*
*/
assert ( filteredPath != NULL );
sprintf( filteredPath, "%s/filtered", path );
if ( !isdir(filteredPath) ) {
makedir(filteredPath);
}
/* construct the fulcrum path */
char *fulcrumpath = form("%s/fulcrum", path );
if ( !isdir(fulcrumpath) ) {
makedir(fulcrumpath);
}
char *catalogname = form("%s/%s.cin", fulcrumpath, name );
catalogfile = fopen ( catalogname, "w" );
if ( !catalogfile ) {
throw(PosixError(errno, form("unable to open catalog %s\n", catalogname) ) );
}
}
//-----------------------------------------------------------------------
void
SearchStorage::insert( const char *BookCaseName,
const int BookNum,
const char *BookShortTitle,
const char *SectionID,
const char *SectionTitle,
const char *buffer,
int size
)
{
// write the search buffer to a file with name "NodeLocator" under
// fulcrumpath
// we use a unique_id() as the file name, in order to avoid spaces, /
// and stuffs like that.....
const char *filtered_file_name = unique_id();
char *filename = form("%s/%s", filteredPath, filtered_file_name );
FILE *fp = fopen( filename, "w" );
if ( !fp ) {
throw(PosixError(errno, form("Unable to open %s", filename) ) );
}
if ( !fwrite ( buffer, size, 1, fp ) ) {
throw(PosixError(errno, "unable to write to buffer\n" ) );
}
fclose(fp);
FlexBuffer new_section_id;
FlexBuffer new_book_short_title;
FlexBuffer new_section_title;
FlexBuffer new_book_case_name;
// append to the catalog file
if ( catalogfile ) {
fprintf( catalogfile, "{\ns %s s 32 1;\n", filtered_file_name );
fprintf( catalogfile, "f 100 \"%s\" ;\n",
FilteredString( new_section_id, SectionID ) );
fprintf( catalogfile, "f 127 \"%s\" ;\n" ,
FilteredString( new_book_short_title, BookShortTitle ) );
fprintf( catalogfile, "f 128 \"%s\" ;\n",
FilteredString( new_section_title, SectionTitle ) );
fprintf( catalogfile, "f 130 \"%d\";\n", BookNum );
fprintf( catalogfile, "f 131 \"%s\";\n",
FilteredString( new_book_case_name, BookCaseName) );
fprintf( catalogfile, "}\n");
}
}
//-----------------------------------------------------------------------
SearchStorage::~SearchStorage()
{
if ( catalogfile ) { fclose(catalogfile); }
if ( filteredPath ) { delete [] filteredPath; }
}