First class data visualization and communication library with embedded devices. Code is maintained at github.com/Overdrivr/Telemetry

Dependents:   telemetry_car_demo telemetry_demo_FRDM-TFC telemetry_example_01 telemetry_indexed_data_demo ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dictionnary.cpp Source File

dictionnary.cpp

00001 #include "dictionnary.h"
00002 #include "stdlib.h"
00003 #include "stdint.h"
00004 #include "string.h"
00005 
00006 /* hash: form hash value for string s */
00007 unsigned hash(const char * s)
00008 {
00009     unsigned hashval;
00010     for (hashval = 0; *s != '\0'; s++)
00011       hashval = *s + 31 * hashval;
00012     return hashval % HASHSIZE;
00013 }
00014 
00015 char * strdup(const char * s) /* make a duplicate of s */
00016 {
00017     char *p;
00018     p = (char *) malloc(strlen(s)+1); /* +1 for ’\0’ */
00019     if (p != NULL)
00020        strcpy(p, s);
00021     return p;
00022 }
00023 
00024 void init_entry(struct nlist * entry)
00025 {
00026   entry->ptr_f32 = NULL;
00027   entry->ptr_u8 = NULL;
00028   entry->ptr_u16 = NULL;
00029   entry->ptr_u32 = NULL;
00030   entry->ptr_i8 = NULL;
00031   entry->ptr_i16 = NULL;
00032   entry->ptr_i32 = NULL;
00033   entry->ptr_function = NULL;
00034 }
00035 
00036 void init_table(struct nlist ** hashtab)
00037 {
00038   uint32_t i = 0;
00039   for(i = 0 ; i < HASHSIZE ; i++)
00040   {
00041     hashtab[i] = NULL;
00042   }
00043 }
00044 
00045 /* lookup: look for s in hashtab */
00046 struct nlist *lookup(struct nlist ** hashtab, const char * key)
00047 {
00048     struct nlist *np;
00049 
00050     for (np = hashtab[hash(key)]; np != NULL; np = np->next)
00051     {
00052       if (strcmp(key, np->key) == 0)
00053       {
00054         return np; /* found */
00055       }
00056     }
00057     return NULL; /* not found */
00058 }
00059 
00060 struct nlist * install(struct nlist ** hashtab, const char * key, void * ptr, ptr_type type)
00061 {
00062     struct nlist * np;
00063     unsigned hashval;
00064 
00065     if ((np = lookup(hashtab, key)) == NULL)
00066     {
00067         // Allocate new hastable entry and initialize it
00068         np = (struct nlist *) malloc(sizeof(*np));
00069 
00070         // If allocation failed
00071         if (np == NULL || (np->key = strdup(key)) == NULL)
00072           return NULL;
00073 
00074         init_entry(np);
00075 
00076         hashval = hash(key);
00077         np->next = hashtab[hashval];
00078         hashtab[hashval] = np;
00079     }
00080 
00081     // Set value
00082     switch(type)
00083     {
00084         case ptr_f32:
00085           np->ptr_f32 = (float *)(ptr);
00086           break;
00087         case ptr_u8:
00088           np->ptr_u8 = (uint8_t *)(ptr);
00089           break;
00090         case ptr_u16:
00091           np->ptr_u16 = (uint16_t *)(ptr);
00092           break;
00093         case ptr_u32:
00094           np->ptr_u32 = (uint32_t *)(ptr);
00095           break;
00096         case ptr_i8:
00097           np->ptr_i8 = (int8_t *)(ptr);
00098           break;
00099         case ptr_i16:
00100           np->ptr_i16 = (int16_t *)(ptr);
00101           break;
00102         case ptr_i32:
00103           np->ptr_i32 = (int32_t *)(ptr);
00104           break;
00105         case ptr_function:
00106           np->ptr_function = ptr;
00107           break;
00108     }
00109 
00110     return np;
00111 }