From cfa42174ee1be93291d2c3ee70b6b3048e4f75b2 Mon Sep 17 00:00:00 2001 From: Ross Wilson Date: Tue, 20 Oct 2015 13:00:08 +0700 Subject: [PATCH] Initial commit --- vimlac/plist.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++ vimlac/plist.h | 32 +++++++++ 2 files changed, 218 insertions(+) create mode 100644 vimlac/plist.c create mode 100644 vimlac/plist.h diff --git a/vimlac/plist.c b/vimlac/plist.c new file mode 100644 index 0000000..743c452 --- /dev/null +++ b/vimlac/plist.c @@ -0,0 +1,186 @@ +/******************************************************************************\ + * plist.c * + * ========= * + * * + * This file implements a 'plist'-like structure to store a list of * + * name/value pairs. In this incarnation the plist stores names and values * + * as char strings. * + * * + * PLIST PlistCreate(void); * + * * + * void PListInsert(PLIST plist, char *name, char *value); * + * * + * void *PlistFind(PLIST plist, char *name); * + * * + * void PlistDestroy(PLIST plist); * + * * + * void PlistDump(PLIST plist); * + * * +\******************************************************************************/ + +#include +#include +#include +#include +#include + +#include "plist.h" + + +/****** + * Define the plist header structure and data types. + ******/ + +typedef struct _PList +{ + char *name; + char *value; + struct _PList *next; +} PList; + + +/****************************************************************************** + Name : error() +Description : printf()-like error routine. + Parameters : params like printf() + Returns : Doesn't, calls exit(). + Comments : + ******************************************************************************/ +static void +error(char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + va_end(ap); + + exit(1); +} + + +/****************************************************************************** + Name : PlistCreate() +Description : Routine to create a plist. + Parameters : + Returns : Address (void *) of plist header. + Comments : + ******************************************************************************/ +PLIST +PlistCreate(void) +{ + PList *plist = malloc(sizeof(PList *)); + + if (plist == NULL) + error("Out of memory."); + + plist = NULL; + + return (PLIST) plist; +} + + +/****************************************************************************** + Name : PlistInsert() +Description : Function to insert new record into the property list. + Parameters : htable - address (void *) of plist header + : record - address (void *) of record to insert + : recszie - size of record (bytes) + Returns : + Comments : This routine takes a copy of the user record. + : New record placed in front of any existing equal record. + ******************************************************************************/ +void +PListInsert(PLIST plist, char *name, char *value) +{ + PList *newrec = malloc(sizeof(PList)); + char *newname = malloc(strlen(name)+1); + char *newvalue = malloc(strlen(value)+1); + PList *hdr = (PList *) plist; + + if (newrec == NULL || newname == NULL || newvalue == NULL) + error("Out of memory"); + + strcpy(newname, name); + strcpy(newvalue, value); + + newrec->next = NULL; + newrec->name = newname; + newrec->value = newvalue; + +// plist = (PList *) plist; + + newrec->next = hdr; + hdr->next = newrec; +} + + +/****************************************************************************** + Name : PlistLookup() +Description : Look for a record in a plist. + Parameters : plist - plist to look in + : name - the name to look for + Returns : Returns pointer to value string or NULL if not found. + Comments : + ******************************************************************************/ +char * +PlistFind(PLIST plist, char *name) +{ + PList *hdr = (PList *) plist; + + while (hdr) + { + if (strcmp(hdr->name, name) == 0) + return hdr->value; + } + + return NULL; +} + + +/****************************************************************************** + Name : PlistDestroy() +Description : Destroy a property list. + Parameters : plist - plist to destroy + Returns : + Comments : + ******************************************************************************/ +void +PlistDestroy(PLIST plist) +{ + PList *hdr = (PList *) plist; + + while (hdr) + { + PList *next = hdr->next; + free(hdr->name); + free(hdr->value); + free(hdr); + hdr = next; + } +} + + +/****************************************************************************** + Name : PlistDump() +Description : Dump the plist to a specified open output FILE. + Parameters : plist - plist to dump + : output - FILE pointer to write to (stdout if NULL) + Returns : + Comments : + ******************************************************************************/ +void +PlistDump(PLIST plist, FILE *output) +{ + PList *hdr = (PList *) plist; + + if (output == NULL) + output = stdout; + + while (hdr) + { + fprintf(output, "%s: %s\n", hdr->name, hdr->value); + hdr = hdr->next; + } +} diff --git a/vimlac/plist.h b/vimlac/plist.h new file mode 100644 index 0000000..65b51e6 --- /dev/null +++ b/vimlac/plist.h @@ -0,0 +1,32 @@ +/******************************************************************************\ + * plist.h * + * ========= * + * * + * This file advertises the plist system interface. * + * * +\******************************************************************************/ + +#ifndef PLIST_H +#define PLIST_H + +#include + +/****** + * Typedefs for user. + ******/ + +typedef void *PLIST; + + +/****** + * Function prototypes. + ******/ + +PLIST PlistCreate(void); +void PListInsert(PLIST plist, char *name, char *value); +char *PlistFind(PLIST plist, char *name); +void PlistDestroy(PLIST plist); +void PlistDump(PLIST plist, FILE *output); + + +#endif