LCOV - code coverage report
Current view: top level - lib/roken - parse_units.c (source / functions) Hit Total Coverage
Test: samba_4_0_test.lcov.info Lines: 0 133 0.0 %
Date: 2014-04-02 Functions: 0 16 0.0 %
Branches: 0 100 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
       3                 :            :  * (Royal Institute of Technology, Stockholm, Sweden).
       4                 :            :  * All rights reserved.
       5                 :            :  *
       6                 :            :  * Redistribution and use in source and binary forms, with or without
       7                 :            :  * modification, are permitted provided that the following conditions
       8                 :            :  * are met:
       9                 :            :  *
      10                 :            :  * 1. Redistributions of source code must retain the above copyright
      11                 :            :  *    notice, this list of conditions and the following disclaimer.
      12                 :            :  *
      13                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      14                 :            :  *    notice, this list of conditions and the following disclaimer in the
      15                 :            :  *    documentation and/or other materials provided with the distribution.
      16                 :            :  *
      17                 :            :  * 3. Neither the name of the Institute nor the names of its contributors
      18                 :            :  *    may be used to endorse or promote products derived from this software
      19                 :            :  *    without specific prior written permission.
      20                 :            :  *
      21                 :            :  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
      22                 :            :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      23                 :            :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      24                 :            :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
      25                 :            :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      26                 :            :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      27                 :            :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      28                 :            :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      29                 :            :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      30                 :            :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      31                 :            :  * SUCH DAMAGE.
      32                 :            :  */
      33                 :            : 
      34                 :            : #include <config.h>
      35                 :            : 
      36                 :            : #include <stdio.h>
      37                 :            : #include <ctype.h>
      38                 :            : #include <string.h>
      39                 :            : #include "roken.h"
      40                 :            : #include "parse_units.h"
      41                 :            : 
      42                 :            : /*
      43                 :            :  * Parse string in `s' according to `units' and return value.
      44                 :            :  * def_unit defines the default unit.
      45                 :            :  */
      46                 :            : 
      47                 :            : static int
      48                 :          0 : parse_something (const char *s, const struct units *units,
      49                 :            :                  const char *def_unit,
      50                 :            :                  int (*func)(int res, int val, unsigned mult),
      51                 :            :                  int init,
      52                 :            :                  int accept_no_val_p)
      53                 :            : {
      54                 :            :     const char *p;
      55                 :          0 :     int res = init;
      56                 :          0 :     unsigned def_mult = 1;
      57                 :            : 
      58         [ #  # ]:          0 :     if (def_unit != NULL) {
      59                 :            :         const struct units *u;
      60                 :            : 
      61         [ #  # ]:          0 :         for (u = units; u->name; ++u) {
      62         [ #  # ]:          0 :             if (strcasecmp (u->name, def_unit) == 0) {
      63                 :          0 :                 def_mult = u->mult;
      64                 :          0 :                 break;
      65                 :            :             }
      66                 :            :         }
      67         [ #  # ]:          0 :         if (u->name == NULL)
      68                 :          0 :             return -1;
      69                 :            :     }
      70                 :            : 
      71                 :          0 :     p = s;
      72         [ #  # ]:          0 :     while (*p) {
      73                 :            :         int val;
      74                 :            :         char *next;
      75                 :            :         const struct units *u, *partial_unit;
      76                 :            :         size_t u_len;
      77                 :            :         unsigned partial;
      78                 :          0 :         int no_val_p = 0;
      79                 :            : 
      80 [ #  # ][ #  # ]:          0 :         while(isspace((unsigned char)*p) || *p == ',')
      81                 :          0 :             ++p;
      82                 :            : 
      83                 :          0 :         val = strtol(p, &next, 0);
      84         [ #  # ]:          0 :         if (p == next) {
      85                 :          0 :             val = 0;
      86         [ #  # ]:          0 :             if(!accept_no_val_p)
      87                 :          0 :                 return -1;
      88                 :          0 :             no_val_p = 1;
      89                 :            :         }
      90                 :          0 :         p = next;
      91         [ #  # ]:          0 :         while (isspace((unsigned char)*p))
      92                 :          0 :             ++p;
      93         [ #  # ]:          0 :         if (*p == '\0') {
      94                 :          0 :             res = (*func)(res, val, def_mult);
      95         [ #  # ]:          0 :             if (res < 0)
      96                 :          0 :                 return res;
      97                 :          0 :             break;
      98         [ #  # ]:          0 :         } else if (*p == '+') {
      99                 :          0 :             ++p;
     100                 :          0 :             val = 1;
     101         [ #  # ]:          0 :         } else if (*p == '-') {
     102                 :          0 :             ++p;
     103                 :          0 :             val = -1;
     104                 :            :         }
     105 [ #  # ][ #  # ]:          0 :         if (no_val_p && val == 0)
     106                 :          0 :             val = 1;
     107                 :          0 :         u_len = strcspn (p, ", \t");
     108                 :          0 :         partial = 0;
     109                 :          0 :         partial_unit = NULL;
     110 [ #  # ][ #  # ]:          0 :         if (u_len > 1 && p[u_len - 1] == 's')
     111                 :          0 :             --u_len;
     112         [ #  # ]:          0 :         for (u = units; u->name; ++u) {
     113         [ #  # ]:          0 :             if (strncasecmp (p, u->name, u_len) == 0) {
     114         [ #  # ]:          0 :                 if (u_len == strlen (u->name)) {
     115                 :          0 :                     p += u_len;
     116                 :          0 :                     res = (*func)(res, val, u->mult);
     117         [ #  # ]:          0 :                     if (res < 0)
     118                 :          0 :                         return res;
     119                 :          0 :                     break;
     120                 :            :                 } else {
     121                 :          0 :                     ++partial;
     122                 :          0 :                     partial_unit = u;
     123                 :            :                 }
     124                 :            :             }
     125                 :            :         }
     126         [ #  # ]:          0 :         if (u->name == NULL) {
     127         [ #  # ]:          0 :             if (partial == 1) {
     128                 :          0 :                 p += u_len;
     129                 :          0 :                 res = (*func)(res, val, partial_unit->mult);
     130         [ #  # ]:          0 :                 if (res < 0)
     131                 :          0 :                     return res;
     132                 :            :             } else {
     133                 :          0 :                 return -1;
     134                 :            :             }
     135                 :            :         }
     136         [ #  # ]:          0 :         if (*p == 's')
     137                 :          0 :             ++p;
     138                 :            :     }
     139                 :          0 :     return res;
     140                 :            : }
     141                 :            : 
     142                 :            : /*
     143                 :            :  * The string consists of a sequence of `n unit'
     144                 :            :  */
     145                 :            : 
     146                 :            : static int
     147                 :          0 : acc_units(int res, int val, unsigned mult)
     148                 :            : {
     149                 :          0 :     return res + val * mult;
     150                 :            : }
     151                 :            : 
     152                 :            : ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
     153                 :          0 : parse_units (const char *s, const struct units *units,
     154                 :            :              const char *def_unit)
     155                 :            : {
     156                 :          0 :     return parse_something (s, units, def_unit, acc_units, 0, 0);
     157                 :            : }
     158                 :            : 
     159                 :            : /*
     160                 :            :  * The string consists of a sequence of `[+-]flag'.  `orig' consists
     161                 :            :  * the original set of flags, those are then modified and returned as
     162                 :            :  * the function value.
     163                 :            :  */
     164                 :            : 
     165                 :            : static int
     166                 :          0 : acc_flags(int res, int val, unsigned mult)
     167                 :            : {
     168         [ #  # ]:          0 :     if(val == 1)
     169                 :          0 :         return res | mult;
     170         [ #  # ]:          0 :     else if(val == -1)
     171                 :          0 :         return res & ~mult;
     172         [ #  # ]:          0 :     else if (val == 0)
     173                 :          0 :         return mult;
     174                 :            :     else
     175                 :          0 :         return -1;
     176                 :            : }
     177                 :            : 
     178                 :            : ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
     179                 :          0 : parse_flags (const char *s, const struct units *units,
     180                 :            :              int orig)
     181                 :            : {
     182                 :          0 :     return parse_something (s, units, NULL, acc_flags, orig, 1);
     183                 :            : }
     184                 :            : 
     185                 :            : /*
     186                 :            :  * Return a string representation according to `units' of `num' in `s'
     187                 :            :  * with maximum length `len'.  The actual length is the function value.
     188                 :            :  */
     189                 :            : 
     190                 :            : static int
     191                 :          0 : unparse_something (int num, const struct units *units, char *s, size_t len,
     192                 :            :                    int (*print) (char *, size_t, int, const char *, int),
     193                 :            :                    int (*update) (int, unsigned),
     194                 :            :                    const char *zero_string)
     195                 :            : {
     196                 :            :     const struct units *u;
     197                 :          0 :     int ret = 0, tmp;
     198                 :            : 
     199         [ #  # ]:          0 :     if (num == 0)
     200                 :          0 :         return snprintf (s, len, "%s", zero_string);
     201                 :            : 
     202 [ #  # ][ #  # ]:          0 :     for (u = units; num > 0 && u->name; ++u) {
     203                 :            :         int divisor;
     204                 :            : 
     205                 :          0 :         divisor = num / u->mult;
     206         [ #  # ]:          0 :         if (divisor) {
     207                 :          0 :             num = (*update) (num, u->mult);
     208                 :          0 :             tmp = (*print) (s, len, divisor, u->name, num);
     209         [ #  # ]:          0 :             if (tmp < 0)
     210                 :          0 :                 return tmp;
     211         [ #  # ]:          0 :             if (tmp > (int) len) {
     212                 :          0 :                 len = 0;
     213                 :          0 :                 s = NULL;
     214                 :            :             } else {
     215                 :          0 :                 len -= tmp;
     216                 :          0 :                 s += tmp;
     217                 :            :             }
     218                 :          0 :             ret += tmp;
     219                 :            :         }
     220                 :            :     }
     221                 :          0 :     return ret;
     222                 :            : }
     223                 :            : 
     224                 :            : static int
     225                 :          0 : print_unit (char *s, size_t len, int divisor, const char *name, int rem)
     226                 :            : {
     227 [ #  # ][ #  # ]:          0 :     return snprintf (s, len, "%u %s%s%s",
     228                 :            :                      divisor, name,
     229                 :            :                      divisor == 1 ? "" : "s",
     230                 :            :                      rem > 0 ? " " : "");
     231                 :            : }
     232                 :            : 
     233                 :            : static int
     234                 :          0 : update_unit (int in, unsigned mult)
     235                 :            : {
     236                 :          0 :     return in % mult;
     237                 :            : }
     238                 :            : 
     239                 :            : static int
     240                 :          0 : update_unit_approx (int in, unsigned mult)
     241                 :            : {
     242         [ #  # ]:          0 :     if (in / mult > 0)
     243                 :          0 :         return 0;
     244                 :            :     else
     245                 :          0 :         return update_unit (in, mult);
     246                 :            : }
     247                 :            : 
     248                 :            : ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
     249                 :          0 : unparse_units (int num, const struct units *units, char *s, size_t len)
     250                 :            : {
     251                 :          0 :     return unparse_something (num, units, s, len,
     252                 :            :                               print_unit,
     253                 :            :                               update_unit,
     254                 :            :                               "0");
     255                 :            : }
     256                 :            : 
     257                 :            : ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
     258                 :          0 : unparse_units_approx (int num, const struct units *units, char *s, size_t len)
     259                 :            : {
     260                 :          0 :     return unparse_something (num, units, s, len,
     261                 :            :                               print_unit,
     262                 :            :                               update_unit_approx,
     263                 :            :                               "0");
     264                 :            : }
     265                 :            : 
     266                 :            : ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
     267                 :          0 : print_units_table (const struct units *units, FILE *f)
     268                 :            : {
     269                 :            :     const struct units *u, *u2;
     270                 :          0 :     size_t max_sz = 0;
     271                 :            : 
     272         [ #  # ]:          0 :     for (u = units; u->name; ++u) {
     273         [ #  # ]:          0 :         max_sz = max(max_sz, strlen(u->name));
     274                 :            :     }
     275                 :            : 
     276         [ #  # ]:          0 :     for (u = units; u->name;) {
     277                 :            :         char buf[1024];
     278                 :            :         const struct units *next;
     279                 :            : 
     280 [ #  # ][ #  # ]:          0 :         for (next = u + 1; next->name && next->mult == u->mult; ++next)
     281                 :            :             ;
     282                 :            : 
     283         [ #  # ]:          0 :         if (next->name) {
     284 [ #  # ][ #  # ]:          0 :             for (u2 = next;
     285                 :          0 :                  u2->name && u->mult % u2->mult != 0;
     286                 :          0 :                  ++u2)
     287                 :            :                 ;
     288         [ #  # ]:          0 :             if (u2->name == NULL)
     289                 :          0 :                 --u2;
     290                 :          0 :             unparse_units (u->mult, u2, buf, sizeof(buf));
     291                 :          0 :             fprintf (f, "1 %*s = %s\n", (int)max_sz, u->name, buf);
     292                 :            :         } else {
     293                 :          0 :             fprintf (f, "1 %s\n", u->name);
     294                 :            :         }
     295                 :          0 :         u = next;
     296                 :            :     }
     297                 :          0 : }
     298                 :            : 
     299                 :            : static int
     300                 :          0 : print_flag (char *s, size_t len, int divisor, const char *name, int rem)
     301                 :            : {
     302         [ #  # ]:          0 :     return snprintf (s, len, "%s%s", name, rem > 0 ? ", " : "");
     303                 :            : }
     304                 :            : 
     305                 :            : static int
     306                 :          0 : update_flag (int in, unsigned mult)
     307                 :            : {
     308                 :          0 :     return in - mult;
     309                 :            : }
     310                 :            : 
     311                 :            : ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
     312                 :          0 : unparse_flags (int num, const struct units *units, char *s, size_t len)
     313                 :            : {
     314                 :          0 :     return unparse_something (num, units, s, len,
     315                 :            :                               print_flag,
     316                 :            :                               update_flag,
     317                 :            :                               "");
     318                 :            : }
     319                 :            : 
     320                 :            : ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
     321                 :          0 : print_flags_table (const struct units *units, FILE *f)
     322                 :            : {
     323                 :            :     const struct units *u;
     324                 :            : 
     325         [ #  # ]:          0 :     for(u = units; u->name; ++u)
     326         [ #  # ]:          0 :         fprintf(f, "%s%s", u->name, (u+1)->name ? ", " : "\n");
     327                 :          0 : }

Generated by: LCOV version 1.9