Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/str.c

    rdcb74c0a rd47279b  
    540540
    541541        dstr_size = str_size(dest);
    542         if (dstr_size >= size)
    543                 return;
    544        
    545542        str_cpy(dest + dstr_size, size - dstr_size, src);
    546 }
    547 
    548 /** Convert space-padded ASCII to string.
    549  *
    550  * Common legacy text encoding in hardware is 7-bit ASCII fitted into
    551  * a fixed-with byte buffer (bit 7 always zero), right-padded with spaces
    552  * (ASCII 0x20). Convert space-padded ascii to string representation.
    553  *
    554  * If the text does not fit into the destination buffer, the function converts
    555  * as many characters as possible and returns EOVERFLOW.
    556  *
    557  * If the text contains non-ASCII bytes (with bit 7 set), the whole string is
    558  * converted anyway and invalid characters are replaced with question marks
    559  * (U_SPECIAL) and the function returns EIO.
    560  *
    561  * Regardless of return value upon return @a dest will always be well-formed.
    562  *
    563  * @param dest          Destination buffer
    564  * @param size          Size of destination buffer
    565  * @param src           Space-padded ASCII.
    566  * @param n             Size of the source buffer in bytes.
    567  *
    568  * @return              EOK on success, EOVERFLOW if the text does not fit
    569  *                      destination buffer, EIO if the text contains
    570  *                      non-ASCII bytes.
    571  */
    572 int spascii_to_str(char *dest, size_t size, const uint8_t *src, size_t n)
    573 {
    574         size_t sidx;
    575         size_t didx;
    576         size_t dlast;
    577         uint8_t byte;
    578         int rc;
    579         int result;
    580 
    581         /* There must be space for a null terminator in the buffer. */
    582         assert(size > 0);
    583         result = EOK;
    584 
    585         didx = 0;
    586         dlast = 0;
    587         for (sidx = 0; sidx < n; ++sidx) {
    588                 byte = src[sidx];
    589                 if (!ascii_check(byte)) {
    590                         byte = U_SPECIAL;
    591                         result = EIO;
    592                 }
    593 
    594                 rc = chr_encode(byte, dest, &didx, size - 1);
    595                 if (rc != EOK) {
    596                         assert(rc == EOVERFLOW);
    597                         dest[didx] = '\0';
    598                         return rc;
    599                 }
    600 
    601                 /* Remember dest index after last non-empty character */
    602                 if (byte != 0x20)
    603                         dlast = didx;
    604         }
    605 
    606         /* Terminate string after last non-empty character */
    607         dest[dlast] = '\0';
    608         return result;
    609543}
    610544
     
    12811215void order_suffix(const uint64_t val, uint64_t *rv, char *suffix)
    12821216{
    1283         if (val > UINT64_C(10000000000000000000)) {
    1284                 *rv = val / UINT64_C(1000000000000000000);
     1217        if (val > 10000000000000000000ULL) {
     1218                *rv = val / 1000000000000000000ULL;
    12851219                *suffix = 'Z';
    1286         } else if (val > UINT64_C(1000000000000000000)) {
    1287                 *rv = val / UINT64_C(1000000000000000);
     1220        } else if (val > 1000000000000000000ULL) {
     1221                *rv = val / 1000000000000000ULL;
    12881222                *suffix = 'E';
    1289         } else if (val > UINT64_C(1000000000000000)) {
    1290                 *rv = val / UINT64_C(1000000000000);
     1223        } else if (val > 1000000000000000ULL) {
     1224                *rv = val / 1000000000000ULL;
    12911225                *suffix = 'T';
    1292         } else if (val > UINT64_C(1000000000000)) {
    1293                 *rv = val / UINT64_C(1000000000);
     1226        } else if (val > 1000000000000ULL) {
     1227                *rv = val / 1000000000ULL;
    12941228                *suffix = 'G';
    1295         } else if (val > UINT64_C(1000000000)) {
    1296                 *rv = val / UINT64_C(1000000);
     1229        } else if (val > 1000000000ULL) {
     1230                *rv = val / 1000000ULL;
    12971231                *suffix = 'M';
    1298         } else if (val > UINT64_C(1000000)) {
    1299                 *rv = val / UINT64_C(1000);
     1232        } else if (val > 1000000ULL) {
     1233                *rv = val / 1000ULL;
    13001234                *suffix = 'k';
    13011235        } else {
     
    13051239}
    13061240
    1307 void bin_order_suffix(const uint64_t val, uint64_t *rv, const char **suffix,
    1308     bool fixed)
    1309 {
    1310         if (val > UINT64_C(1152921504606846976)) {
    1311                 *rv = val / UINT64_C(1125899906842624);
    1312                 *suffix = "EiB";
    1313         } else if (val > UINT64_C(1125899906842624)) {
    1314                 *rv = val / UINT64_C(1099511627776);
    1315                 *suffix = "TiB";
    1316         } else if (val > UINT64_C(1099511627776)) {
    1317                 *rv = val / UINT64_C(1073741824);
    1318                 *suffix = "GiB";
    1319         } else if (val > UINT64_C(1073741824)) {
    1320                 *rv = val / UINT64_C(1048576);
    1321                 *suffix = "MiB";
    1322         } else if (val > UINT64_C(1048576)) {
    1323                 *rv = val / UINT64_C(1024);
    1324                 *suffix = "KiB";
    1325         } else {
    1326                 *rv = val;
    1327                 if (fixed)
    1328                         *suffix = "B  ";
    1329                 else
    1330                         *suffix = "B";
    1331         }
    1332 }
    1333 
    13341241/** @}
    13351242 */
Note: See TracChangeset for help on using the changeset viewer.