* Sebastian Hammer, Adam Dickmeiss
*
* $Log: extract.c,v $
- * Revision 1.29 1995-11-21 15:01:14 adam
+ * Revision 1.32 1995-11-25 10:24:05 adam
+ * More record fields - they are enumerated now.
+ * New options: flagStoreData flagStoreKey.
+ *
+ * Revision 1.31 1995/11/24 11:31:35 adam
+ * Commands add & del read filenames from stdin if source directory is
+ * empty.
+ * Match criteria supports 'constant' strings.
+ *
+ * Revision 1.30 1995/11/22 17:19:16 adam
+ * Record management uses the bfile system.
+ *
+ * Revision 1.29 1995/11/21 15:01:14 adam
* New general match criteria implemented.
* New feature: document groups.
*
else if (*s == '$')
{
int spec_len;
- char special[32];
+ char special[64];
const char *spec_src = NULL;
const char *s1 = ++s;
while (*s1 && *s1 != ' ' && *s1 != '\t')
s1++;
spec_len = s1 - s;
- if (spec_len > 31)
- spec_len = 31;
+ if (spec_len > 63)
+ spec_len = 63;
memcpy (special, s, spec_len);
special[spec_len] = '\0';
s = s1;
if (spec_src)
{
strcpy (dst, spec_src);
- dst += strlen(spec_src);
+ dst += strlen (spec_src);
}
}
+ else if (*s == '\"' || *s == '\'')
+ {
+ int stopMarker = *s++;
+ char tmpString[64];
+ int i = 0;
+
+ while (*s && *s != stopMarker)
+ {
+ if (i < 63)
+ tmpString[i++] = *s;
+ }
+ if (*s)
+ s++;
+ tmpString[i] = '\0';
+ strcpy (dst, tmpString);
+ dst += strlen (tmpString);
+ }
else
{
logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
return dstBuf;
}
+static int recordExtract (SYSNO *sysno, const char *fname,
+ struct recordGroup *rGroup, int deleteFlag,
+ int fd,
+ const char *file_type,
+ RecType recType)
+{
+ struct recExtractCtrl extractCtrl;
+ int r;
+ char *matchStr;
+ SYSNO sysnotmp;
+ Record rec;
+
+ extractCtrl.fd = fd;
+
+ /* extract keys */
+ extractCtrl.subType = "";
+ extractCtrl.init = wordInit;
+ extractCtrl.add = addRecordKeyAny;
+
+ reckeys.buf_used = 0;
+ file_read_start (extractCtrl.fd);
+ extractCtrl.readf = file_read;
+ r = (*recType->extract)(&extractCtrl);
+ file_read_stop (extractCtrl.fd);
+ close (extractCtrl.fd);
+
+ if (r)
+ {
+ logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
+ return 0;
+ }
+
+ /* perform match if sysno not known and if match criteria is specified */
+
+ matchStr = NULL;
+ if (!sysno)
+ {
+ sysnotmp = 0;
+ sysno = &sysnotmp;
+ if (rGroup->fileMatch)
+ {
+ char *rinfo;
+
+ matchStr = fileMatchStr(&reckeys, rGroup, fname, file_type,
+ rGroup->fileMatch);
+ if (matchStr)
+ {
+ rinfo = dict_lookup (matchDict, matchStr);
+ if (rinfo)
+ memcpy (sysno, rinfo+1, sizeof(*sysno));
+ }
+ else
+ {
+ logf (LOG_WARN, "Record not inserted");
+ return 0;
+ }
+ }
+ }
+
+ /* new record ? */
+ if (! *sysno)
+ {
+ if (deleteFlag)
+ {
+ logf (LOG_LOG, "? record %s", fname);
+ return 1;
+ }
+ logf (LOG_LOG, "add record %s", fname);
+ rec = rec_new (records);
+ *sysno = rec->sysno;
+
+ if (matchStr)
+ dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
+ flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
+
+ records_inserted++;
+ }
+ else
+ {
+ struct recKeys delkeys;
+
+ rec = rec_get (records, *sysno);
+
+ delkeys.buf_used = rec->size[recInfo_delKeys];
+ delkeys.buf = rec->info[recInfo_delKeys];
+ flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
+ if (deleteFlag)
+ {
+ if (!delkeys.buf_used)
+ {
+ logf (LOG_WARN, "cannot delete %s - no delete keys",
+ fname);
+ }
+ else
+ logf (LOG_LOG, "delete record %s", fname);
+ records_deleted++;
+ rec_del (records, &rec);
+ return 1;
+ }
+ else
+ {
+ if (!delkeys.buf_used)
+ {
+ logf (LOG_WARN, "cannot update %s - no delete keys",
+ fname);
+ }
+ else
+ {
+ logf (LOG_LOG, "update record %s", fname);
+ flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
+ records_updated++;
+ }
+ }
+ }
+ free (rec->info[recInfo_fileType]);
+ rec->info[recInfo_fileType] =
+ rec_strdup (file_type, &rec->size[recInfo_fileType]);
+
+ free (rec->info[recInfo_filename]);
+ rec->info[recInfo_filename] =
+ rec_strdup (fname, &rec->size[recInfo_filename]);
+
+ free (rec->info[recInfo_delKeys]);
+ if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
+ {
+ logf (LOG_LOG, "Storing keys...");
+ rec->info[recInfo_delKeys] = malloc (reckeys.buf_used);
+ rec->size[recInfo_delKeys] = reckeys.buf_used;
+ memcpy (rec->info[recInfo_delKeys], reckeys.buf,
+ rec->size[recInfo_delKeys]);
+ }
+ else
+ {
+ rec->info[recInfo_delKeys] = NULL;
+ rec->size[recInfo_delKeys] = 0;
+ }
+ free (rec->info[recInfo_databaseName]);
+ rec->info[recInfo_databaseName] =
+ rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
+
+ rec_put (records, &rec);
+ return 1;
+}
+
int fileExtract (SYSNO *sysno, const char *fname, struct recordGroup *rGroup,
int deleteFlag)
{
- SYSNO sysnotmp;
- int i, r;
+ int i, fd;
char gprefix[128];
char ext[128];
char ext_res[128];
const char *file_type;
- const char *file_match;
- struct recExtractCtrl extractCtrl;
RecType recType;
- Record rec;
- char *matchStr;
if (!rGroup->groupName || !*rGroup->groupName)
*gprefix = '\0';
else
sprintf (gprefix, "%s.", rGroup->groupName);
- logf (LOG_DEBUG, "fileExtractAdd %s", fname);
+ logf (LOG_DEBUG, "fileExtract %s", fname);
/* determine file extension */
for (i = strlen(fname); --i >= 0; )
return 0;
/* determine match criteria */
- sprintf (ext_res, "%sfileMatch.%s", gprefix, ext);
- file_match = res_get (common_resource, ext_res);
- if (!file_match)
+ if (rGroup->fileMatch)
{
- sprintf (ext_res, "%sfileMatch", gprefix);
- file_match = res_get (common_resource, ext_res);
+ sprintf (ext_res, "%sfileMatch.%s", gprefix, ext);
+ rGroup->fileMatch = res_get (common_resource, ext_res);
+ if (!rGroup->fileMatch)
+ {
+ sprintf (ext_res, "%sfileMatch", gprefix);
+ rGroup->fileMatch = res_get (common_resource, ext_res);
+ }
}
/* determine database name */
if (!rGroup->databaseName)
rGroup->databaseName = "Default";
- /* open input file */
- if ((extractCtrl.fd = open (fname, O_RDONLY)) == -1)
+ if (rGroup->flagStoreData == -1)
{
- logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
- return 0;
- }
-
- /* extract keys */
- extractCtrl.subType = "";
- extractCtrl.init = wordInit;
- extractCtrl.add = addRecordKeyAny;
-
- reckeys.buf_used = 0;
- file_read_start (extractCtrl.fd);
- extractCtrl.readf = file_read;
- r = (*recType->extract)(&extractCtrl);
- file_read_stop (extractCtrl.fd);
- close (extractCtrl.fd);
-
- if (r)
- {
- logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
- return 0;
- }
-
- /* perform match if sysno not known and if match criteria is specified */
-
- matchStr = NULL;
- if (!sysno && file_match)
- {
- char *rinfo;
-
- sysno = &sysnotmp;
- matchStr = fileMatchStr(&reckeys, rGroup, fname, file_type,
- file_match);
- if (matchStr)
- {
- rinfo = dict_lookup (matchDict, matchStr);
- if (rinfo)
- memcpy (sysno, rinfo+1, sizeof(*sysno));
- else
- *sysno = 0;
- }
- else
+ const char *sval;
+ sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
+ if (!(sval = res_get (common_resource, ext_res)))
{
- logf (LOG_WARN, "Record not inserted");
- return 0;
+ sprintf (ext_res, "%sstoreData", gprefix);
+ sval = res_get (common_resource, ext_res);
}
+ if (sval)
+ rGroup->flagStoreData = atoi (sval);
}
+ if (rGroup->flagStoreData == -1)
+ rGroup->flagStoreData = 0;
- /* new record ? */
- if (! *sysno)
- {
- logf (LOG_LOG, "add record %s", fname);
- rec = rec_new (records);
- *sysno = rec->sysno;
-
- if (matchStr)
- dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
- flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
- records_inserted++;
- }
- else
+ if (rGroup->flagStoreKeys == -1)
{
- struct recKeys delkeys;
-
- rec = rec_get (records, *sysno);
+ const char *sval;
- delkeys.buf_used = rec->size[2];
- delkeys.buf = rec->info[2];
- flushRecordKeys (*sysno, 0, &delkeys, rec->info[3]);
- flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
-
- records_updated++;
+ sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
+ if (!(sval = res_get (common_resource, ext_res)))
+ {
+ sprintf (ext_res, "%sstoreKeys", gprefix);
+ sval = res_get (common_resource, ext_res);
+ }
+ if (sval)
+ rGroup->flagStoreKeys = atoi (sval);
}
- free (rec->info[0]);
- rec->info[0] = rec_strdup (file_type, &rec->size[0]);
+ if (rGroup->flagStoreKeys == -1)
+ rGroup->flagStoreKeys = 0;
- free (rec->info[1]);
- rec->info[1] = rec_strdup (fname, &rec->size[1]);
- free (rec->info[2]);
- if (reckeys.buf_used > 0)
- {
- rec->info[2] = malloc (reckeys.buf_used);
- rec->size[2] = reckeys.buf_used;
- memcpy (rec->info[2], reckeys.buf, rec->size[2]);
- }
- else
+ /* open input file */
+ if ((fd = open (fname, O_RDONLY)) == -1)
{
- rec->info[2] = NULL;
- rec->size[2] = 0;
+ logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
+ return 0;
}
- free (rec->info[3]);
- rec->info[3] = rec_strdup (rGroup->databaseName, &rec->size[3]);
-
- rec_put (records, &rec);
+ recordExtract (sysno, fname, rGroup, deleteFlag, fd,
+ file_type, recType);
+ close (fd);
return 1;
}
+