home *** CD-ROM | disk | FTP | other *** search
-
- #include "address.h"
- #include <assert.h>
- #include <stdlib.h>
- #include <string.h>
- #include <pragma/exec_lib.h>
- #include <clib/alib_protos.h>
-
- // kopiere einen String in den dynamischen Speicher
- static char *copystr(char *s)
- {
- char *r = NULL;
- if (s != NULL)
- {
- int l = strlen(s)+1;
- r = malloc(l);
- if (r != NULL)
- {
- memcpy(r,s,l);
- };
- };
- return r;
- }
-
- // gebe einen String auf dem dynamischen Speicher frei
- static void freestr(char *s)
- {
- if (s != NULL)
- free(s);
- }
-
- // formatierte Ausgabe auf einer Datei für Addressausgaben
- static void printstr(FILE *f, char *header, char *s)
- {
- assert(f != NULL && header != NULL);
- if (s != NULL)
- fprintf(f,"%s%s\n",header,s)
- else
- fprintf(f,"%s <unbekannt>\n",header);
- };
-
- // alloc an address and copy parameters.
- struct Address *allocAddress(char *name, char *firstname, char *street, char *town)
- {
- struct Address *adr = malloc(sizeof(struct Address));
- if (adr)
- {
- adr->name = copystr(name);
- adr->firstname = copystr(firstname);
- adr->street = copystr(street);
- adr->town = copystr(town);
- };
- return adr;
- }
-
- // free an address
- void freeAddress(struct Address *adr)
- {
- if (adr != NULL)
- {
- freestr(adr->name);
- freestr(adr->firstname);
- freestr(adr->street);
- freestr(adr->town);
- };
- }
-
- static struct MinList addressheader;
- static struct MinList *addresslist = NULL;
-
- // add an address to the addresslist
- void addAddress(struct Address *adr)
- {
- assert(adr != NULL);
- if (addresslist == NULL)
- {
- NewList((struct List *) &addressheader);
- addresslist = &addressheader;
- };
- AddTail((struct List *) addresslist,(struct Node *) adr);
- }
-
- // alloc an address and read from ASCII mask
- struct Address *readAddressmask()
- {
- static char name[80], firstname[80], street[80], town[80];
- printf("\fAdresseingabe:\n");
- printf("Nachname: "); fflush(stdout);
- gets(name);
- printf("Vorname : "); fflush(stdout);
- gets(firstname);
- printf("Straße : "); fflush(stdout);
- gets(street);
- printf("Wohnort : "); fflush(stdout);
- gets(town);
- return allocAddress(name,firstname,street,town);
- }
-
- // write an address in an ASCII mask
- void writeAddressmask(FILE *f, struct Address *adr)
- {
- assert(f != NULL && adr != NULL);
- printstr(f,"Name : ",adr->name);
- printstr(f,"Vorname: ",adr->firstname);
- printstr(f,"Straße : ",adr->street);
- printstr(f,"Wohnort: ",adr->town);
- }
-
- // write addresslist to an ASCII mask
- void writeAddresslist(FILE *f)
- {
- assert(f != NULL);
- if (addresslist == NULL || IsListEmpty((struct List *) addresslist))
- printf("Keine Adressen in der Liste.\n")
- else
- {
- struct Address *adr = (struct Address *) addresslist->mlh_Head;
- printf("\f");
- while (adr->node.mln_Succ != NULL)
- {
- writeAddressmask(f,adr);
- fprintf(f,"\n");
- adr = (struct Address *) adr->node.mln_Succ;
- };
- };
- }
-