Changes in uspace/lib/c/generic/str.c [d39c46e0:38d150e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/str.c
rd39c46e0 r38d150e 196 196 * code was invalid. 197 197 */ 198 errno_t chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)198 int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size) 199 199 { 200 200 if (*offset >= size) … … 839 839 * non-ASCII bytes. 840 840 */ 841 errno_t spascii_to_str(char *dest, size_t size, const uint8_t *src, size_t n)841 int spascii_to_str(char *dest, size_t size, const uint8_t *src, size_t n) 842 842 { 843 843 size_t sidx; … … 845 845 size_t dlast; 846 846 uint8_t byte; 847 errno_t rc;848 errno_t result;847 int rc; 848 int result; 849 849 850 850 /* There must be space for a null terminator in the buffer. */ … … 918 918 * @param src Source utf16 string. 919 919 * 920 * @return EOK, if success, an error code otherwise.921 */ 922 errno_t utf16_to_str(char *dest, size_t size, const uint16_t *src)920 * @return EOK, if success, negative otherwise. 921 */ 922 int utf16_to_str(char *dest, size_t size, const uint16_t *src) 923 923 { 924 924 size_t idx = 0, dest_off = 0; 925 925 wchar_t ch; 926 errno_t rc = EOK;926 int rc = EOK; 927 927 928 928 /* There must be space for a null terminator in the buffer. */ … … 961 961 * @param src Source string. 962 962 * 963 * @return EOK, if success, an error code otherwise.964 */ 965 errno_t str_to_utf16(uint16_t *dest, size_t dlen, const char *src)966 { 967 errno_t rc = EOK;963 * @return EOK, if success, negative otherwise. 964 */ 965 int str_to_utf16(uint16_t *dest, size_t dlen, const char *src) 966 { 967 int rc = EOK; 968 968 size_t offset = 0; 969 969 size_t idx = 0; … … 1273 1273 } 1274 1274 1275 /** Convert string to a number. 1276 * Core of strtol and strtoul functions. 1277 * 1278 * @param nptr Pointer to string. 1279 * @param endptr If not NULL, function stores here pointer to the first 1280 * invalid character. 1281 * @param base Zero or number between 2 and 36 inclusive. 1282 * @param sgn It's set to 1 if minus found. 1283 * @return Result of conversion. 1284 */ 1285 static unsigned long 1286 _strtoul(const char *nptr, char **endptr, int base, char *sgn) 1287 { 1288 unsigned char c; 1289 unsigned long result = 0; 1290 unsigned long a, b; 1291 const char *str = nptr; 1292 const char *tmpptr; 1293 1294 while (isspace(*str)) 1295 str++; 1296 1297 if (*str == '-') { 1298 *sgn = 1; 1299 ++str; 1300 } else if (*str == '+') 1301 ++str; 1302 1303 if (base) { 1304 if ((base == 1) || (base > 36)) { 1305 /* FIXME: set errno to EINVAL */ 1306 return 0; 1307 } 1308 if ((base == 16) && (*str == '0') && ((str[1] == 'x') || 1309 (str[1] == 'X'))) { 1310 str += 2; 1311 } 1312 } else { 1313 base = 10; 1314 1315 if (*str == '0') { 1316 base = 8; 1317 if ((str[1] == 'X') || (str[1] == 'x')) { 1318 base = 16; 1319 str += 2; 1320 } 1321 } 1322 } 1323 1324 tmpptr = str; 1325 1326 while (*str) { 1327 c = *str; 1328 c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 : 1329 (c <= '9' ? c - '0' : 0xff))); 1330 if (c >= base) { 1331 break; 1332 } 1333 1334 a = (result & 0xff) * base + c; 1335 b = (result >> 8) * base + (a >> 8); 1336 1337 if (b > (ULONG_MAX >> 8)) { 1338 /* overflow */ 1339 /* FIXME: errno = ERANGE*/ 1340 return ULONG_MAX; 1341 } 1342 1343 result = (b << 8) + (a & 0xff); 1344 ++str; 1345 } 1346 1347 if (str == tmpptr) { 1348 /* 1349 * No number was found => first invalid character is the first 1350 * character of the string. 1351 */ 1352 /* FIXME: set errno to EINVAL */ 1353 str = nptr; 1354 result = 0; 1355 } 1356 1357 if (endptr) 1358 *endptr = (char *) str; 1359 1360 if (nptr == str) { 1361 /*FIXME: errno = EINVAL*/ 1362 return 0; 1363 } 1364 1365 return result; 1366 } 1367 1368 /** Convert initial part of string to long int according to given base. 1369 * The number may begin with an arbitrary number of whitespaces followed by 1370 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be 1371 * inserted and the number will be taken as hexadecimal one. If the base is 0 1372 * and the number begin with a zero, number will be taken as octal one (as with 1373 * base 8). Otherwise the base 0 is taken as decimal. 1374 * 1375 * @param nptr Pointer to string. 1376 * @param endptr If not NULL, function stores here pointer to the first 1377 * invalid character. 1378 * @param base Zero or number between 2 and 36 inclusive. 1379 * @return Result of conversion. 1380 */ 1381 long int strtol(const char *nptr, char **endptr, int base) 1382 { 1383 char sgn = 0; 1384 unsigned long number = 0; 1385 1386 number = _strtoul(nptr, endptr, base, &sgn); 1387 1388 if (number > LONG_MAX) { 1389 if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) { 1390 /* FIXME: set 0 to errno */ 1391 return number; 1392 } 1393 /* FIXME: set ERANGE to errno */ 1394 return (sgn ? LONG_MIN : LONG_MAX); 1395 } 1396 1397 return (sgn ? -number : number); 1398 } 1275 1399 1276 1400 /** Duplicate string. … … 1333 1457 str_ncpy(dest, size + 1, src, size); 1334 1458 return dest; 1459 } 1460 1461 /** Convert initial part of string to unsigned long according to given base. 1462 * The number may begin with an arbitrary number of whitespaces followed by 1463 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be 1464 * inserted and the number will be taken as hexadecimal one. If the base is 0 1465 * and the number begin with a zero, number will be taken as octal one (as with 1466 * base 8). Otherwise the base 0 is taken as decimal. 1467 * 1468 * @param nptr Pointer to string. 1469 * @param endptr If not NULL, function stores here pointer to the first 1470 * invalid character 1471 * @param base Zero or number between 2 and 36 inclusive. 1472 * @return Result of conversion. 1473 */ 1474 unsigned long strtoul(const char *nptr, char **endptr, int base) 1475 { 1476 char sgn = 0; 1477 unsigned long number = 0; 1478 1479 number = _strtoul(nptr, endptr, base, &sgn); 1480 1481 return (sgn ? -number : number); 1335 1482 } 1336 1483 … … 1392 1539 * 1393 1540 */ 1394 static errno_t str_uint(const char *nptr, char **endptr, unsigned int base,1541 static int str_uint(const char *nptr, char **endptr, unsigned int base, 1395 1542 bool *neg, uint64_t *result) 1396 1543 { … … 1513 1660 * 1514 1661 */ 1515 errno_t str_uint8_t(const char *nptr, const char **endptr, unsigned int base,1662 int str_uint8_t(const char *nptr, const char **endptr, unsigned int base, 1516 1663 bool strict, uint8_t *result) 1517 1664 { … … 1521 1668 char *lendptr; 1522 1669 uint64_t res; 1523 errno_t ret = str_uint(nptr, &lendptr, base, &neg, &res);1670 int ret = str_uint(nptr, &lendptr, base, &neg, &res); 1524 1671 1525 1672 if (endptr != NULL) … … 1560 1707 * 1561 1708 */ 1562 errno_t str_uint16_t(const char *nptr, const char **endptr, unsigned int base,1709 int str_uint16_t(const char *nptr, const char **endptr, unsigned int base, 1563 1710 bool strict, uint16_t *result) 1564 1711 { … … 1568 1715 char *lendptr; 1569 1716 uint64_t res; 1570 errno_t ret = str_uint(nptr, &lendptr, base, &neg, &res);1717 int ret = str_uint(nptr, &lendptr, base, &neg, &res); 1571 1718 1572 1719 if (endptr != NULL) … … 1607 1754 * 1608 1755 */ 1609 errno_t str_uint32_t(const char *nptr, const char **endptr, unsigned int base,1756 int str_uint32_t(const char *nptr, const char **endptr, unsigned int base, 1610 1757 bool strict, uint32_t *result) 1611 1758 { … … 1615 1762 char *lendptr; 1616 1763 uint64_t res; 1617 errno_t ret = str_uint(nptr, &lendptr, base, &neg, &res);1764 int ret = str_uint(nptr, &lendptr, base, &neg, &res); 1618 1765 1619 1766 if (endptr != NULL) … … 1654 1801 * 1655 1802 */ 1656 errno_t str_uint64_t(const char *nptr, const char **endptr, unsigned int base,1803 int str_uint64_t(const char *nptr, const char **endptr, unsigned int base, 1657 1804 bool strict, uint64_t *result) 1658 1805 { … … 1661 1808 bool neg; 1662 1809 char *lendptr; 1663 errno_t ret = str_uint(nptr, &lendptr, base, &neg, result);1810 int ret = str_uint(nptr, &lendptr, base, &neg, result); 1664 1811 1665 1812 if (endptr != NULL) … … 1693 1840 * 1694 1841 */ 1695 errno_t str_size_t(const char *nptr, const char **endptr, unsigned int base,1842 int str_size_t(const char *nptr, const char **endptr, unsigned int base, 1696 1843 bool strict, size_t *result) 1697 1844 { … … 1701 1848 char *lendptr; 1702 1849 uint64_t res; 1703 errno_t ret = str_uint(nptr, &lendptr, base, &neg, &res);1850 int ret = str_uint(nptr, &lendptr, base, &neg, &res); 1704 1851 1705 1852 if (endptr != NULL)
Note:
See TracChangeset
for help on using the changeset viewer.