Commit 76994c52 authored by rswindell's avatar rswindell
Browse files

Move parseBytes() from ini_file.c (private/static) to genwrap.c (public) and

rename to parse_byte_count(), so we can use it more places (e.g. jsexec).
parent bfdebd36
......@@ -8,7 +8,7 @@
* @format.tab-size 4 (Plain Text/Source Code File Header) *
* @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
* *
* Copyright 2012 Rob Swindell - http://www.synchro.net/copyright.html *
* Copyright Rob Swindell - http://www.synchro.net/copyright.html *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License *
......@@ -194,6 +194,43 @@ char* DLLCALL c_escape_str(const char* src, char* dst, size_t maxlen, BOOL ctrl_
return(dst);
}
/* Returns a byte count parsed from the 'str' argument, supporting power-of-2
* short-hands (e.g. 'K' for kibibytes).
* If 'unit' is specified (greater than 1), result is divided by this amount.
*
* Moved from ini_file.c/parseBytes()
*/
int64_t DLLCALL parse_byte_count(const char* str, ulong unit)
{
char* p=NULL;
double bytes;
bytes=strtod(str,&p);
if(p!=NULL) {
switch(toupper(*p)) {
case 'E':
bytes*=1024;
/* fall-through */
case 'P':
bytes*=1024;
/* fall-through */
case 'T':
bytes*=1024;
/* fall-through */
case 'G':
bytes*=1024;
/* fall-through */
case 'M':
bytes*=1024;
/* fall-through */
case 'K':
bytes*=1024;
break;
}
}
return((int64_t)(unit>1 ? (bytes/unit):bytes));
}
/****************************************************************************/
/* Convert ASCIIZ string to upper case */
/****************************************************************************/
......
......@@ -8,7 +8,7 @@
* @format.tab-size 4 (Plain Text/Source Code File Header) *
* @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
* *
* Copyright 2011 Rob Swindell - http://www.synchro.net/copyright.html *
* Copyright Rob Swindell - http://www.synchro.net/copyright.html *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License *
......@@ -367,6 +367,9 @@ DLLEXPORT char* DLLCALL c_unescape_str(char* str);
DLLEXPORT char DLLCALL c_unescape_char_ptr(const char* str, char** endptr);
DLLEXPORT char DLLCALL c_unescape_char(char ch);
/* Power-of-2 byte count string parser (e.g. "100K" returns 102400 if unit is 1) */
DLLEXPORT int64_t DLLCALL parse_byte_count(const char*, ulong unit);
/* Microsoft (e.g. DOS/Win32) real-time system clock API (ticks since process started) */
typedef clock_t msclock_t;
#if defined(_WIN32) || defined(__OS2__)
......
......@@ -1244,37 +1244,6 @@ ulong DLLCALL iniGetLongInt(str_list_t list, const char* section, const char* ke
return(parseLongInteger(vp));
}
static int64_t parseBytes(const char* value, ulong unit)
{
char* p=NULL;
double bytes;
bytes=strtod(value,&p);
if(p!=NULL) {
switch(toupper(*p)) {
case 'E':
bytes*=1024;
/* fall-through */
case 'P':
bytes*=1024;
/* fall-through */
case 'T':
bytes*=1024;
/* fall-through */
case 'G':
bytes*=1024;
/* fall-through */
case 'M':
bytes*=1024;
/* fall-through */
case 'K':
bytes*=1024;
break;
}
}
return((int64_t)(unit>1 ? (bytes/unit):bytes));
}
int64_t DLLCALL iniReadBytes(FILE* fp, const char* section, const char* key, ulong unit, int64_t deflt)
{
char* value;
......@@ -1286,7 +1255,7 @@ int64_t DLLCALL iniReadBytes(FILE* fp, const char* section, const char* key, ulo
if(*value==0) /* blank value */
return(deflt);
return(parseBytes(value,unit));
return(parse_byte_count(value,unit));
}
int64_t DLLCALL iniGetBytes(str_list_t list, const char* section, const char* key, ulong unit, int64_t deflt)
......@@ -1298,7 +1267,7 @@ int64_t DLLCALL iniGetBytes(str_list_t list, const char* section, const char* ke
if(vp==NULL || *vp==0) /* blank value or missing key */
return(deflt);
return(parseBytes(vp,unit));
return(parse_byte_count(vp,unit));
}
#if !defined(NO_SOCKET_SUPPORT)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment