Changes in kernel/generic/src/lib/func.c [4ce914d4:67a88c3] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/lib/func.c
r4ce914d4 r67a88c3 27 27 */ 28 28 29 /** @addtogroup generic 29 /** @addtogroup generic 30 30 * @{ 31 31 */ … … 33 33 /** 34 34 * @file 35 * @brief 35 * @brief Miscellaneous functions. 36 36 */ 37 37 … … 79 79 } 80 80 81 /** Convert ascii representation to unative_t 82 * 83 * Supports 0x for hexa & 0 for octal notation. 84 * Does not check for overflows, does not support negative numbers 85 * 86 * @param text Textual representation of number 87 * @return Converted number or 0 if no valid number ofund 88 */ 89 unative_t atoi(const char *text) 90 { 91 int base = 10; 92 unative_t result = 0; 93 94 if (text[0] == '0' && text[1] == 'x') { 95 base = 16; 96 text += 2; 97 } else if (text[0] == '0') 98 base = 8; 99 100 while (*text) { 101 if (base != 16 && \ 102 ((*text >= 'A' && *text <= 'F' ) 103 || (*text >='a' && *text <='f'))) 104 break; 105 if (base == 8 && *text >='8') 106 break; 107 108 if (*text >= '0' && *text <= '9') { 109 result *= base; 110 result += *text - '0'; 111 } else if (*text >= 'A' && *text <= 'F') { 112 result *= base; 113 result += *text - 'A' + 10; 114 } else if (*text >= 'a' && *text <= 'f') { 115 result *= base; 116 result += *text - 'a' + 10; 117 } else 118 break; 119 text++; 120 } 121 122 return result; 123 } 124 125 126 void order(const uint64_t val, uint64_t *rv, char *suffix) 127 { 128 if (val > 10000000000000000000ULL) { 129 *rv = val / 1000000000000000000ULL; 130 *suffix = 'Z'; 131 } else if (val > 1000000000000000000ULL) { 132 *rv = val / 1000000000000000ULL; 133 *suffix = 'E'; 134 } else if (val > 1000000000000000ULL) { 135 *rv = val / 1000000000000ULL; 136 *suffix = 'T'; 137 } else if (val > 1000000000000ULL) { 138 *rv = val / 1000000000ULL; 139 *suffix = 'G'; 140 } else if (val > 1000000000ULL) { 141 *rv = val / 1000000ULL; 142 *suffix = 'M'; 143 } else if (val > 1000000ULL) { 144 *rv = val / 1000ULL; 145 *suffix = 'k'; 146 } else { 147 *rv = val; 148 *suffix = ' '; 149 } 150 } 151 81 152 /** @} 82 153 */
Note:
See TracChangeset
for help on using the changeset viewer.