Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

autokey.c File Reference

#include "rcs_defs.hh"
#include "autokey.h"
#include "_timer.h"
#include "rcs_prnt.hh"

Include dependency graph for autokey.c:

Include dependency graph

Go to the source code of this file.

Defines

#define AUTOKEY_WRITE_OK   (1)

Functions

int autokey_entry_compare (struct AUTOKEY_TABLE_ENTRY *entry, const char *name, int chksum)
int autokey_entry_write (struct AUTOKEY_TABLE_ENTRY *entry, const char *name, int chksum)
int autokey_entry_check (struct AUTOKEY_TABLE_ENTRY *entry)
int autokey_getkey (void *table, int max, const char *name)
int autokey_releasekey (void *table, int max, const char *name, int key)


Define Documentation

#define AUTOKEY_WRITE_OK   (1)
 

Definition at line 17 of file autokey.c.


Function Documentation

int autokey_entry_compare struct AUTOKEY_TABLE_ENTRY   entry,
const char *    name,
int    chksum
 

Definition at line 20 of file autokey.c.

Referenced by autokey_getkey(), and autokey_releasekey().

00022 {
00023   int i;
00024   if (0 == entry || 0 == name)
00025     {
00026       return -1;
00027     }
00028   if (chksum != entry->chksum)
00029     {
00030       return (chksum > entry->chksum ? 1 : -1);
00031     }
00032   for (i = 0; i < AUTOKEY_NAMELENGTH; i++)
00033     {
00034       if (name[i] != entry->name[i])
00035         {
00036           return (name[i] > entry->name[i] ? 1 : -1);
00037         }
00038       if (0 == name[i])
00039         {
00040           break;
00041         }
00042     }
00043   return (0);
00044 }

int autokey_entry_write struct AUTOKEY_TABLE_ENTRY   entry,
const char *    name,
int    chksum
 

Definition at line 47 of file autokey.c.

Referenced by autokey_getkey().

00049 {
00050   int i;
00051   int name_end_hit = 0;
00052   if (0 == entry || 0 == name)
00053     {
00054       return -1;
00055     }
00056   for (i = 0; i < AUTOKEY_NAMELENGTH; i++)
00057     {
00058       if (!name_end_hit)
00059         {
00060           entry->name[i] = name[i];
00061         }
00062       else
00063         {
00064           entry->name[i] = 0;
00065         }
00066       if (0 == name[i])
00067         {
00068           name_end_hit = 1;
00069         }
00070     }
00071   entry->chksum = chksum;
00072 }

int autokey_entry_check struct AUTOKEY_TABLE_ENTRY   entry
 

Definition at line 75 of file autokey.c.

Referenced by autokey_getkey().

00076 {
00077   unsigned int chksum = 0;
00078   int i;
00079   if (0 == entry)
00080     {
00081       return 0;
00082     }
00083   if (!entry->name[0])
00084     {
00085       return 0;
00086     }
00087   for (i = 0; i < AUTOKEY_NAMELENGTH && entry->name[i] != 0; i++)
00088     {
00089       chksum += (unsigned int) entry->name[i];
00090     }
00091   return (chksum == entry->chksum);
00092 }

int autokey_getkey void *    table,
int    max,
const char *    name
 

Definition at line 95 of file autokey.c.

00096 {
00097   int index;
00098   struct AUTOKEY_TABLE_ENTRY *ktable = (struct AUTOKEY_TABLE_ENTRY *) table;
00099   unsigned int chksum = 0;
00100   int i;
00101   int tries;
00102   int table_full;
00103   if (0 == table || max < 1 || 0 == name)
00104     {
00105 #ifndef NO_STDIO_SUPPORT
00106       rcs_print_error ("Bad parameters to autokey_getkey(%p,%d,%p)\n",
00107                        table, max, name);
00108 #endif
00109       return -1;
00110     }
00111 
00112   if (name[0] == 0)
00113     {
00114 #ifndef NO_STDIO_SUPPORT
00115       rcs_print_error ("Bad name for autokey_getkey()\n");
00116 #endif
00117       return -1;
00118     }
00119 
00120   for (i = 0; i < AUTOKEY_NAMELENGTH && name[i] != 0; i++)
00121     {
00122       chksum += (unsigned int) name[i];
00123     }
00124 
00125 
00126   /* Check to see if this name is already in the table if so return its entry 
00127      number. */
00128   for (index = 0; index < max; index++)
00129     {
00130       if (!autokey_entry_compare (&(ktable[index]), name, chksum))
00131         {
00132           rcs_print ("autkey found  %d for %s already in table\n", index,
00133                      name);
00134           return index;
00135         }
00136     }
00137 
00138   tries = 0;
00139 
00140   do
00141     {
00142       /* Search the table for and empty or invalid entry and try to claim it. */
00143       table_full = 1;
00144       for (index = 0; index < max; index++)
00145         {
00146           if (!autokey_entry_check (&(ktable[index])))
00147             {
00148               // Empty or invalid table entry let's claim it.
00149               autokey_entry_write (&(ktable[index]), name, chksum);
00150               table_full = 0;
00151               break;
00152             }
00153         }
00154       if (table_full)
00155         {
00156           rcs_print_error
00157             ("autokey table is full no room for %s in table of size %d\n",
00158              name, max);
00159           return -1;
00160         }
00161 
00162       /* Since we have no mutual exclusion we need to sleep for 10 milliseconds
00163          and check to see that it is still there. */
00164       esleep (0.01);
00165 
00166       if (!autokey_entry_compare (&(ktable[index]), name, chksum))
00167         {
00168           rcs_print ("autkey adding %d for %s\n", index, name);
00169           return index;
00170         }
00171     }
00172   while (tries < 100);
00173 #ifndef NO_STDIO_SUPPORT
00174   rcs_print_error ("autokey_getkey: timed out\n");
00175 #endif
00176   return -1;
00177 }

int autokey_releasekey void *    table,
int    max,
const char *    name,
int    key
 

Definition at line 181 of file autokey.c.

00182 {
00183   int chksum = 0;
00184   int i;
00185   struct AUTOKEY_TABLE_ENTRY *ktable = (struct AUTOKEY_TABLE_ENTRY *) table;
00186 
00187   if (0 == table || max < 1 || 0 == name || key >= max)
00188     {
00189 #ifndef NO_STDIO_SUPPORT
00190       rcs_print_error ("Bad parameters to autokey_releasekey(%p,%d,%p,%d)\n",
00191                        table, max, name, key);
00192 #endif
00193       return -1;
00194     }
00195 
00196   if (name[0] == 0)
00197     {
00198 #ifndef NO_STDIO_SUPPORT
00199       rcs_print_error ("Bad name for autokey_releasekey()\n");
00200 #endif
00201       return -1;
00202     }
00203 
00204   chksum = 0;
00205 
00206   for (i = 0; i < AUTOKEY_NAMELENGTH && name[i] != 0; i++)
00207     {
00208       chksum += (unsigned int) name[i];
00209     }
00210   if (autokey_entry_compare (&(ktable[key]), name, chksum))
00211     {
00212 #ifndef NO_STDIO_SUPPORT
00213       rcs_print_error
00214         ("autokey_releasekey entry in table for %s no longer matches key so it will not be released.\n",
00215          name);
00216 #endif
00217       return (-1);
00218     }
00219 
00220   for (i = 0; i < AUTOKEY_NAMELENGTH && name[i] != 0; i++)
00221     {
00222       ktable[key].name[i] = 0;
00223     }
00224   ktable[key].chksum = 0;
00225 }


Generated on Sun Dec 2 15:56:56 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001