engine/hlbrlib.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <ctype.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include "hlbr.h"
00006 #include "hlbrlib.h"
00007 
00008 /*********************
00009  * Log file functions
00010  *********************/
00011 
00017 FILE* LogFile(LogFileRec* log)
00018 {
00019 #ifdef KEEP_LOGFILE_OPEN
00020         if (log->fp == NULL)
00021                 log->fp = fopen(log->fname, "a");
00022 #else
00023         log->fp = fopen(log->fname, "a");
00024 #endif
00025         if (!(log->fp)) {
00026                 PRINTERROR1("LogFile: Couldn't open file \"%s\" for appending\n",
00027                             log->fname);
00028                 return NULL;
00029         }
00030 
00031         return log->fp;
00032 } 
00033 
00039 void CloseLogFile(LogFileRec* log)
00040 {
00041 #ifndef KEEP_LOGFILE_OPEN
00042         if (log->fp != NULL)
00043                 fclose(log->fp);
00044 #endif
00045 } 
00046 
00053 int LogMessage(char* Message, void* Data)
00054 {
00055         FILE*           fp;
00056         
00057         DEBUGPATH;
00058 
00059         if (!Data) {
00060                 //PRINTERROR("I must have a filename to write to!\n");
00061                 //return FALSE;
00062                 fp = stdin;
00063         } else {
00064                 fp = fopen(((LogFileRec*)Data)->fname, "a");
00065                 if (!LogFile((LogFileRec*)Data))
00066                         return FALSE;
00067         }
00068         
00069         fwrite(Message, strlen(Message), 1, fp);
00070         fwrite("\n", 1, 1, fp);
00071         
00072         if (Data)
00073                 CloseLogFile((LogFileRec*)Data);
00074         
00075         return TRUE;
00076 }
00077 
00078 
00079 
00080 /*********************/ 
00081 /* STRING FUNCTIONS */
00082 /*******************/ 
00083 
00084 
00088 char *RmSpace(char *s)
00089 {
00090     char *p;
00091 
00092     for (p = s + strlen(s) - 1; ((isspace(*p)) && (p >= s)); p--);
00093     if (p != s + strlen(s) - 1)
00094         *(p + 1) = 0;
00095     for (p = s; ((isspace(*p)) && (*p)); p++);
00096     if (p != s)
00097         strcpy(s, p);
00098 
00099     return (char *) s;
00100 }
00101 
00102 /* 
00103   This function would be called from a ParseArgs action.
00104   Returns: allocated buffer that must be freed on success
00105   Returns: NULL on error or if the option did not match.
00106 */
00107 
00108 char *ParseCmp(char *name, char *buf)
00109 {
00110     char opt[255];
00111 
00112     if (!buf[0] || !name[0])
00113         return NULL;
00114 
00115     snprintf(opt, sizeof(opt), "%s=", name);
00116     if (strncasecmp(buf, opt, strlen(name) + 1) == 0) {
00117         DBG(
00118                 PRINTERROR3("%s(%s->%s) \n", __FUNCTION__, name,
00119                            (strchr(buf, '=') + 1)));
00120         return (char *) strdup(strchr(buf, '=') + 1);
00121     }
00122     return NULL;
00123 }
00124 
00125 /*************************/
00126 /* END STRING FUNCTIONS */
00127 /***********************/
00128 
00129 
00130 
00131 /**********************/
00132 /*** LINKED LISTS  ***/
00133 /********************/
00134 
00144 QueueList *ListAdd(char *ss, QueueList *q, char separator)
00145 {
00146    QueueList *ll, *z;
00147    char *s, *ptr, *p;
00148 
00149    s = strdup(ss);
00150    MALLOC_CHECK(s);
00151    ptr = (char *) MALLOC(strlen(ss));
00152    MALLOC_CHECK(ptr);
00153 
00154    do {
00155       p = strchr(s, separator);
00156       if (p != NULL) {
00157          *p = 0;
00158          p++;
00159          strcpy(ptr, p);
00160       } else
00161          *ptr = 0;
00162       RmSpace(s);
00163       RmSpace(ptr);
00164       MALLOC_CHECK( ll = (QueueList *) MALLOC(sizeof(QueueList)) );
00165       ll->next = NULL;
00166       MALLOC_CHECK( ll->item = (char *) MALLOC(strlen(s) + 1) );
00167       strcpy(ll->item, s);
00168       if (q == NULL)
00169          q = ll;
00170       else {
00171          z = q;
00172          while (z->next != NULL)
00173             z = z->next;
00174          z->next = ll;
00175       }
00176       *s = 0;
00177       strcpy(s, ptr);
00178    }
00179    while (s[0]);
00180 
00181    FREE(s);
00182    FREE(ptr);
00183    return q;
00184 }
00185 
00191 QueueList *ListDel(char *s, QueueList *q, int *retval)
00192 {
00193    QueueList *ll, *list, *old;
00194 
00195    ll = q;
00196    list = q;
00197    old = q;
00198    *retval = 0;
00199 
00200    while (ll != NULL) {
00201       if (strcasecmp(ll->item, s) == 0) {
00202          if (ll == list) {
00203             list = (ll->next);
00204             FREE(ll->item);
00205             FREE(ll);
00206             ll = list;
00207          } else {
00208             old->next = ll->next;
00209             FREE(ll->item);
00210             FREE(ll);
00211             ll = old->next;
00212          }
00213          *retval = 1;
00214       } else {
00215          old = ll;
00216          ll = ll->next;
00217       }
00218    }
00219    return list;
00220 }
00221 
00222 /* clear out a list */
00223 void ListClear(QueueList *list)
00224 {
00225    QueueList *ll, *q;
00226    ll = list;
00227    while (ll != NULL) {
00228       q = ll->next;
00229       FREE(ll->item);
00230       FREE(ll);
00231       ll = q;
00232    }
00233 }
00234 
00235 
00236 
00237 /***********************************
00238  * Generic memory buffer functions *
00239  ***********************************/
00240 
00241 void DumpBuffer(unsigned char *data, int size, FILE *stream)
00242 {
00243         int i;
00244 
00245         if (data == NULL || size <= 0)
00246                 return;
00247 
00248         for (i=0; i < size; i++)
00249                 putc((data[i] >= 32 && data[i] <=127 ? data[i] : '.'), stream);
00250 }

Generated on Sat Jul 7 23:33:10 2007 for HLBR by  doxygen 1.5.2