-: 0:Source:ZIP/str2mnyCE.c -: 0:Graph:str2mnyCE.gcno -: 0:Data:str2mnyCE.gcda -: 0:Runs:1 -: 0:Programs:1 -: 1:// (C) Copyright Adolfo Di Mare 2011 -: 2:// Use, modification and distribution are subject to the -: 3:// Boost Software License, Version 1.0. -: 4:// (See accompanying file LICENSE_1_0.txt or copy at -: 5:// http://www.boost.org/LICENSE_1_0.txt) -: 6: -: 7:// str2mnyCE.c (C) 2013 adolfo@di-mare.com -: 8: -: 9:// Define this macro if your compiler does not have a (long long) data type -: 10:// #define MNYFMT_NO_LONG_LONG -: 11: -: 12:#include "mnyfmt.h" -: 13: -: 14:#ifdef __cplusplus -: 15:// To compile the C source with a C++ compiler, override the file's extension. -: 16:// The GNU gcc compiler does this with option -x: gcc -x c++ -: 17:#endif -: 18: -: 19:/** \file str2mnyCE.c -: 20: \brief Implementation for \c str2mnyCE(). -: 21: -: 22: \author Adolfo Di Mare -: 23: \date 2011 -: 24:*/ -: 25: -: 26:/** Returns an integer value that corresponds to 'amount', scaled 10^CE digits. -: 27: - For " -102,455.87" returns '-102_455_8700' when CE==4 && dec=='.'. -: 28: - For " -102,455.07" returns '-102_455_07' when CE==2 && dec=='.'. -: 29: - For " -102,455.87" returns '-102_455' when CE==0 && dec=='.'. -: 30: - The decimal separator is 'dec'. -: 31: - CE is the 'currency exponent'. -: 32: - Requires 'CE<=6' <--> scale up to 10^6 <--> 6 decimal digits. -: 33: -: 34: \dontinclude mnyfmtts.c -: 35: \skipline test::str2mnyCE() -: 36: \until }} -: 37: -: 38: \see str2mnyCE.c -: 39:*/ 49: 40:mnyfmt_long str2mnyCE( const char * amount , char dec , unsigned CE ) { 49: 40-block 0 -: 41: #ifndef isdigit -: 42: #define isdigit(ch) ('0'<=(char)(ch) && (char)(ch)<='9') -: 43: #define remove_isdigit -: 44: #endif -: 45: static mnyfmt_long TENPOW[6+1] = { 1,10,100,1000,10000,100000,1000000 }; 49: 46: if ( CE>(-1+sizeof(TENPOW)/sizeof(*TENPOW)) ) { return 0; } 49: 46-block 0 1: 46-block 1 -: 47: 48: 48: const char *p = amount; // " -102,455.87" 48: 49: int isNegative = 0; // false 197: 50: while ( (*p!=0) && !isdigit(*p) && *p!=dec ) { isNegative = (*p=='-'); ++p; } 48: 50-block 0 149: 50-block 1 197: 50-block 2 193: 50-block 3 70: 50-block 4 159: 50-block 5 48: 51: if ( *p==0 ) { return 0; } 48: 51-block 0 4: 51-block 1 -: 52: 44: 53: mnyfmt_long nInt = 0; 241: 54: while ( *p!=0 && *p!=dec ) { 44: 54-block 0 241: 54-block 1 235: 54-block 2 197: 55: if ( isdigit(*p) ) { nInt = nInt*10 + (*p-'0'); } 197: 55-block 0 168: 55-block 1 168: 55-block 2 197: 56: ++p; 197: 56-block 0 -: 57: } -: 58: 44: 59: mnyfmt_long nFrac = 0; 44: 60: if ( *p!=0 ) { ++p; } // skip 'dec' 44: 60-block 0 38: 60-block 1 -: 61: unsigned i; 139: 62: for ( i=0; i