back to topotato report
topotato coverage report
Current view: top level - lib - tc.c (source / functions) Hit Total Coverage
Test: test_pim_bfd.py::PIMBFDTest Lines: 0 30 0.0 %
Date: 2023-02-24 18:39:40 Functions: 0 1 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Traffic Control (TC) main library
       3             :  * Copyright (C) 2022  Shichu Yang
       4             :  *
       5             :  * This program is free software; you can redistribute it and/or modify it
       6             :  * under the terms of the GNU General Public License as published by the Free
       7             :  * Software Foundation; either version 2 of the License, or (at your option)
       8             :  * any later version.
       9             :  *
      10             :  * This program is distributed in the hope that it will be useful, but WITHOUT
      11             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12             :  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
      13             :  * more details.
      14             :  *
      15             :  * You should have received a copy of the GNU General Public License along
      16             :  * with this program; see the file COPYING; if not, write to the Free Software
      17             :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
      18             :  */
      19             : 
      20             : #include "tc.h"
      21             : 
      22           0 : int tc_getrate(const char *str, uint64_t *rate)
      23             : {
      24           0 :         char *endp;
      25           0 :         uint64_t raw = strtoull(str, &endp, 10);
      26             : 
      27           0 :         if (endp == str)
      28             :                 return -1;
      29             : 
      30             :         /* if the string only contains a number, it must be valid rate (bps) */
      31           0 :         bool valid = (*endp == '\0');
      32             : 
      33           0 :         const char *p = endp;
      34           0 :         bool bytes = false, binary_base = false;
      35           0 :         int power = 0;
      36             : 
      37           0 :         while (*p) {
      38           0 :                 if (strcmp(p, "Bps") == 0) {
      39             :                         bytes = true;
      40             :                         valid = true;
      41             :                         break;
      42           0 :                 } else if (strcmp(p, "bit") == 0) {
      43             :                         valid = true;
      44             :                         break;
      45             :                 }
      46           0 :                 switch (*p) {
      47             :                 case 'k':
      48             :                 case 'K':
      49             :                         power = 1;
      50             :                         break;
      51           0 :                 case 'm':
      52             :                 case 'M':
      53           0 :                         power = 2;
      54           0 :                         break;
      55           0 :                 case 'g':
      56             :                 case 'G':
      57           0 :                         power = 3;
      58           0 :                         break;
      59           0 :                 case 't':
      60             :                 case 'T':
      61           0 :                         power = 4;
      62           0 :                         break;
      63           0 :                 case 'i':
      64             :                 case 'I':
      65           0 :                         if (power != 0)
      66             :                                 binary_base = true;
      67             :                         else
      68             :                                 return -1;
      69             :                         break;
      70             :                 default:
      71             :                         return -1;
      72             :                 }
      73           0 :                 p++;
      74             :         }
      75             : 
      76           0 :         if (!valid)
      77             :                 return -1;
      78             : 
      79           0 :         for (int i = 0; i < power; i++)
      80           0 :                 raw *= binary_base ? 1024ULL : 1000ULL;
      81             : 
      82           0 :         if (bytes)
      83           0 :                 *rate = raw;
      84             :         else
      85           0 :                 *rate = raw / 8ULL;
      86             : 
      87             :         return 0;
      88             : }

Generated by: LCOV version v1.16-topotato