goinfre/libs/libgetopt/getopt.c

goinfre/libs/libgetopt/getopt.c

  _____________________________________________________________________________ 
 /   / Index / Goinfre / Resume / Links / Contact / Sitemap /    .: v0.6.1.0 |^|
|\__/-------+         +--------+-------+---------+---------+-----------------+#|
|  1| /goinfre/libs/libgetopt/getopt.c                                       |#|
|  2| ==========================================                             |#|
|  3|                                                                        |#|
|  4|                                                   [ raw ] [ download ] |#|
|  5|                                             ` `` ````````````````````` |#|
|  6|                                                                        |#|
|  7| /**                                                                    |#|
|  8|  * libgetopt/getopt.c                                                  |#|
|  9|  *                                                                     |#|
| 10|  * Copyright (c) Manfred Touron 2008                                   |#|
| 11|  */                                                                    |#|
| 12|                                                                        |#|
| 13| #include <stdlib.h>                                                    |#|
| 14| #include <unistd.h>                                                    |#|
| 15| #include <stdio.h>                                                     |#|
| 16| #include <string.h>                                                    |#|
| 17|                                                                        |#|
| 18| #include "core.h"                                                      |#|
| 19|                                                                        |#|
| 20| int      _getopt_ac = 0;                                               |#|
| 21| char **      _getopt_av = NULL;                                        |#|
| 22|                                                                        |#|
| 23| # define ARG_ISOPT(X)      (X < _getopt_ac && _getopt_av[X][0] == '-'  |#|
| 24| &&      \                                                              |#|
| 25|                    _getopt_av[X][1] && !_getopt_av[X][2])              |#|
| 26|                                                                        |#|
| 27| void      usage(const char *str)                                       |#|
| 28| {                                                                      |#|
| 29|   fprintf(stdout, "usage: %s %s\n", _getopt_av[0], str);               |#|
| 30| }                                                                      |#|
| 31|                                                                        |#|
| 32| int      getopt_init(int ac, char **av)                                |#|
| 33| {                                                                      |#|
| 34|   int      i;                                                          |#|
| 35|                                                                        |#|
| 36|   _getopt_ac = ac;                                                     |#|
| 37|   _getopt_av = av;                                                     |#|
| 38|   for (i = 1; i < ac && av[i]; i++)                                    |#|
| 39|     ;                                                                  |#|
| 40|   return (i == ac && !av[i]);                                          |#|
| 41| }                                                                      |#|
| 42|                                                                        |#|
| 43| int      getopt_exists(const char c)                                   |#|
| 44| {                                                                      |#|
| 45|   int      i;                                                          |#|
| 46|                                                                        |#|
| 47|   for (i = 1; i < _getopt_ac; i++)                                     |#|
| 48|     if (ARG_ISOPT(i) && _getopt_av[i][1] == c)                         |#|
| 49|       return (i);                                                      |#|
| 50|   return (0);                                                          |#|
| 51| }                                                                      |#|
| 52|                                                                        |#|
| 53| int      getopt_exists_all(const char *fmt)                            |#|
| 54| {                                                                      |#|
| 55|   int      i;                                                          |#|
| 56|   int      p;                                                          |#|
| 57|                                                                        |#|
| 58|   for (i = 0; fmt[i]; i++)                                             |#|
| 59|     if ((p = getopt_exists(fmt[i])))                                   |#|
| 60|       {                                                                |#|
| 61|       if (fmt[i + 1] == ':')                                           |#|
| 62|         {                                                              |#|
| 63|           if (p + 1 == _getopt_ac)                                     |#|
| 64|             return (0);                                                |#|
| 65|           i++;                                                         | |
| 66|         }                                                              | |
| 67|       }                                                                | |
| 68|     else                                                               | |
| 69|       return (0);                                                      | |
| 70|   return (1);                                                          | |
| 71| }                                                                      | |
| 72|                                                                        | |
| 73| int      getopt_check(const char *fmt)                                 | |
| 74| {                                                                      | |
| 75|   int      i;                                                          | |
| 76|   int      tmp;                                                        | |
| 77|                                                                        | |
| 78|   for (i = 1; fmt[i]; i++)                                             | |
| 79|     {                                                                  | |
| 80|       if ((tmp = getopt_exists(fmt[i])) == 0)                          | |
| 81|       return (0);                                                      | |
| 82|       if (fmt[i + 1] == ':')                                           | |
| 83|       if (i++ + 1 >= _getopt_ac)                                       | |
| 84|         return (0);                                                    | |
| 85|     }                                                                  | |
| 86|   return (1);                                                          | |
| 87| }                                                                      | |
| 88|                                                                        | |
| 89| char      *getopt_get(const char c, const char *def)                   | |
| 90| {                                                                      | |
| 91|   if (!getopt_exists(c))                                               | |
| 92|     return ((char *)def);                                              | |
| 93|   return (_getopt_av[getopt_exists(c) + 1]);                           | |
| 94| }                                                                      | |
| 95|                                                                        | |
+---+                                                                        | |
\_   \______ mo5.so - normal - 1337.so - rev.so - video_r.so +---------------+ |
  |__       \_____   plain.so - color.so - comment.so       /  moul 2008 (c) |V|
     \-----\______\________________________________________/-----------------+-+