Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

strings_utils.c

Go to the documentation of this file.
00001 /*! \file strings_utils.c
00002     This file contains various string utilities.
00003  */
00004 
00005 
00006 #include "strings_utils.h"
00007 
00008 
00009 
00010 /*! \brief Replace all series of '\n' by one ';' and all series of spaces by one space.
00011     \param st Pointer to a zero terminated string of characters that represents the original string.
00012     \return Upon successful completion the function returns a pointer to a zero terminated string of charaters
00013             that represents the resulting string. If an error occured (no memory), then the function returns
00014             NULL.
00015     \warning The returned pointer points to a memory location allocated within the function. Therefore you
00016              <B>MUST</B> free it (using free()). Before you free it, make sure that the pointer is not NULL.
00017  */
00018 
00019 char *make_one_line (char *st)
00020 {
00021   char *res;
00022   int  size, i, j, space, nl;
00023 
00024   /***************************************************************/
00025   /* Allocated memory for the result                             */
00026   /***************************************************************/
00027 
00028   size = strlen(st);
00029   res = (char*) malloc(sizeof(char) * (size + 1));
00030   if (res == NULL) { return NULL; }
00031 
00032   /***************************************************************/
00033   /* Start replacement                                           */
00034   /***************************************************************/
00035  
00036   j     = 0; 
00037   space = 0;
00038   nl    = 0;
00039 
00040   for (i=0; i<size; i++)
00041   {
00042     if (st[i] == ' ')
00043     {
00044       if (space == 0) { res[j++] = ' '; }
00045       space = 1;
00046       nl    = 0;
00047       continue;
00048     }
00049 
00050     space = 0;
00051 
00052     if (st[i] == '\n')
00053     {
00054       if (nl == 0) { res[j++] = ';'; }
00055       nl = 1;
00056       continue;
00057     }
00058 
00059     nl = 0;
00060 
00061     res[j++] = st[i];
00062   }
00063 
00064 
00065   res[j] = 0;
00066   return res;
00067 }
00068 
00069 /*! \example test_make_one_line.c
00070     This example shows how to use the function make_one_line().
00071  */
00072 
00073 /*! \brief Contains hexadecimal codes for the 256 values from 0 to 255. Values are written using 3 digits (01, 02, ...).
00074  */
00075 
00076 extern char *bin2hex_hexa[256];
00077 
00078 
00079 /*! \brief Split a string into fields.
00080     \param st Pointer to a string of characters to split.
00081     \param delimitor Pointer to a string of characters that defines the delimitor used to separate fieds.
00082     \param f Pointer to a structure 'fields' that contains the fields extracted from the string 'st'.
00083     \return The function returns the number of fields extracted. If an error occurs, the the function returns -1.
00084     \warning <UL>
00085                <LI>The structure 'f' contains elements that have been allocated within the function. So you must
00086                    free these elements when you don't need them any more. To do this, you should use the function
00087                    'free_fields()'.
00088                <LI>For this function, 2 (or more) delimitors in a row are seen as one and oly one delimitor.
00089                    Example: the string &quot;A;;;B&quot; has 2 fields (&quot;A&quot; and &quot;B&quot;), not 4
00090                    (assuming that the delimitor is the character &quot;;&quot;).
00091              </UL>
00092  */
00093 
00094 int s_split(char *st, char* delimitor, struct fields *f)
00095 {
00096   char*    pt;
00097   int      nb_field, field_size, i, size;
00098   char**   tab;   
00099 
00100   /***************************************************************/
00101   /* Allocate initial number of fields - INITIAL_NUMBER_OF_FIELD */
00102   /***************************************************************/
00103 
00104   size = INITIAL_NUMBER_OF_FIELD;
00105   tab = (char**)malloc(size*sizeof(char*));
00106   if (tab == NULL)
00107   {
00108     f->number_of_fields = -1;
00109     f->size             = -1;
00110     f->tabs             = NULL;
00111     return -1;
00112   }
00113 
00114   nb_field = 0; 
00115   pt       = strtok(st, delimitor);
00116   
00117   while (pt != NULL)
00118   {
00119     if (nb_field == size)
00120     {
00121       size *= 2;
00122       tab = (char**)realloc((void*)tab, size*sizeof(char*));
00123       if (tab == NULL)
00124       {
00125         for (i=0; i<nb_field; i++) { free ((void*)(tab[i])); }
00126         free ((void*)tab);
00127         f->number_of_fields = -1;
00128         f->size             = -1;
00129         f->tabs             = NULL;
00130         return -1;
00131       }
00132     } 
00133 
00134     field_size = strlen(pt)+1;
00135     tab[nb_field] = (char*)malloc(field_size*sizeof(char));
00136     if (tab[nb_field] == NULL)
00137     {
00138       for (i=0; i<nb_field; i++) { free ((void*)(tab[i])); }
00139       free ((void*)tab);
00140       f->number_of_fields = -1;
00141       f->size             = -1;
00142       f->tabs             = NULL;
00143       return -1;
00144     }
00145 
00146     strcpy (tab[nb_field], pt);
00147     nb_field++;
00148 
00149     pt = strtok (NULL, delimitor);
00150   }
00151   
00152   f->number_of_fields = nb_field;
00153   f->size             = size;
00154   f->tabs             = tab;
00155   
00156   return nb_field; 
00157 }
00158 
00159 /*! \example test_s_split.c
00160     This file shows how to use the function s_split().
00161  */
00162 
00163 
00164 /*! \brief Split a string into fields.
00165     \param st Pointer to a string of characters to split.
00166     \param delimitor Pointer to a string of characters that defines the delimitor used to separate fieds.
00167     \param f Pointer to a structure 'fields' that contains the fields extracted from the string 'st'.
00168     \return The function returns the number of fields extracted. If an error occurs, the the function returns -1.
00169     \warning <UL>
00170                <LI>The structure 'f' contains elements that have been allocated within the function. So you must
00171                    free these elements when you don't need them any more. To do this, you should use the function
00172                    'free_fields()'.
00173                <LI>This function does NOT behave like the function s_split().
00174                    Example: the string &quot;A;;;B&quot; has 4 fields (&quot;A&quot;, &quot;&quot;, &quot;&quot;
00175                    and &quot;B&quot;), not 2 (assuming that the
00176                    delimitor is the character &quot;;&quot;).
00177              </UL>
00178  */
00179 
00180 int s_split_exact(char *st, char* delimitor, struct fields *f)
00181 {
00182   char     *pt, *start, *stop;
00183   int      nb_field, field_size, i, j, size, pfields;
00184   char     **tab, **delimitors, **mem;
00185 
00186   /***************************************************************/
00187   /* Set fields' structure to error values                       */
00188   /***************************************************************/
00189 
00190   f->number_of_fields = -1;
00191   f->size             = -1;
00192   f->tabs             = NULL;
00193 
00194   /***************************************************************/
00195   /* Allocate initial number of fields - INITIAL_NUMBER_OF_FIELD */
00196   /***************************************************************/
00197 
00198   size = INITIAL_NUMBER_OF_FIELD;
00199 
00200   delimitors = (char**)malloc(size*sizeof(char*));
00201   if (delimitors == NULL) { return -1; }
00202 
00203   /***************************************************************/
00204   /* Search the string for all delimitors                        */
00205   /***************************************************************/
00206 
00207   nb_field = 0;
00208   start    = st;
00209 
00210   pt = strstr (start, delimitor);
00211   while (pt != NULL)
00212   {
00213     /*************************************************************/
00214     /* Re-allocate memory if necessary                           */
00215     /*************************************************************/
00216 
00217     if (nb_field == size)
00218     {
00219       size *= 2;
00220       mem = (char**)realloc((void*)delimitors, size * sizeof(char*));
00221       if (mem == NULL) { free(delimitors); return -1; }
00222       delimitors = mem;
00223     }
00224 
00225     /*************************************************************/
00226     /* Save delimitor's position                                 */
00227     /*************************************************************/
00228 
00229     delimitors[nb_field] = pt;
00230     nb_field++;
00231 
00232     start = pt + strlen(delimitor);
00233 
00234     pt = strstr (start, delimitor);
00235   }
00236 
00237   /***************************************************************/
00238   /* note: "A;B;"   => 2 fields                                  */
00239   /*       "A;B"    => 2 fields                                  */
00240   /***************************************************************/
00241 
00242   if (*start != 0)
00243   {
00244     if (nb_field == size)
00245     {
00246       size *= 2;
00247       mem = (char**)realloc((void*)delimitors, size * sizeof(char*));
00248       if (mem == NULL) { free(delimitors); return -1; }
00249       delimitors = mem;
00250     }
00251 
00252     delimitors[nb_field] = st + strlen(st);
00253 
00254     nb_field++;
00255   }
00256 
00257   /***************************************************************/
00258   /* Allocate fields' structure                                  */
00259   /***************************************************************/
00260 
00261   tab = (char**)malloc(nb_field * sizeof(char*));
00262   if (tab == NULL)
00263   {
00264     free (delimitors);
00265     return -1;
00266   }
00267 
00268   f->number_of_fields = nb_field;
00269   f->size             = nb_field;
00270   f->tabs             = tab;
00271 
00272   /***************************************************************/
00273   /* Now extract fields and store them into fields' structure    */
00274   /***************************************************************/
00275 
00276   pfields = 0;
00277   start   = st;
00278   for (j=0; j<nb_field; j++)
00279   {
00280     stop       = delimitors[j];
00281     field_size = (int)(stop-start);
00282 
00283     pt         = (char*) malloc ((field_size+1) * sizeof(char));
00284     if (pt == NULL)
00285     {
00286       f->number_of_fields = -1;
00287       f->size             = -1;
00288       free (delimitors);
00289       for (i=0; i<pfields; i++) { free ((void*)tab[i]); }
00290       return -1;
00291     } 
00292 
00293     strncpy (pt, start, field_size);
00294     pt[field_size] = 0;
00295 
00296     tab[pfields] = pt;
00297     pfields += 1;
00298 
00299     start = stop + strlen(delimitor);
00300   }
00301 
00302 
00303   free (delimitors);
00304   return nb_field;
00305 }
00306 
00307 /*! \example test_s_split_exact.c
00308     This file shows how to use the function s_split_exact().
00309  */
00310 
00311 /*! \brief Free allocated memory used to put fields extracted from a string of characters.
00312     \param fd Pointer to a structure 'fields'.
00313     \warning You should call this function after a call to s_split(). Please note that if s_split() failes,
00314              then you don't have to free the resulting structure 'fields'. But you can do it, it won't harm.
00315  */
00316 
00317 void free_fields (struct fields *fd)
00318 {
00319   int i;
00320 
00321   for (i=0; i<fd->number_of_fields; i++) { free ((void*)((fd->tabs)[i])); }
00322   free (fd->tabs);
00323   fd->number_of_fields = -1;
00324   fd->size             = -1;
00325   fd->tabs             = NULL;
00326 }
00327 
00328 /*! \brief Extract everything between the string " = " and the end of the string.
00329     \param src Pointer to the string to parse.
00330     \param dst Pointer to a memory location used to put the extracted string.
00331     \param nbcar Maximum number of characters the function can put into 'dst'.
00332     \return The function return a pointer to 'dst', or NULL if an error occured.
00333  */
00334 
00335 char* get_end_of_string (char *src, char *dst, int nbcar)
00336 {
00337   char *pt;
00338   int  size;
00339  
00340   if (nbcar <= 0) { return NULL; }  /* be paranoid */
00341  
00342   pt = strstr (src, " = ");
00343   if (pt == NULL) { dst[0]=0; return dst; }
00344   pt += 3;
00345 
00346   size = strlen(pt);
00347   if (nbcar < size+1) { dst[0]=0; return NULL; }
00348 
00349   strcpy (dst, pt);
00350 
00351   return dst;
00352 }
00353 
00354 /*! \example test_get_end_of_string.c
00355     This file shows how to use the function get_end_of_string().
00356  */
00357 
00358 /*! \brief Transform a IP address "XXX.XXX.XXX.XXX" into a hexadecimal representation "HHHHHHHH".
00359     \param src Pointer to a string of characters that contains an IP address written as "XXX.XXX.XXX.XXX".
00360     \param dst Pointer to a memory location used to put the resulting IP address in hexadecimal notation.
00361     \param nbcar Maximum number of characters the function can put into 'dst'.
00362     \return The function returns a pointer to 'dst', or NULL if an error occured.
00363  */
00364 
00365 char* IP2hex (char *src, char *dst, int nbcar)
00366 {
00367   char          *pt;
00368   struct fields fd;
00369   int           n;
00370   int           ip1, ip2, ip3, ip4;
00371   
00372   if (nbcar < 12) { return NULL; }  /* be paranoid */
00373   memset ((void*)dst, 0, 12);
00374 
00375   pt = (char*)malloc((strlen(src)+1)*sizeof(char));
00376   if (pt == NULL) { return NULL; }
00377   strcpy (pt, src);
00378 
00379   n = s_split(pt, ".", &fd);
00380   if (n != 4)
00381   {
00382     free (pt);
00383     return NULL;
00384   }
00385   free (pt);
00386 
00387   ip1 = atoi (fd.tabs[0]);
00388   ip2 = atoi (fd.tabs[1]);
00389   ip3 = atoi (fd.tabs[2]);
00390   ip4 = atoi (fd.tabs[3]);
00391 
00392   if ((ip1 < 0) || (ip1 > 255)) { free_fields(&fd); return NULL; } 
00393   if ((ip2 < 0) || (ip2 > 255)) { free_fields(&fd); return NULL; } 
00394   if ((ip3 < 0) || (ip3 > 255)) { free_fields(&fd); return NULL; } 
00395   if ((ip4 < 0) || (ip4 > 255)) { free_fields(&fd); return NULL; } 
00396 
00397   sprintf (dst, "%s%s%s%s", bin2hex_hexa[ip1], bin2hex_hexa[ip2], bin2hex_hexa[ip3], bin2hex_hexa[ip4]);
00398  
00399   free_fields(&fd);
00400   return dst;
00401 }
00402 
00403 /*! \example test_IP2hex.c
00404     This file shows how to ude the function IP2hex().
00405  */
00406 
00407 
00408 /*! \brief Delete spaces (or tabulations) at the end of a string. 
00409     \param src pointer to the string to parser.
00410     \return The function returns a pointer to 'src'.
00411     \warning The string 'src' is modified.
00412  */
00413 
00414 char* post_chop(char *src)
00415 {
00416   int  last_idx;
00417   int  i;
00418 
00419   last_idx = strlen(src);
00420   if (last_idx == 0) { return src; }
00421 
00422   last_idx--;
00423   for (i=last_idx; i>=0; i--)
00424   { 
00425      if ((src[i]==' ') || (src[i]==9)) { src[i]=0; }
00426      else { return src; }
00427   }
00428   return src;
00429 }
00430 
00431 /*! \brief Delete spaces (or tabulations) at the begining of a string.
00432     \param src Pointer to a string of of characters that contains the string to parser.
00433     \return value the function returns a pointer to 'src', or NULL if an error occured.
00434     \warning the string 'src' is modified.
00435  */
00436 
00437 char* pre_chop(char *src)
00438 {
00439   int  size;
00440   int  i, passed, count;
00441   char *aux;
00442 
00443   size = strlen(src);
00444   if (size == 0) { return src; }
00445 
00446   aux = (char*)malloc((size+1)*sizeof(char));
00447   if (aux == NULL) { return NULL; }
00448 
00449   passed = 0;
00450   count  = 0;
00451   for (i=0; i<size; i++)
00452   {
00453      if (passed == 0)
00454      {
00455        if ((src[i] != ' ') && (src[i] != 9))
00456        { aux[count++]=src[i]; passed = 1; }
00457      }
00458      else { aux[count++]=src[i]; }
00459   }
00460   aux[count]=0;
00461   strcpy (src, aux);
00462   free (aux);
00463   return src;
00464 }
00465 
00466 /*! \brief Delete spaces (or tabulations) at the begining and at the end of a string.
00467     \param src Pointer to the string to parse.
00468     \return The function returns a pointer to 'src', or NULL if an error occured.
00469     \warning The string 'src' is modified.
00470  */
00471 
00472 
00473 char* chop(char *src)
00474 { return pre_chop(post_chop(src)); }
00475 
00476 /*! \example test_chop.c
00477     This file shows how to use the function chop().
00478  */
00479 
00480 /*! \brief Replace all charater 'new line' (that is '\n'), by the sequence '\n#'.
00481     \param src Pointer to the string to convert into comment.
00482     \param dst Pointer to a pointer that will be used to store the resulting string.
00483     \return The function returns a pointer to the resulting string, or NULL if an error occured.
00484     \warning The returned pointer has been allocated using 'malloc()'. So you must free the allocated memory yourself
00485              (using 'free()').
00486  */ 
00487 
00488 char* add_comment (char *src, char **dst)
00489 {
00490   int   src_size, i, j, nnline, dst_size;
00491   char  c;
00492 
00493 
00494   src_size = strlen(src);
00495 
00496   /* Count the number of new lines */
00497   nnline = 0;
00498   for (i=0; i<src_size; i++) { if (src[i] == '\n') { nnline++; } }
00499 
00500   /* Allocate memory for the destination */
00501   dst_size = src_size + nnline;
00502   *dst = (char*)malloc((dst_size+1)*sizeof(char));
00503   if (*dst == NULL) { return NULL; }
00504  
00505   /* Replace all '\n' by "\n#" */
00506   j = 0;
00507   for (i=0; i<src_size; i++)
00508   {
00509     c = src[i];
00510     (*dst)[j++] = c;
00511     if (c == '\n') { (*dst)[j++] = '#'; }
00512   }
00513 
00514   (*dst)[j] = 0; 
00515 
00516   return *dst;
00517 }
00518 
00519 /*! \example test_add_comment.c
00520     This file shows how to use the function add_comment().
00521  */
00522 
00523 /*! \brief Extract a field's value from a configuration file.
00524     \param buffer Pointer to a NULL terminated string of characters that contains the line extracted from the configuration
00525            file.
00526     \param delim Pointer to a NULL terminated string of characters that defines the delimitor between the field' label and
00527            the field's value.
00528     \return The function returns a pointer to the field's value. If an error occured, the function returns NULL.
00529  */
00530 
00531 
00532 char* get_config_value (char *buffer, char *delim)
00533 {
00534   char *res, *pt;
00535 
00536   pt = strstr (buffer, delim);
00537   if (pt == NULL) { return NULL; }
00538   pt += 1;
00539 
00540   res = (char*)malloc((strlen(pt)+1)*sizeof(char));
00541   if (res == NULL) { return NULL; }
00542   strcpy (res, pt);
00543 
00544   if (chop(res) == NULL) {
00545                            free(res);
00546                            return NULL;
00547                          };
00548   return res;
00549 }
00550 
00551 /*! \brief The maximum number of characters of an interger (in decimal). For example the value "134" takes 3 characters.
00552  */
00553 
00554 #define INTEGER_STR_MAX_SIZE 32
00555 
00556 /*! \brief Replace a tag by an integer in a string.
00557     \param src Pointer to NULL terminated string of characters that contains the tag to replace.
00558     \param tag Tag to replace.
00559     \param value Integer value that will replace the tag.
00560     \return The function returns a pointer to the resulting string of characters. If an error occured, then the function
00561             returns a NULL pointer (this means that the program is running out of memory).
00562  */
00563 
00564 char* replace_tag_by_integer (char* src, char *tag, int value)
00565 {
00566   char *res, *start, *stop, c;
00567   int  tag_size, size;
00568   char buff[INTEGER_STR_MAX_SIZE];
00569 
00570   /* Look for the tag to replace */
00571   start = strstr(src, tag);
00572   if (start == NULL)
00573   {
00574     size = strlen(src)+1;
00575     res = (char*)malloc(size * sizeof(char)); 
00576     if (res == NULL) { return NULL; }
00577     strcpy (res, src);
00578     return res;
00579   }
00580 
00581   tag_size = strlen(tag);
00582   stop = start + tag_size;
00583 
00584   sprintf (buff, "%d", value);
00585   size = (strlen(src) - tag_size) + strlen(buff) + 1;
00586 
00587   res = (char*)malloc(size * sizeof(char));
00588   if (res == NULL) { return NULL; }
00589   c = *start;
00590   *start = 0;
00591   sprintf (res, "%s%s%s", src, buff, stop);
00592   *start = c;
00593 
00594   return res;
00595 }
00596 
00597 /*! \brief The array is used to make the relation between characters and intergers ('0'->0, '1'->1, ...).
00598  */
00599 
00600 extern int char2int[256];
00601 
00602 /*! \brief The array is used to make the relation between characters and hexadecimal ('0'->0, '1'->1, ..., 'a'->10, ...)
00603  */
00604 
00605 extern int char2hex[256];
00606 
00607 
00608 
00609 
00610 /*! \brief Convert a string of characters into an 'unsigned int'.
00611     \param st Pointer to a NULL terminated string of characters that will be converted into "u_int'.
00612     \param error Pointer to an interger used to signal an error.
00613     \return The function returns the value represented bu the string 'st' (if sucess, then 'error' is equal to 0). Please
00614             note that if an error occures, then the returned value is equal to -1 (all in binary - in this case 'error'
00615             is equal to 1).
00616  */
00617 
00618 unsigned int string2unsigned_int (char *st, int *error)
00619 {
00620   int           last_idx, i, digit;
00621   unsigned int  value, factor;
00622 
00623 
00624   value    = 0;
00625   factor   = 1;
00626   *error   = 0;
00627 
00628   last_idx = strlen(st)-1;
00629   for (i=last_idx; i>=0; i--)
00630   {
00631     digit = char2int[(unsigned int)st[i]];
00632     if (digit == -1) { *error = 1; return (unsigned int)-1; }
00633     value += (unsigned int)digit*factor;
00634     factor *= 10;
00635   }
00636 
00637   return value;
00638 }
00639 
00640 
00641 /*! \brief Convert a string of characters into a 'time_t'.
00642     \param st Pointer to a NULL terminated string of characters that will be converted into "time_t".
00643     \param error Pointer to an interger used to signal an error.
00644     \return The function returns the value represented bu the string 'st' (if sucess, then 'error' is equal to 0). Please
00645             note that if an error occures, then the returned value is equal to -1 (all in binary - in this case 'error'
00646             is equal to 1).
00647  */
00648 
00649 time_t string2time_t (char *st, int *error)
00650 {
00651   int     last_idx, i, digit;
00652   time_t  value, factor;
00653 
00654 
00655   value    = 0;
00656   factor   = 1;
00657   *error   = 0;
00658 
00659   last_idx = strlen(st)-1;
00660   for (i=last_idx; i>=0; i--)
00661   {
00662     digit = char2int[(unsigned int)st[i]];
00663     if (digit == -1) { *error = 1; return (unsigned int)-1; }
00664     value += (unsigned int)digit*factor;
00665     factor *= 10;
00666   }
00667 
00668   return value;
00669 }
00670 
00671 /*! \brief Convert a string of characters into an 'int'.
00672     \param st Pointer to a NULL terminated string of characters that will be converted into "int'.
00673     \param error Pointer to an interger used to signal an error.
00674     \return The function returns the value represented bu the string 'st' (if sucess, then 'error' is equal to 0). Please
00675             note that if an error occures, then the returned value is equal to -1 (in this case 'error' is equal to 1).
00676  */
00677 
00678 int string2int (char *st, int *error)
00679 {
00680   int    last_idx, i, digit;
00681   int    value, factor;
00682 
00683 
00684   value    = 0;
00685   factor   = 1;
00686   *error   = 0;
00687 
00688   last_idx = strlen(st)-1;
00689   for (i=last_idx; i>=0; i--)
00690   {
00691     digit = char2int[(unsigned int)st[i]];
00692     if (digit == -1) { *error = 1; return -1; }
00693     value += digit*factor;
00694     factor *= 10;
00695   }
00696 
00697   return value;
00698 }
00699 
00700 /*! \example test_string2int.c
00701     This file shows how to use the function string2int().
00702  */
00703 
00704 
00705 /*! \brief Test if a string of characters represents an interger.
00706     \param st Pointer to a NULL terminated string of characters that should represent an integer.
00707     \return The function returns 0 if the string 'st' represents an interger. Otherwise, it returns 1.
00708  */
00709 
00710 int is_integer (char *st)
00711 {
00712   int i, size, digit;
00713 
00714   if (st == NULL) { return 1; }
00715   if (st[0] == 0) { return 1; } 
00716 
00717   size = strlen (st);
00718 
00719   for (i=0; i<size; i++)
00720   {
00721     digit = char2int[(unsigned int)st[i]];
00722     if (digit == -1) { return 1; }
00723   }
00724 
00725   return 0;
00726 }
00727 
00728 /*! \example test_is_integer.c
00729     This file shows how to use the function is_integer().
00730  */
00731 
00732 /*! \brief Test if a string of characters represents a hexadecimal.
00733     \param st Pointer to a NULL terminated string of characters that should represent a hexadecimal.
00734     \return The function returns 0 if the string 'st' represents an interger. Otherwise, it returns 1.
00735  */
00736 
00737 int is_hexa (char *st)
00738 {
00739   int i, size, digit;
00740 
00741   if (st == NULL) { return 1; }
00742   if (st[0] == 0) { return 1; }
00743 
00744   size = strlen (st);
00745 
00746   for (i=0; i<size; i++)
00747   {
00748     digit = char2hex[(unsigned int)st[i]];
00749     if (digit == -1) { return 1; }
00750   }
00751 
00752   return 0;
00753 }
00754 
00755 /*! \example test_is_hexa.c
00756     This file shows how to use the function is_hexa().
00757  */
00758 
00759 /*! \brief Test if a string of characters represents an IP address.
00760     \param st Pointer to a zero terminated string of characters that might represents an IP address.
00761     \return The function return 0 is 'st' represents an IP address. Otherwise, the function returns 1.
00762     \warning An error code 1 could also mean &quot;Out of memory&quot;. But it should not happen.
00763  */
00764 
00765 int is_ip_address (char *st)
00766 {
00767   struct fields fd;
00768   int           n, i, err;
00769   char          *aux; 
00770 
00771   if (st == NULL) { return 1; }
00772   if (st[0] == 0) { return 1; }
00773 
00774   aux = (char*)malloc((strlen(st)+1)*sizeof(char));
00775   if (aux == NULL) { return 1; }
00776   strcpy (aux, st);
00777 
00778   n = s_split(aux, ".", &fd);
00779   if (n == -1)
00780   { 
00781     free(aux);
00782     return 1;
00783   }
00784 
00785   if (fd.number_of_fields != 4)
00786   {
00787     free (aux);
00788     free_fields(&fd);
00789     return 1;
00790   }  
00791 
00792   for (i=0; i<fd.number_of_fields; i++)
00793   {
00794     n = string2int(fd.tabs[i], &err);
00795     if (err == 1)
00796     {
00797       free (aux);
00798       free_fields(&fd);
00799       return 1;
00800     }
00801     if ((n<0) || (n>255))
00802     {
00803       free (aux);
00804       free_fields(&fd);
00805       return 1;
00806     }
00807   }
00808 
00809   free(aux);
00810   free_fields(&fd);
00811   return 0;
00812 }
00813 
00814 /*! \example test_is_ip_address.c
00815     This file shows how to use the function is_ip_address()
00816  */
00817 
00818 
00819 /*! \brief Convert an IPV4 address to its numerical equivalent.
00820     \param st Pointer to a zero terminated string of characters that represents the IP address (X.X.X.X).
00821     \param num Pointer to a array of - at least - 4 unsigned char.
00822     \return The function returns 0 if the operation was successfull, 1 otherwise.
00823  */
00824 
00825 int ip_address_to_num (char *st, unsigned char *num)
00826 {
00827   struct fields fd;
00828   int           n;
00829 
00830 
00831   if (st == NULL) { return 1; }
00832   if (st[0] == 0) { return 1; }
00833 
00834   if (is_ip_address(st) == 1) { return 1; }
00835 
00836   /********************************************/
00837   /* Split the IP address                     */
00838   /********************************************/
00839 
00840   n = s_split(st, ".", &fd);
00841   if (n == -1) { return 1; }
00842 
00843   if (fd.number_of_fields != 4)
00844   {
00845     free_fields(&fd);
00846     return 1;
00847   }
00848 
00849   /********************************************/
00850   /* First byte X.x.x.x                       */
00851   /********************************************/
00852 
00853   num[0] = (unsigned char) (atoi(fd.tabs[0]));
00854 
00855   /********************************************/
00856   /* Second byte x.X.x.x                      */
00857   /********************************************/
00858 
00859   num[1] = (unsigned char) (atoi(fd.tabs[1]));
00860 
00861   /********************************************/
00862   /* Third byte x.x.X.x                       */
00863   /********************************************/
00864 
00865   num[2] = (unsigned char) (atoi(fd.tabs[2]));
00866 
00867   /********************************************/
00868   /* Fourth byte x.x.x.X                      */
00869   /********************************************/
00870 
00871   num[3] = (unsigned char) (atoi(fd.tabs[3]));
00872 
00873   free_fields(&fd);
00874   return 0;
00875 }
00876 
00877 /*! \example test_ip_address_to_num.c
00878     This file shows how to use the function ip_address_to_num().
00879  */

Generated on Thu Apr 3 16:23:44 2003 for Common_C_libraries by doxygen1.3-rc1