//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------// // convert const char * to uint bool libcore::str2uint(uint64_t &value, uint64_t default_value, const char *pstr, size_t size) { if (size == 0) { value = default_value; return false; } if (is_hex(pstr, size, true) != false) // 0x?? { if (libcore::hex2uint(value, default_value, pstr, size) != false) { return true; } } if (is_sdec(pstr, size) != false) { if (libcore::dec2sint((int64_t &)value, (int64_t &)default_value, pstr, size) != false) { return true; } } if (is_udec(pstr, size) != false) { if (libcore::dec2uint(value, default_value, pstr, size) != false) { return true; } } return false; }
// ================================== // --------------------------------- // Is a string a signed decimal? // --------------------------------- unsigned int is_dec (char * const str_i) { return (is_udec (&str_i[(str_i[0] == '-') || (str_i[0] == '+')])); }
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------// int main(int argc, char *argv[]) { int rc; if (argc != 1) { if ( (strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "-help") == 0) || (strcmp(argv[1], "--help") == 0) ) { printf("example: %s\n", argv[0]); return 0; } } rc = uint2str(); if (rc == -1) return 1; rc = sint2str(); if (rc == -1) return 1; rc = is_hex(); if (rc == -1) return 1; rc = is_udec(); if (rc == -1) return 1; rc = is_sdec(); if (rc == -1) return 1; rc = is_uint_string_overflow(); if (rc == -1) return 1; rc = is_sint_string_overflow(); if (rc == -1) return 1; rc = hex2uint(); if (rc == -1) return 1; rc = hex2sint(); if (rc == -1) return 1; rc = sign_expand(); if (rc == -1) return 1; /* // convert dec string to uint bool dec2uint(uint64_t &result, uint64_t default_value, const char *pstr, size_t size); bool dec2uint(uint64_t &result, uint64_t default_value, const char *pstr); bool dec2uint(uint64_t &result, uint64_t default_value, const std::string &str); bool dec2uint(uint64_t &result, const char *pstr, size_t size); bool dec2uint(uint64_t &result, const char *pstr); bool dec2uint(uint64_t &result, const std::string &str); */ rc = dec2sint(); if (rc == -1) return 1; rc = str2uint(); if (rc == -1) return 1; rc = str2bool(); if (rc == -1) return 1; /* // convert byte to hex string const char *bin2hex(const uint8_t byte, bool flag_up = false); // convert sibmol to number, example: '0' -> 0, 'F' -> 15 bool hex2bin(uint8_t source, uint8_t &target); // block read from handle size_t blk_read(int handle, off64_t offset, void *pdata, size_t size); // block write to handle size_t blk_write(int handle, off64_t offset, const void *pdata, size_t size, bool flag_sync = false); // block recv from handle size_t blk_recv(int handle, void *pdata, size_t size); // block send to handle size_t blk_send(int handle, const void *pdata, size_t size); // file open read only int file_open_ro(const char *pfilename); // file open read/write int file_open_rw(const char *pfilename, bool flag_truncate = false, bool flag_excl = false); // file close int file_close(int handle, bool flag_sync = false); // read data from exist file int file_get(const char *pfilename, off_t offset, void *pdata, size_t data_size); int file_get(const char *pfilename, void **pdata, size_t *data_size); int file_get(const char *pfilename, std::string &data); // write data to exist file int file_set(const char *pfilename, off_t offset, const void *pdata, size_t data_size, bool flag_sync = false, bool flag_truncate = false, bool flag_excl = false); int file_set(const char *pfilename, off_t offset, const std::string &data, bool flag_sync = false, bool flag_truncate = false, bool flag_excl = false); int file_set(const char *pfilename, const std::string &data, bool flag_sync = false, bool flag_truncate = false, bool flag_excl = false); // return (concat str1 and str2) or NULL char *concat_str(const char *pstr1, const char *pstr2); // get env var and convert to bool bool env2bool(bool &result, bool value_default, const char *pname); */ rc = get_signal_name(); if (rc == -1) return 1; rc = flip(); if (rc == -1) return 1; rc = remove_file_ext(); if (rc == -1) return 1; /* // check ipv4 string like '127.0.0.1' bool is_ipaddress(const char *str); // set signal bool set_signal(int signo, void (*sig_handler)(int)); // string to lower void strtolower(const std::string &source, std::string &target); // check pointer and strlen size_t strlen(const char *pstr); */ rc = find(); if (rc == -1) return 1; rc = bin2print(); if (rc == -1) return 1; rc = rnd(); if (rc == -1) return 1; return 0; }