ftp.nice.ch/pub/next/database/plz/NXplz.1.1.NI.s.tar.gz#/NXplz-1.1/ortarda.c

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

/* $Id: ortarda.c,v 1.1 1993/06/28 09:31:07 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 <ctype.h>
#ifdef __NeXT__
#include <stdlib.h>
#else
#include <malloc.h>
#endif
#include <stdio.h>
#include <string.h>

#include "message.h"
#include "plzfile.h"
#include "ortarda.h"

static PlzFile ortarda = {(FILE *) 0, 0, 0, 0};

static Shortcut abk[] =
{
    {"St.",      "Sankt"},
    {"st.",      "sankt"},
    {"ST.",      "SANKT"},
    {"St ",      "Sankt "},
    {"st ",      "sankt "},
    {"ST ",      "SANKT "},
    {(char *) 0, (char *) 0},
};




OrtardaSet *OrtardaSetSucheOrt (OrtardaSet *list, char *ortsname)
{
    OrtardaSet *result = (OrtardaSet *) malloc (sizeof (OrtardaSet));
    Ortarda *l;
    Ortarda *r;
    int i;
    char *e_ortsname;
    char *help;

    if (result == (OrtardaSet *) 0)
    {
	OutOfMemory ("RematchPlace");
	return result;
    }

    result->list = (Ortarda *) malloc ((unsigned int) (list->entries * 
				     sizeof (Ortarda) + 2));
    if (result->list == (Ortarda *) 0)
    {
	OutOfMemory ("RematchPlace");
	return (OrtardaSet *) 0;
    }
    result->entries = 0;

    e_ortsname = AbkuerzungenExpandieren (ortsname, abk);
    help = IsoToCapitals (e_ortsname);
    free (e_ortsname);
    e_ortsname = CStringToFixed (help, sizeof (((Ortarda *) 0)->oname_sort_a));
    free (help);

    for (l = list->list, r = result->list, i = 0; i < list->entries; i++, l++)
    {
	if (strncmp (l->oname_sort_a, e_ortsname, sizeof (l->oname_sort_a))
	== 0)
	{
	    strncpy ((char *)r++, (char *)l, sizeof (Ortarda));
	    result->entries++;
	}
    }
    free (e_ortsname);
    return result;
}




OrtardaSet *OrtardaSucheOrt (char *ortsname)
{
    long i;
    long pos;
    long entries;
    Range *foundEntries;
    Ortarda searchKeys;
    OrtardaSet *result = (OrtardaSet *) malloc (sizeof (OrtardaSet));
    Ortarda *r;
    char *help1, *help2;
    char *e_ortsname;
    int max;
    unsigned char *c;

    if (result == (OrtardaSet *) 0)
    {
	OutOfMemory ("SearchForPlace");
	return result;
    }

    OpenPlzFile (&ortarda, "ortarda");
    if (ortarda.f == (FILE *) 0)
	return (OrtardaSet *) 0;

    e_ortsname = AbkuerzungenExpandieren (ortsname, abk);
    help1 = IsoToCapitals (e_ortsname);
    help2 = CStringToFixed (help1, sizeof (((Ortarda *) 0)->oname_sort_a));
    strncpy (searchKeys.oname_sort_a, help2, sizeof (searchKeys.oname_sort_a));
    free (help1);
    free (help2);

    /* Auf Grund von Sortierfehlern in der Datei "ortarda" dürfen wir nur nach
     * dem ersten Teil des Ortsnamens suchen. Beispiel: "Mülheim-Kärlich" ->
     * suche "Mülheim". Die Variable "max" enthält die signifikante Länge.
     */
    i = 0;
    for (c = (unsigned char *) e_ortsname; *c && !ispunct (*c); c++)
	if (*c == ISO_Ae || *c == ISO_Oe || *c == ISO_Ue || *c == ISO_ae
	|| *c == ISO_oe || *c == ISO_ue || *c == ISO_ss || *c == IBM_Ae 
	|| *c == IBM_Oe || *c == IBM_Ue || *c == IBM_ae	|| *c == IBM_oe 
	|| *c == IBM_ue || *c == IBM_ss)
	    i++;
    if (ispunct (*c))
    {
	while ((ispunct (*(c-1)) || isspace (*(c-1)))
	&& (int) c >= (int) e_ortsname)
	    c--;
	max = (char *) c - e_ortsname + i;
    }
    else
	max = sizeof (((Ortarda *) 0)->oname_sort_a);
    free (e_ortsname);

    if (max == 0)
    {
	result->entries = 0;
	return result;
    }

    foundEntries = BinarySearchAll (ortarda.f, ortarda.recordLength, 0L,
				    ortarda.entries, 
				    (int) (((Ortarda *) 0)->oname_sort_a - 
				    ((Ortarda *) 0)->ala), 
				    max, searchKeys.oname_sort_a, 
				    (int) (((Ortarda *) 0)->ortname_a - 
				    ((Ortarda *) 0)->ala),
				    userInterfaceMethods.OrtardaRecordRead,
				    &ortarda);
    if (foundEntries->first == -1 && foundEntries->last == -1)
	result->entries = 0;
    else
	result->entries = foundEntries->last - foundEntries->first + 1;

    Note ("\n");
    if (result->entries == 0)
    {
	free (foundEntries);
	result->list = (Ortarda *) 0;
	return result;
    }
    else
	result->list = (Ortarda *) malloc ((unsigned int) (result->entries * 
					sizeof (Ortarda) + 2));

    if (result->list == (Ortarda *) 0)
    {
	free (foundEntries);
	OutOfMemory ("SearchForPlace");
	return (OrtardaSet *) 0;
    }

    pos = foundEntries->first;
    entries = 0;
    for (r = result->list, i = 0; i < result->entries; i++, r++)
    {
	fgetr (r, pos++, &ortarda);
      entries++;
    }
    free (foundEntries);
    result->entries = entries;

    return result;
}




OrtardaSet *OrtardaSetSuchePlzAlt (OrtardaSet *list, char *plzalt,
				   char verkehrsgebiet)
{
    OrtardaSet *result = (OrtardaSet *) malloc (sizeof (OrtardaSet));
    Ortarda searchKeys;
    Ortarda *l;
    Ortarda *r;
    char *help1;
    int i;

    if (result == (OrtardaSet *) 0)
    {
	OutOfMemory ("SearchForPlace");
	return result;
    }

    result->list = (Ortarda *) malloc ((unsigned int) (list->entries * 
				     sizeof (Ortarda) + 2));
    if (result->list == (Ortarda *) 0)
    {
	OutOfMemory ("SearchForPlace");
	return (OrtardaSet *) 0;
    }
    result->entries = 0;

    help1 = OldPlz (plzalt, verkehrsgebiet);
    searchKeys.plz_w_o[0] = *help1;
    strncpy (searchKeys.plzalt, help1 + 1, 4);

    for (l = list->list, r = result->list, i = 0; i < list->entries; i++, l++)
    {
	if (searchKeys.plz_w_o[0] == (l->plz_w_o)[0] &&
	   (strncmp (searchKeys.plzalt, l->plzalt, sizeof (l->plzalt)) == 0))
	{
	    strncpy ((char *)r++, (char *)l, sizeof (Ortarda));
	    result->entries++;
	}
    }
    return result;
}




OrtardaSet *OrtardaSetSuchePostanstalt (OrtardaSet *list, char *postanstalt)
{
    OrtardaSet *result = (OrtardaSet *) malloc (sizeof (OrtardaSet));
    char fPostanstalt[sizeof (((Ortarda *) 0)->npanst_a)];
    Ortarda *l;
    Ortarda *r;
    int i;

    if (result == (OrtardaSet *) 0)
    {
	OutOfMemory ("SuchePostanstalt");
	return result;
    }

    result->list = (Ortarda *) malloc ((unsigned int) (list->entries * 
				     sizeof (Ortarda) + 2));
    if (result->list == (Ortarda *) 0)
    {
	OutOfMemory ("SuchePostanstalt");
	return (OrtardaSet *) 0;
    }
    result->entries = 0;

    for (i = 0; i < sizeof (fPostanstalt) && *postanstalt; i++)
	fPostanstalt[i] = *postanstalt++;
    for (; i < sizeof (fPostanstalt); i++)
	fPostanstalt[i] = ' ';

    for (l = list->list, r = result->list, i = 0; i < list->entries; i++, l++)
    {
	if (strncmp (l->npanst_a, fPostanstalt, sizeof (fPostanstalt)) == 0)
	{
	    strncpy ((char *)r++, (char *)l, sizeof (Ortarda));
	    result->entries++;
	}
    }
    return result;
}




#define IsMultipleNewStreetPlz(u) (IsMultipleNewPlz (u->nplzo_z))
#define IsMultipleNewPoboxPlz(u) (IsMultipleNewPlz (u->nplzo_p))

char *OrtardaSetOrtEindeutig (OrtardaSet *ortardaSet)
{
    int i;
    static char alort[sizeof (((Ortarda *) 0)->alort) + 1];
    Ortarda *u;
    Boolean unique = True;

    if (ortardaSet->entries > 0)
    {
	u = ortardaSet->list;
	strncpy (alort, u->alort, sizeof (u->alort));
	alort[sizeof (alort) - 1] = '\0';
    }
    else
	return "";

    for (i = 1, u++; i < ortardaSet->entries && unique; i++, u++)
	if (strncmp (alort, u->alort, sizeof (u->alort)) != 0)
	    unique = False;
    if (unique)
	return alort;
    else
	return "";
}




char *OrtardaSetPlzAltEindeutig (OrtardaSet *ortardaSet)
{
    int i;
    static char plz[6];
    Ortarda *u;
    Boolean unique = True;

    if (ortardaSet->entries > 0)
    {
	u = ortardaSet->list;
	strncpy (plz, u->plz_w_o, 5);
	plz[5] = '\0';
    }
    else
	return "";

    for (i = 1, u++; i < ortardaSet->entries && unique; i++, u++)
	if (strncmp (plz, u->plz_w_o, 5) != 0)
	    unique = False;
    if (unique)
	return plz;
    else
	return "";
}

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