1 /* $Id: fmspell.c,v 1.1 1998/02/24 21:01:22 shavirin Exp $
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *               National Center for Biotechnology Information
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's official duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government have not placed any restriction on its use or reproduction.
13 *
14 *  Although all reasonable efforts have been taken to ensure the accuracy
15 *  and reliability of the software and data, the NLM and the U.S.
16 *  Government do not and cannot warrant the performance or results that
17 *  may be obtained by using this software or data. The NLM and the U.S.
18 *  Government disclaim all warranties, express or implied, including
19 *  warranties of performance, merchantability or fitness for any particular
20 *  purpose.
21 *
22 *  Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * File Name:  $RCSfile: fmspell.c,v $
27 *
28 * Author:  Sergei Shavirin
29 *
30 * Initial Version Creation Date: 02/24/1998
31 *
32 * $Revision: 1.1 $
33 *
34 * File Description:
35 *        Database formatter for NCBI SPELL Service
36 *
37 * $Log: fmspell.c,v $
38 * Revision 1.1  1998/02/24 21:01:22  shavirin
39 * Initial revision
40 *
41 *
42 * ==========================================================================
43 */
44 
45 #include <ncbi.h>
46 #include <ncbisort.h>
47 #include <ncbisam.h>
48 
49 #define NUMARG 3
50 
51 Args fmspell_args[NUMARG] = {
52     { "Input file name for spell database",
53       "termlist", NULL, NULL, FALSE, 'd', ARG_STRING, 0.0, 0, NULL},
54     {"Input file anme for stop words",
55      "stopwords", NULL,NULL,FALSE,'s',ARG_FILE_IN, 0.0,0,NULL},
56     {"Logfile name:",
57      "fmspell.log", NULL,NULL,TRUE,'l',ARG_FILE_OUT, 0.0,0,NULL}
58 };
59 
60 #define DB_filename    (CharPtr) fmspell_args[0].strvalue
61 #define STOP_filename  (CharPtr) fmspell_args[1].strvalue
62 #define LogFileName    (CharPtr) fmspell_args[2].strvalue
63 
FMSpellCreateIndex(CharPtr db_name)64 static Boolean FMSpellCreateIndex(CharPtr db_name)
65 {
66     SORTObjectPtr sop;
67     Char filenamebuf[FILENAME_MAX], DBName[FILENAME_MAX];
68     Char ext1[8], ext2[8], ext3[8], ext4[8];
69     FILE *fd_out;
70     CharPtr files;
71     ISAMErrorCode error;
72     ISAMObjectPtr isamp;
73 
74     /*  object for unique sorting */
75 
76     if((sop = SORTObjectNew(NULL, '\0', 0,
77                             FALSE, TRUE)) == NULL) {
78         ErrPostEx(SEV_ERROR, 0, 0, "Failed to create SORT Object");
79         return FALSE;
80     }
81 
82     sprintf(filenamebuf, "%s.tmp", db_name);
83 
84     if((fd_out = FileOpen(filenamebuf, "w")) == NULL)
85         return FALSE;
86 
87     files = db_name;
88 
89     SORTFiles(&files, 1, fd_out, sop);
90     SORTObjectFree(sop);
91 
92     FileClose(fd_out);
93     FileRename(filenamebuf, db_name);
94 
95     sprintf(filenamebuf, "%s.idx", db_name);
96 
97     if((isamp = ISAMObjectNew(ISAMString, db_name,
98                               filenamebuf)) == NULL) {
99         ErrPostEx(SEV_ERROR, 0, 0, "Creating of ISAM object failed");
100         return FALSE;
101     }
102 
103     if((error = ISAMMakeIndex(isamp, 0)) != ISAMNoError) {
104         ErrPostEx(SEV_ERROR, 0, 0, "Creating of index failed with "
105                   "error code %ld\n", (long) error);
106         ISAMObjectFree(isamp);
107         return FALSE;
108     }
109 
110     ISAMObjectFree(isamp);
111 
112     return TRUE;
113 }
114 
Main(void)115 Int2 Main(void)
116 {
117 
118     if ( !GetArgs ("fmspell", NUMARG, fmspell_args) ) {
119         return -1;
120     }
121     if ( !ErrSetLog (LogFileName) ) {
122         ErrShow();
123     } else {
124         ErrSetOpts (ERR_CONTINUE, ERR_LOG_ON);
125     }
126 
127     if(!FMSpellCreateIndex(DB_filename))
128         return 1;
129 
130     if(!FMSpellCreateIndex(STOP_filename))
131         return 1;
132 
133     ErrPostEx(SEV_INFO, 0, 0, "Formating finished successfuly");
134 
135     return 0;
136 }
137