campo-sirio/include/prefix.cpp

225 lines
4.5 KiB
C++
Executable File

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#define __PREFIX_CPP
#include <prefix.h>
#ifndef FOXPRO
#include <applicat.h>
#endif
#include <array.h>
#include <extcdecl.h>
#include <scanner.h>
#include <utility.h>
HIDDEN const char* const glockname = "xx";
TPrefix::TPrefix()
{
CHECK(prefhndl == NULL, "Can't create two prefix objects");
_prefix = ".";
_dirfl = dirfl;
_recfl = recfl;
_fdir = fdir;
_rdir = rdir;
}
TPrefix::~TPrefix()
{
set();
}
HIDDEN int closeall(bool changestudy, TBit_array& excl)
{
if (!openf) return 0;
TDir d;
d.get(1);
const int max = (int) d.eod();
int err = NOERR;
for (int i = 1; i < max; i++)
if (openf[i] != NULL)
{
isfdptr isfd = openf[i];
d.get(i + 1, _nolock, _nordir, _sysdirop);
if (changestudy || d.name()[0] != '%')
{
excl.set(i, isfd->f.LockMode == _excllock);
CClose(&isfd->f);
CBCloseFile(&isfd->i, &err);
exclunlock(CInsPref((char*) glockname, _nordir), FALSE);
}
}
return max;
}
HIDDEN void openall(bool changestudy, TBit_array& excl, int oldmax)
{
if (!openf) return ;
TDir d;
d.get(1);
const int max = (int) d.eod();
for (int i = max; i < oldmax; i++)
if (openf[i] != NULL)
{
d.get(i + 1, _nolock, _nordir, _sysdirop);
if (changestudy || d.name()[0] != '%')
fatal_box("Can't reopen file n. %d", i + 1);
}
int err = NOERR;
for (i = 1; i < max; i++)
if (openf[i] != NULL)
{
isfdptr isfd = openf[i];
d.get(i + 1, _nolock, _nordir, _sysdirop);
if (changestudy || d.name()[0] != '%')
{
isfd->ft = d.name()[0] == '%' ? _comdir : _nordir;
d.get(i + 1, _nolock, (TDirtype) isfd->ft);
*isfd->d = *d.filedesc();
TTrec r;
r.get(i + 1, (TDirtype) isfd->ft);
*isfd->r = *r.rec();
if (excllock(CInsPref((char*) glockname, NORDIR), FALSE) == -1 && errno == EACCES)
fatal_box("Can't reopen file n. %d : file in use", i + 1);
COpen(&isfd->f, isfd->d->SysName, isfd->d->LenR, 0, excl[i]);
if ((err = isfd->f.IOR) == NOERR)
CBOpenFile (&isfd->i, CGetIdxName(isfd->d->SysName), excl[i], &err);
if (err != NOERR)
fatal_box("Can't reopen file n. %d : error n. %d", i + 1, err);
}
}
}
void TPrefix::set(const char* name, bool force, TFilelock mode)
{
if (name == NULL)
{
CCloseDir(NORDIR);
CCloseDir(COMDIR);
CCloseRecDir(NORDIR);
CCloseRecDir(COMDIR);
return;
}
if (_prefix == name) return;
if (!force && !test(name)) return;
TBit_array excl;
int max = 0;
if (_prefix != ".")
{
max = closeall(FALSE, excl);
CCloseDir(NORDIR);
CCloseDir(COMDIR);
CCloseRecDir(NORDIR);
CCloseRecDir(COMDIR);
}
if (strcmp(name, "DEF") == 0)
{
CGetPref();
_prefix = cprefix;
const int l = strlen(__ptprf);
if (l > 0) _prefix.ltrim(l);
}
else
{
_prefix = name;
if (*__ptprf && *name) strcpy(cprefix, __ptprf);
else strcpy(cprefix, "");
strcat(cprefix, name);
}
if (!test(_prefix))
fatal_box("Impossibile utilizzare la ditta %s", name);
COpenDir((int) mode, NORDIR);
COpenDir((int) mode, COMDIR);
COpenRecDir((int) mode, NORDIR);
COpenRecDir((int) mode, COMDIR);
if (_prefix != ".")
openall(FALSE, excl, max);
}
bool TPrefix::test(const char* s) const
{
if (s && *s && strcmp(s, "DEF"))
{
TString80 s1; s1 << __ptprf << s << "/dir.gen";
if (!fexist(s1))
return error_box("Impossibile trovare il file '%s'", (const char*)s1);
}
if ((_dirfl[0] > 1) || (_dirfl[1] > 1) ||
(_recfl[0] > 1) || (_recfl[1] > 1))
return error_box("Impossibile cambiare ditta (dir.gen o trc.gen in uso)");
return TRUE;
}
void TPrefix::put()
{
CPutPref((char*)(const char*)_prefix);
}
bool TPrefix::test(long codditta) const
{
TString16 s; s.format("%05lda", codditta);
return test(s);
}
long TPrefix::get_codditta() const
{
const long codditta = atol((const char*)_prefix);
return codditta;
}
bool TPrefix::set_codditta(long codditta, bool force)
{
if (force || test(codditta))
{
TString16 s; s.format("%05lda", codditta);
set(s, force);
put();
return TRUE;
}
else return FALSE;
}
const char* firm2dir(long codditta)
{
TFixed_string dir(__tmp_string, 256);
dir.format("%05lda", codditta);
dir.insert(__ptprf, 0);
return __tmp_string;
}