added qse_str_to_ntime() and similar functions.

relocated cli error codes into the new enum type qse_cli_error_code_t
This commit is contained in:
hyung-hwan 2018-10-16 15:53:08 +00:00
parent 80ceed7bb2
commit 2915a591f0
3 changed files with 137 additions and 9 deletions

View File

@ -69,11 +69,15 @@ struct qse_opt_t
#define QSE_CLI_OPTNAME (1 << 0)
#define QSE_CLI_OPTVAL (1 << 1)
#define QSE_CLI_ERROR_INVALID_OPTNAME 1
#define QSE_CLI_ERROR_MISSING_OPTNAME 2
#define QSE_CLI_ERROR_REDUNDANT_OPTVAL 3
#define QSE_CLI_ERROR_MISSING_OPTVAL 4
#define QSE_CLI_ERROR_MEMORY 5
enum qse_cli_error_code_t
{
QSE_CLI_ERROR_INVALID_OPTNAME = 1,
QSE_CLI_ERROR_MISSING_OPTNAME = 2,
QSE_CLI_ERROR_REDUNDANT_OPTVAL = 3,
QSE_CLI_ERROR_MISSING_OPTVAL = 4,
QSE_CLI_ERROR_MEMORY = 5
};
typedef enum qse_cli_error_code_t qse_cli_error_code_t;
typedef struct qse_cli_opt_t qse_cli_opt_t;
typedef struct qse_cli_t qse_cli_t;
@ -89,10 +93,10 @@ struct qse_cli_opt_t
};
typedef int (*qse_cli_errcb_t) (
qse_cli_t* cli,
int code,
const qse_char_t* qname,
const qse_char_t* qval
qse_cli_t* cli,
qse_cli_error_code_t code,
const qse_char_t* qname,
const qse_char_t* qval
);
struct qse_cli_data_t

View File

@ -221,6 +221,28 @@ QSE_EXPORT void qse_subtime (
qse_ntime_t* z
);
/**
* The qse_strtontime() function converts a numeric text to the numeric time.
* seconds.nanoseconds
* 10.231
*/
QSE_EXPORT int qse_mbs_to_ntime (
const qse_mchar_t* text,
qse_ntime_t* ntime
);
QSE_EXPORT int qse_wcs_to_ntime (
const qse_wchar_t* text,
qse_ntime_t* ntime
);
#if defined(QSE_CHAR_IS_MCHAR)
# define qse_str_to_ntime(text,ntime) qse_mbs_to_ntime(text,ntime)
#else
# define qse_str_to_ntime(text,ntime) qse_wcs_to_ntime(text,ntime)
#endif
#if defined(__cplusplus)
}
#endif

View File

@ -25,6 +25,8 @@
*/
#include <qse/cmn/time.h>
#include <qse/cmn/chr.h>
#include "mem-prv.h"
#if defined(_WIN32)
@ -613,3 +615,103 @@ void qse_subtime (const qse_ntime_t* x, const qse_ntime_t* y, qse_ntime_t* z)
z->nsec = z->nsec + QSE_NSECS_PER_SEC;
}
}
int qse_mbs_to_ntime (const qse_mchar_t* text, qse_ntime_t* ntime)
{
const qse_mchar_t* p = text, * cp;
qse_ntime_t tv = { 0, 0 };
int neg = 0;
if (*p == QSE_MT('-'))
{
neg = 1;
p++;
}
else if (*p == QSE_MT('+'))
{
p++;
}
cp = p;
while (QSE_ISMDIGIT(*p))
{
qse_long_t oldsec = tv.sec;
tv.sec = tv.sec * 10 + (*p - QSE_MT('0'));
if (tv.sec < oldsec) return -1; /* overflow? */
p++;
}
if (cp == p) return -1;
if (*p == QSE_MT('.'))
{
qse_int32_t base = QSE_SEC_TO_NSEC(1); /* 1000000000 */
p++;
base /= 10; /* the max value is 999999999. 9 digits ony */
while (*p && QSE_ISMDIGIT(*p) && base > 0)
{
tv.nsec += (*p - QSE_MT('0')) * base;
base /= 10;
p++;
}
}
if (*p != QSE_MT('\0')) return -1;
if (neg) tv.sec *= -1;
*ntime = tv;
return 0;
}
int qse_wcs_to_ntime (const qse_wchar_t* text, qse_ntime_t* ntime)
{
const qse_wchar_t* p = text, * cp;
qse_ntime_t tv = { 0, 0 };
int neg = 0;
if (*p == QSE_WT('-'))
{
neg = 1;
p++;
}
else if (*p == QSE_WT('+'))
{
p++;
}
cp = p;
while (QSE_ISWDIGIT(*p))
{
qse_long_t oldsec = tv.sec;
tv.sec = tv.sec * 10 + (*p - QSE_WT('0'));
if (tv.sec < oldsec) return -1; /* overflow? */
p++;
}
if (cp == p) return -1;
if (*p == QSE_WT('.'))
{
qse_int32_t base = QSE_SEC_TO_NSEC(1); /* 1000000000 */
p++;
base /= 10; /* the max value is 999999999. 9 digits ony */
while (*p && QSE_ISWDIGIT(*p) && base > 0)
{
tv.nsec += (*p - QSE_WT('0')) * base;
base /= 10;
p++;
}
}
if (*p != QSE_WT('\0')) return -1;
if (neg) tv.sec *= -1;
*ntime = tv;
return 0;
}