ftp.nice.ch/pub/next/database/plz/xplz.3.4.s.tar.gz#/xplz-3.4/batch.c

This is batch.c in view mode; [Download] [Up]

/* $Id: batch.c,v 2.7 1993/06/04 14:46:52 klute Exp klute $ */

/* 
 * Copyright 1993 Rainer Klute <klute@irb.informatik.uni-dortmund.de>
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation. The author makes no representations about the suitability
 * of this software for any purpose. It is provided "as is" without express
 * or implied warranty.
 *
 */

#include <malloc.h>
#include <string.h>

#include "xplz.h"
#include "message.h"
#include "batch.h"
#include "convert.h"
#include "parse.h"




/*
 * OpenFile
 */

static void OpenFile (FILE **f, char *filename, char *type, char *filetype,
		      FILE *fallback)
{
    if (*f == (FILE *) 0)
    {
	*f = fopen (filename, type);
	if (*f == (FILE *) 0)
	    *f = fallback;
    }
}




/*
 * BatchConvert: gibt "True" zurück, wenn fertig, bei Unterbrechungen "False".
 */

Boolean BatchConvert (Batch *batch)
{
    char *help;
    char *strassenFeld, *plzFeld, *ortsFeld;
    char *strasse, *hausnummer, *postfach, *plz_alt, *ort, *postanstalt,
	 *ortsteil;
    char record[5000];
    Boolean done = False;

    switch (batch->status)
    {

	case BatchInit:
	{
	    OpenFile (&batch->ifl, batch->inputFile, "r", "Eingabedatei",
		      stdin);
	    OpenFile (&batch->ofl, batch->outputFile, "w", "Ausgabedatei",
		      stdout);
	    OpenFile (&batch->efl, batch->errorFile, "w", "Fehlerdatei",
		      stderr);
	    if (batch->ifl == (FILE *) 0 || batch->ofl == (FILE *) 0
	    || batch->efl == (FILE *) 0)
		batch->status = BatchError;
	    else
		batch->status = BatchRunning;
	    break;
	}

	case BatchRunning:
	{
	    *record = '\0';
	    fgets (record, sizeof (record), batch->ifl);
	    if (!*record)
	    {
		batch->status = BatchFinished;
		break;
	    }
	    if (batch->inputRecord != (char *) 0)
		free (batch->inputRecord);
	    batch->inputRecord = IbmToIso (record);
	    strassenFeld =
	    ReadSeparatedField (batch->inputRecord, batch->columnStreet,
				batch->separator);
	    ortsFeld =
	    ReadSeparatedField (batch->inputRecord, batch->columnPlace,
				batch->separator);
	    if (batch->columnPlz > 0 && batch->columnPlz != batch->columnPlace)
	    {
		plzFeld =
		ReadSeparatedField (batch->inputRecord, batch->columnPlz,
				    batch->separator);
		help = (char *) malloc (strlen (plzFeld) + strlen (ortsFeld)
					+ 2);
		sprintf (help, "%s %s", plzFeld, ortsFeld);
		free (plzFeld);
		free (ortsFeld);
		ortsFeld = help;
	    }		
	    ParseStrasse (strassenFeld, &strasse, &hausnummer, &postfach);
	    ParseOrt (ortsFeld, &plz_alt, &ort, &postanstalt, &ortsteil);
	    if (batch->beforeConvertCallback != (void (*)()) 0)
		batch->beforeConvertCallback (batch, strasse, hausnummer,
					      postfach, plz_alt, ort,
					      postanstalt, ortsteil);
	    ConvertPlz (batch->pc, strasse, hausnummer, postfach, plz_alt, ort,
			postanstalt, ortsteil);
	    free (strasse);
	    free (hausnummer);
	    free (postfach);
	    free (plz_alt);
	    free (ort);
	    free (postanstalt);
	    free (ortsteil);
	    if (batch->afterConvertCallback != (void (*)()) 0)
	        batch->afterConvertCallback (batch);
	    if (batch->pc->status == PlzPlzNeuGefunden)
		batch->successCallback (batch);
	    else
		batch->failureCallback (batch);
	    free (strassenFeld);
	    free (ortsFeld);
	    if (feof (batch->ifl))
		batch->status = BatchFinished;
	    break;
	}

	case BatchInterrupted:
	{
	    done = True;
	    break;
	}


	case BatchFinished:
	{
	    if (batch->ifl != stdin)
		fclose (batch->ifl);
	    if (batch->ofl != stdout)
		fclose (batch->ofl);
	    if (batch->efl != stderr)
		fclose (batch->efl);
	    if (batch->finishedCallback != (void (*)()) 0)
		batch->finishedCallback (batch);
	    done = True;
	    break;
	}

	default:
	{
	    Message ("Interner Fehler in \"BatchConvert\".\n");
	    batch->status = BatchError;
	    done = True;
	}
    }

    return done;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.