#include "pcache.h"
-static int PData_invariant(const PData *my) {
+static int PData_invariant(const PData * my)
+{
int retVal;
retVal = my != NULL
&& my->name != NULL
&& my->idx != NULL
&& (my->type == PInt);
-
+
return retVal;
}
-static PData *newPInt(const char *name, const char *idx, unsigned long int value) {
+static PData *newPInt(const char *name, const char *idx, unsigned long int value)
+{
PData *my = malloc(sizeof(PData));
- my->name = malloc(strlen(name)+1);
+ my->name = malloc(strlen(name) + 1);
strcpy(my->name, name);
- my->idx = malloc(strlen(idx)+1);
+ my->idx = malloc(strlen(idx) + 1);
strcpy(my->idx, idx);
my->type = PInt;
return my;
}
-static void delPData(PData *my) {
+static void delPData(PData * my)
+{
if (my != NULL) {
switch (my->type) {
case PInt:
}
}
-static int compare(const PData *my, const char *name, const char *idx) {
+static int compare(const PData * my, const char *name, const char *idx)
+{
int retVal;
retVal = strcmp(my->idx, idx);
if (retVal == 0) {
retVal = strcmp(my->name, name);
}
-
return retVal < 0 ? -1 : retVal > 0 ? 1 : 0;
}
-static PCache *insert(PCache *my, const char *name, const char *idx, unsigned long int value) {
+static PCache *insert(PCache * my, const char *name, const char *idx, unsigned long int value)
+{
if (my == NULL) {
my = malloc(sizeof(PCache));
my->r = NULL;
my->data = newPInt(name, idx, value);
} else {
switch (compare(my->data, name, idx)) {
- case 0:
- delPData(my->data);
- my->data = newPInt(name, idx, value);
- break;
- case -1:
- my->l = insert(my->l, name, idx, value);
- break;
- case 1:
- my->r = insert(my->r, name, idx, value);
- break;
+ case 0:
+ delPData(my->data);
+ my->data = newPInt(name, idx, value);
+ break;
+ case -1:
+ my->l = insert(my->l, name, idx, value);
+ break;
+ case 1:
+ my->r = insert(my->r, name, idx, value);
+ break;
}
}
return my;
}
-static PData *find(const PCache *my, const char *name, const char *idx) {
+static PData *find(const PCache * my, const char *name, const char *idx)
+{
PData *retVal;
if (my == NULL) {
retVal = NULL;
} else {
switch (compare(my->data, name, idx)) {
- case 0:
- retVal = my->data;
- break;
- case -1:
- retVal = find(my->l, name, idx);
- break;
- case 1:
- retVal = find(my->r, name, idx);
- break;
+ case 0:
+ retVal = my->data;
+ break;
+ case -1:
+ retVal = find(my->l, name, idx);
+ break;
+ case 1:
+ retVal = find(my->r, name, idx);
+ break;
}
}
return retVal;
}
-int PCache_storeInt(PCache **my, const char *name, const char *idx, unsigned long int value) {
+int PCache_storeInt(PCache ** my, const char *name, const char *idx, unsigned long int value)
+{
- *my = insert(*my, name, idx, value);
+ *my = insert(*my, name, idx, value);
return 0;
}
-
-int PCache_getInt(const PCache *my, const char *name, const char *idx, unsigned long int *value) {
+
+int PCache_getInt(const PCache * my, const char *name, const char *idx, unsigned long int *value)
+{
int retVal;
const PData *data;
+
#ifndef PCHE_H
#define PCHE_H
-enum PType { PInt };
+enum PType {
+ PInt
+};
+
#if 0
-enum PType { PInt, PString, PIntArray, PStringArray };
+enum PType {
+ PInt, PString, PIntArray, PStringArray
+};
+
#endif
struct PDataS {
char *PString;
unsigned long int *PIntArray;
char **PStringArray;
- } value;
+ }
+ value;
};
-typedef struct PDataS PData;
+typedef struct PDataS PData;
struct PCacheS {
struct PCacheS *r;
};
typedef struct PCacheS PCache;
-int PCache_storeInt(PCache **my, const char *name, const char *idx, unsigned long int value);
-int PCache_getInt(const PCache *my, const char *name, const char *idx, unsigned long int *value);
+int PCache_storeInt(PCache ** my, const char *name, const char *idx, unsigned long int value);
+int PCache_getInt(const PCache * my, const char *name, const char *idx, unsigned long int *value);
#endif