Reformat libmaxsi format.cpp.

This commit is contained in:
Jonas 'Sortie' Termansen 2012-09-28 23:20:10 +02:00
parent 63b1c50cd2
commit 6f08593801
1 changed files with 295 additions and 300 deletions

View File

@ -1,6 +1,6 @@
/****************************************************************************** /*******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. Copyright(C) Jonas 'Sortie' Termansen 2011.
This file is part of LibMaxsi. This file is part of LibMaxsi.
@ -11,8 +11,8 @@
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
more details. details.
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>. along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
@ -20,313 +20,308 @@
format.cpp format.cpp
Provides printf formatting functions that uses callbacks. Provides printf formatting functions that uses callbacks.
******************************************************************************/ *******************************************************************************/
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
namespace Maxsi namespace String {
static int ConvertInt32(int32_t num, char* dest)
{ {
namespace String int result = 0;
int32_t copy = num;
if ( num < 0 )
{ {
int ConvertInt32(int32_t num, char* dest) *dest++ = '-';
result++;
int offset = 0;
while ( copy < -9 ) { copy /= 10; offset++; }
result += offset;
while ( offset >= 0 )
{ {
int result = 0; dest[offset] = '0' - num % 10; num /= 10; offset--;
int32_t copy = num;
if ( num < 0 )
{
*dest++ = '-';
result++;
int offset = 0;
while ( copy < -9 ) { copy /= 10; offset++; }
result += offset;
while ( offset >= 0 )
{
dest[offset] = '0' - num % 10; num /= 10; offset--;
}
}
else
{
int offset = 0;
while ( copy > 9 ) { copy /= 10; offset++; }
result += offset;
while ( offset >= 0 )
{
dest[offset] = '0' + num % 10; num /= 10; offset--;
}
}
return result + 1;
} }
}
int ConvertInt64(int64_t num, char* dest) else
{
int offset = 0;
while ( copy > 9 ) { copy /= 10; offset++; }
result += offset;
while ( offset >= 0 )
{ {
int result = 0; dest[offset] = '0' + num % 10; num /= 10; offset--;
int64_t copy = num;
if ( num < 0 )
{
*dest++ = '-';
result++;
int offset = 0;
while ( copy < -9 ) { copy /= 10; offset++; }
result += offset;
while ( offset >= 0 )
{
dest[offset] = '0' - num % 10; num /= 10; offset--;
}
}
else
{
int offset = 0;
while ( copy > 9 ) { copy /= 10; offset++; }
result += offset;
while ( offset >= 0 )
{
dest[offset] = '0' + num % 10; num /= 10; offset--;
}
}
return result + 1;
}
int ConvertUInt32(uint32_t num, char* dest)
{
int result = 0;
uint32_t copy = num;
int offset = 0;
while ( copy > 9 ) { copy /= 10; offset++; }
result += offset;
while ( offset >= 0 )
{
dest[offset] = '0' + num % 10; num /= 10; offset--;
}
return result + 1;
}
int ConvertUInt64(uint64_t num, char* dest)
{
int result = 0;
uint64_t copy = num;
int offset = 0;
while ( copy > 9 ) { copy /= 10; offset++; }
result += offset;
while ( offset >= 0 )
{
dest[offset] = '0' + num % 10; num /= 10; offset--;
}
return result + 1;
}
int ConvertUInt32Hex(uint32_t num, char* dest)
{
char chars[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
int result = 0;
uint32_t copy = num;
int offset = 0;
while ( copy > 15 ) { copy /= 16; offset++; }
result += offset;
while ( offset >= 0 )
{
dest[offset] = chars[num % 16]; num /= 16; offset--;
}
return result + 1;
}
int ConvertUInt64Hex(uint64_t num, char* dest)
{
char chars[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
int result = 0;
uint64_t copy = num;
int offset = 0;
while ( copy > 15 ) { copy /= 16; offset++; }
result += offset;
while ( offset >= 0 )
{
dest[offset] = chars[num % 16]; num /= 16; offset--;
}
return result + 1;
} }
} }
namespace Format return result + 1;
{ }
#define READY_SIZE 128
static int ConvertInt64(int64_t num, char* dest)
#define READY_FLUSH() \ {
ready[readylen] = '\0'; \ int result = 0;
if ( 0 < readylen && callback && callback(user, ready, readylen) != readylen ) { return SIZE_MAX; } \ int64_t copy = num;
written += readylen; readylen = 0;
if ( num < 0 )
extern "C" size_t vprintf_callback(size_t (*callback)(void*, const char*, size_t), {
void* user, *dest++ = '-';
const char* restrict format, result++;
va_list parameters)
{ int offset = 0;
size_t written = 0; while ( copy < -9 ) { copy /= 10; offset++; }
size_t readylen = 0; result += offset;
char ready[READY_SIZE + 1]; while ( offset >= 0 )
{
while ( *format != '\0' ) dest[offset] = '0' - num % 10; num /= 10; offset--;
{ }
char c = *(format++); }
else
if ( c != '%' ) {
{ int offset = 0;
if ( READY_SIZE <= readylen ) { READY_FLUSH(); } while ( copy > 9 ) { copy /= 10; offset++; }
ready[readylen++] = c; result += offset;
continue; while ( offset >= 0 )
} {
dest[offset] = '0' + num % 10; num /= 10; offset--;
if ( *format == '%' ) { continue; } }
}
const unsigned UNSIGNED = 0;
const unsigned INTEGER = (1<<0); return result + 1;
const unsigned BIT64 = (1<<1); }
const unsigned HEX = (1<<2);
const unsigned STRING = 8; static int ConvertUInt32(uint32_t num, char* dest)
const unsigned CHARACTER = 9; {
#if defined(__x86_64__) int result = 0;
const unsigned WORDWIDTH = BIT64; uint32_t copy = num;
#else int offset = 0;
const unsigned WORDWIDTH = 0; while ( copy > 9 ) { copy /= 10; offset++; }
#endif result += offset;
while ( offset >= 0 )
// TODO: Support signed datatypes! {
dest[offset] = '0' + num % 10; num /= 10; offset--;
unsigned type = 0; }
bool scanning = true; return result + 1;
while ( scanning ) }
{
switch( *(format++) ) static int ConvertUInt64(uint64_t num, char* dest)
{ {
case '3': int result = 0;
case '2': uint64_t copy = num;
break; int offset = 0;
case '6': while ( copy > 9 ) { copy /= 10; offset++; }
case '4': result += offset;
type |= BIT64; while ( offset >= 0 )
break; {
case 'p': dest[offset] = '0' + num % 10; num /= 10; offset--;
type = WORDWIDTH | HEX; }
scanning = false;
break; return result + 1;
case 't': }
type |= INTEGER;
case 'z': static int ConvertUInt32Hex(uint32_t num, char* dest)
case 'l': {
if ( type & WORDWIDTH ) { type |= BIT64; } char chars[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
type |= WORDWIDTH; 'A', 'B', 'C', 'D', 'E', 'F' };
break; int result = 0;
case 'j': uint32_t copy = num;
type |= BIT64; int offset = 0;
break; while ( copy > 15 ) { copy /= 16; offset++; }
case 'u': result += offset;
type |= UNSIGNED; while ( offset >= 0 )
scanning = false; {
break; dest[offset] = chars[num % 16]; num /= 16; offset--;
case 'd': }
case 'i':
type |= INTEGER; return result + 1;
scanning = false; }
break;
case 'x': static int ConvertUInt64Hex(uint64_t num, char* dest)
case 'X': {
type |= HEX; char chars[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
scanning = false; 'A', 'B', 'C', 'D', 'E', 'F' };
break; int result = 0;
case 's': uint64_t copy = num;
type = STRING; int offset = 0;
scanning = false; while ( copy > 15 ) { copy /= 16; offset++; }
break; result += offset;
case 'c': while ( offset >= 0 )
type = CHARACTER; {
scanning = false; dest[offset] = chars[num % 16]; num /= 16; offset--;
break; }
default:
return SIZE_MAX; return result + 1;
} }
}
} // namespace String
switch ( type )
{ #define READY_SIZE 128
case INTEGER:
{ #define READY_FLUSH() \
if ( READY_SIZE - readylen < 10 ) { READY_FLUSH(); } ready[readylen] = '\0'; \
int32_t num = va_arg(parameters, int32_t); if ( 0 < readylen && callback && callback(user, ready, readylen) != readylen ) { return SIZE_MAX; } \
readylen += String::ConvertInt32(num, ready + readylen); written += readylen; readylen = 0;
break;
} extern "C" size_t vprintf_callback(size_t (*callback)(void*, const char*, size_t),
case UNSIGNED: void* user,
{ const char* restrict format,
if ( READY_SIZE - readylen < 10 ) { READY_FLUSH(); } va_list parameters)
uint32_t num = va_arg(parameters, uint32_t); {
readylen += String::ConvertUInt32(num, ready + readylen); size_t written = 0;
break; size_t readylen = 0;
} char ready[READY_SIZE + 1];
case INTEGER | BIT64:
{ while ( *format != '\0' )
if ( READY_SIZE - readylen < 10 ) { READY_FLUSH(); } {
int64_t num = va_arg(parameters, int64_t); char c = *(format++);
readylen += String::ConvertInt64(num, ready + readylen);
break; if ( c != '%' )
} {
case UNSIGNED | BIT64: if ( READY_SIZE <= readylen ) { READY_FLUSH(); }
{ ready[readylen++] = c;
if ( READY_SIZE - readylen < 20 ) { READY_FLUSH(); } continue;
uint64_t num = va_arg(parameters, uint64_t); }
readylen += String::ConvertUInt64(num, ready + readylen);
break; if ( *format == '%' ) { continue; }
}
case INTEGER | HEX: const unsigned UNSIGNED = 0;
case UNSIGNED | HEX: const unsigned INTEGER = (1<<0);
{ const unsigned BIT64 = (1<<1);
if ( READY_SIZE - readylen < 8 ) { READY_FLUSH(); } const unsigned HEX = (1<<2);
uint32_t num = va_arg(parameters, uint32_t); const unsigned STRING = 8;
readylen += String::ConvertUInt32Hex(num, ready + readylen); const unsigned CHARACTER = 9;
break; #if defined(__x86_64__)
} const unsigned WORDWIDTH = BIT64;
case INTEGER | BIT64 | HEX: #else
case UNSIGNED | BIT64 | HEX: const unsigned WORDWIDTH = 0;
{ #endif
if ( READY_SIZE - readylen < 16 ) { READY_FLUSH(); }
uint64_t num = va_arg(parameters, uint64_t); // TODO: Support signed datatypes!
readylen += String::ConvertUInt64Hex(num, ready + readylen);
break; unsigned type = 0;
}
case STRING: bool scanning = true;
{ while ( scanning )
READY_FLUSH(); {
const char* str = va_arg(parameters, const char*); switch( *(format++) )
size_t len = strlen(str); {
if ( callback && callback(user, str, len) != len ) { return SIZE_MAX; } case '3':
written += len; case '2':
break; break;
} case '6':
case CHARACTER: case '4':
{ type |= BIT64;
int c = va_arg(parameters, int); break;
if ( READY_SIZE <= readylen ) { READY_FLUSH(); } case 'p':
ready[readylen++] = c; type = WORDWIDTH | HEX;
break; scanning = false;
} break;
} case 't':
} type |= INTEGER;
case 'z':
READY_FLUSH(); case 'l':
if ( type & WORDWIDTH ) { type |= BIT64; }
return written; type |= WORDWIDTH;
} break;
} case 'j':
type |= BIT64;
break;
case 'u':
type |= UNSIGNED;
scanning = false;
break;
case 'd':
case 'i':
type |= INTEGER;
scanning = false;
break;
case 'x':
case 'X':
type |= HEX;
scanning = false;
break;
case 's':
type = STRING;
scanning = false;
break;
case 'c':
type = CHARACTER;
scanning = false;
break;
default:
return SIZE_MAX;
}
}
switch ( type )
{
case INTEGER:
{
if ( READY_SIZE - readylen < 10 ) { READY_FLUSH(); }
int32_t num = va_arg(parameters, int32_t);
readylen += String::ConvertInt32(num, ready + readylen);
break;
}
case UNSIGNED:
{
if ( READY_SIZE - readylen < 10 ) { READY_FLUSH(); }
uint32_t num = va_arg(parameters, uint32_t);
readylen += String::ConvertUInt32(num, ready + readylen);
break;
}
case INTEGER | BIT64:
{
if ( READY_SIZE - readylen < 10 ) { READY_FLUSH(); }
int64_t num = va_arg(parameters, int64_t);
readylen += String::ConvertInt64(num, ready + readylen);
break;
}
case UNSIGNED | BIT64:
{
if ( READY_SIZE - readylen < 20 ) { READY_FLUSH(); }
uint64_t num = va_arg(parameters, uint64_t);
readylen += String::ConvertUInt64(num, ready + readylen);
break;
}
case INTEGER | HEX:
case UNSIGNED | HEX:
{
if ( READY_SIZE - readylen < 8 ) { READY_FLUSH(); }
uint32_t num = va_arg(parameters, uint32_t);
readylen += String::ConvertUInt32Hex(num, ready + readylen);
break;
}
case INTEGER | BIT64 | HEX:
case UNSIGNED | BIT64 | HEX:
{
if ( READY_SIZE - readylen < 16 ) { READY_FLUSH(); }
uint64_t num = va_arg(parameters, uint64_t);
readylen += String::ConvertUInt64Hex(num, ready + readylen);
break;
}
case STRING:
{
READY_FLUSH();
const char* str = va_arg(parameters, const char*);
size_t len = strlen(str);
if ( callback && callback(user, str, len) != len ) { return SIZE_MAX; }
written += len;
break;
}
case CHARACTER:
{
int c = va_arg(parameters, int);
if ( READY_SIZE <= readylen ) { READY_FLUSH(); }
ready[readylen++] = c;
break;
}
}
}
READY_FLUSH();
return written;
} }