/*********************************************************************
This file is part of Siworin. Copyright © 2021 by Ruud Harmsen.
Siworin is a simple word indexer and local search engine. See
https://rudhar.com/sfreview/siworin/#siw04 and
https://rudhar.com/sfreview/siworin/toolsrc/siworin .
Siworin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
Siworin is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Siworin. If not, see .
********************************************************************/
#include
#include
#include
#include "siworin-files.h"
#include "siworin-config.h"
FILE *siworin_fp_paths = NULL;
FILE *siworin_fp_pathoffs = NULL;
FILE *siworin_fp_words = NULL;
FILE *siworin_fp_wordoffs = NULL;
FILE *siworin_fp_wordfreq = NULL;
static char *default_prefix = "siworin";
static char *make_name (const char *file_prefix, const char *file_postfix);
int siworin_fileopen_paths (
const char *file_prefix, const char *mode_paths, const char *mode_pathoffs)
{
char *name;
int retval = 0;
if (!(name = make_name(file_prefix, ".paths")))
{
fprintf(stderr, "%s, line %d: cannot malloc space for paths\n",
__FILE__, __LINE__);
retval = 5;
}
else
{
siworin_fp_paths = fopen(name, mode_paths);
if (!siworin_fp_paths)
{
fprintf(stderr, "%s, line %d: cannot open %s in mode %s\n",
__FILE__, __LINE__, name, mode_paths);
retval = 1;
}
free(name);
}
if (!(name = make_name(file_prefix, ".pathoffs")))
{
fprintf(stderr, "%s, line %d: cannot malloc space for pathoffs\n",
__FILE__, __LINE__);
retval = 6;
}
else
{
siworin_fp_pathoffs = fopen(name, mode_pathoffs);
if (!siworin_fp_pathoffs)
{
fprintf(stderr, "%s, line %d: cannot open %s in mode %s\n",
__FILE__, __LINE__, name, mode_pathoffs);
retval = 2;
}
}
return retval;
}
int siworin_fileopen_words (
const char *file_prefix, const char *mode_words, const char *mode_wordoffs)
{
char *name;
int retval = 0;
if (!(name = make_name(file_prefix, ".words")))
{
fprintf(stderr, "%s, line %d: cannot malloc space for words\n",
__FILE__, __LINE__);
retval = 7;
}
else
{
siworin_fp_words = fopen(name, mode_words);
if (!siworin_fp_words)
{
fprintf(stderr, "%s, line %d: cannot open %s in mode %s\n",
__FILE__, __LINE__, name, mode_words);
retval = 3;
}
free(name);
}
if (!(name = make_name(file_prefix, ".wordoffs")))
{
fprintf(stderr, "%s, line %d: cannot malloc space for wordoffs\n",
__FILE__, __LINE__);
retval = 8;
}
else
{
siworin_fp_wordoffs = fopen(name, mode_wordoffs);
if (!siworin_fp_wordoffs)
{
fprintf(stderr, "%s, line %d: cannot open %s in mode %s\n",
__FILE__, __LINE__, name, mode_wordoffs);
retval = 4;
}
}
if (atoi(siworin_config_value(SIWORIN_CONFIG_WRITE_WORDS_FREQ)))
{
if (!(name = make_name(file_prefix, ".wrdcnt")))
{
fprintf(stderr, "%s, line %d: cannot malloc space for words\n",
__FILE__, __LINE__);
retval = 17;
}
else
{
siworin_fp_wordfreq = fopen(name, mode_words);
if (!siworin_fp_wordfreq)
{
fprintf(stderr, "%s, line %d: cannot open %s in mode %s\n",
__FILE__, __LINE__, name, mode_words);
retval = 13;
}
free(name);
}
}
return retval;
}
int siworin_fileopen_close (void)
{
if (siworin_fp_paths)
{
fclose(siworin_fp_paths);
siworin_fp_paths = NULL;
}
if (siworin_fp_pathoffs)
{
fclose(siworin_fp_pathoffs);
siworin_fp_pathoffs = NULL;
}
if (siworin_fp_words)
{
fclose(siworin_fp_words);
siworin_fp_words = NULL;
}
if (siworin_fp_wordoffs)
{
fclose(siworin_fp_wordoffs);
siworin_fp_wordoffs = NULL;
}
if (siworin_fp_wordfreq)
{
fclose(siworin_fp_wordfreq);
siworin_fp_wordfreq = NULL;
}
return 0;
}
static char *make_name (const char *file_prefix, const char *file_postfix)
{
char *name = NULL;
if (!file_prefix || !file_prefix[0])
file_prefix = default_prefix;
name = malloc(strlen(file_prefix) + strlen(file_prefix) + 1);
if (name)
sprintf(name, "%s%s", file_prefix, file_postfix);
return name;
}