Changeset 21 for rxutilex/trunk/rxutilex.c
- Timestamp:
- Oct 28, 2014, 12:30:20 PM (11 years ago)
- File:
-
- 1 edited
-
rxutilex/trunk/rxutilex.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
rxutilex/trunk/rxutilex.c
r20 r21 83 83 #define US_PIDSTR_MAXZ ( CCHMAXPATH + 100 ) // ...of a process information string 84 84 #define US_TIMESTR_MAXZ 256 // ...of a formatted time string 85 #define US_NUMSTR_MAXZ 256 // ...of a formatted number string 85 86 #define US_PIPESTATUS_MAXZ 128 // ...of a pipe status string 86 87 … … 103 104 "Sys2QueryForegroundProcess", 104 105 "Sys2QueryPhysicalMemory", 106 "Sys2FormatNumber", 105 107 "Sys2FormatTime", 106 108 "Sys2GetEpochTime", … … 127 129 RexxFunctionHandler Sys2Version; 128 130 131 RexxFunctionHandler Sys2FormatNumber; 129 132 RexxFunctionHandler Sys2FormatTime; 130 133 RexxFunctionHandler Sys2GetEpochTime; … … 903 906 904 907 /* ------------------------------------------------------------------------- * 908 * Sys2FormatNumber * 909 * * 910 * Format a number using locale-specific thousands separators. The input * 911 * number may be a positive or negative integer or floating point value. It * 912 * must not contain any separators already, and any decimal point which it * 913 * contains must be a period (rather than any localized decimal symbol). * 914 * * 915 * REXX ARGUMENTS: * 916 * 1. Number to be formatted. (REQUIRED) * 917 * 2. Number of decimal places to use for floating point values. Ignored * 918 * for integer values. Defaults to 2 if not specified. * 919 * * 920 * REXX RETURN VALUE: The formatted number, or '' on error. * 921 * ------------------------------------------------------------------------- */ 922 ULONG APIENTRY Sys2FormatNumber( PSZ pszName, ULONG argc, RXSTRING argv[], PSZ pszQueue, PRXSTRING prsResult ) 923 { 924 CHAR achNumber[ US_NUMSTR_MAXZ ]; // Formatted output string 925 PSZ pszTZ, // Pointer to TZ environment var 926 pszSetTZ; 927 float fVal; 928 int iVal; 929 int iPrec; 930 931 // Make sure we have at least one valid argument (the input number) 932 if ( argc < 1 || ( !RXVALIDSTRING(argv[0]) )) return ( 40 ); 933 934 935 /* These next 4 lines really shouldn't be necessary, but without them 936 * getenv() and (apparently) tzset() may see the value of TZ as NULL 937 * if the environment variable was changed in the REXX script. 938 */ 939 DosScanEnv("TZ", &pszTZ ); 940 pszSetTZ = (PSZ) malloc( strlen( pszTZ ) + 5 ); 941 if ( pszSetTZ ) { 942 sprintf( pszSetTZ, "TZ=%s", pszTZ ); 943 putenv( pszSetTZ ); 944 } 945 946 // Use the locale and timezone settings from the environment 947 tzset(); 948 setlocale( LC_ALL, ""); 949 950 // Check for a decimal place and treat as float or integer accordingly 951 if ( strchr( argv[0].strptr, '.') != NULL ) { 952 if (( sscanf( argv[0].strptr, "%f", &fVal )) != 1 ) return ( 40 ); 953 if ( argc >= 2 && ( RXVALIDSTRING(argv[1]) ) && 954 (( sscanf( argv[1].strptr, "%d", &iPrec )) == 1 )) 955 { 956 // Use user-specified precision 957 sprintf( achNumber, "%'.*f", iPrec, fVal ); 958 } 959 else 960 sprintf( achNumber, "%'.2f", fVal ); 961 } 962 else { 963 if (( sscanf( argv[0].strptr, "%d", &iVal )) != 1 ) return ( 40 ); 964 sprintf( achNumber, "%'d", iVal ); 965 } 966 967 // Return the formatted number 968 MAKERXSTRING( *prsResult, achNumber, strlen( achNumber )); 969 970 if ( pszSetTZ ) free( pszSetTZ ); 971 return ( 0 ); 972 } 973 974 975 /* ------------------------------------------------------------------------- * 905 976 * Sys2FormatTime * 906 977 * * … … 979 1050 DosScanEnv("TZ", &pszTZ ); 980 1051 pszSetTZ = (PSZ) malloc( strlen( pszTZ ) + 5 ); 981 sprintf( pszSetTZ, "TZ=%s", pszTZ ); 982 putenv( pszSetTZ ); 1052 if ( pszSetTZ ) { 1053 sprintf( pszSetTZ, "TZ=%s", pszTZ ); 1054 putenv( pszSetTZ ); 1055 } 983 1056 984 1057 // Use the locale and timezone settings from the environment … … 991 1064 WriteErrorCode( ttSeconds, "time"); 992 1065 MAKERXSTRING( *prsResult, "", 0 ); 1066 if ( pszSetTZ ) free( pszSetTZ ); 993 1067 return 0; 994 1068 } … … 1000 1074 WriteErrorCode( 1, "gmtime"); 1001 1075 MAKERXSTRING( *prsResult, "0", 1 ); 1076 if ( pszSetTZ ) free( pszSetTZ ); 1002 1077 return 0; 1003 1078 } … … 1008 1083 WriteErrorCode( 1, "localtime"); 1009 1084 MAKERXSTRING( *prsResult, "0", 1 ); 1085 if ( pszSetTZ ) free( pszSetTZ ); 1010 1086 return 0; 1011 1087 } … … 1032 1108 WriteErrorCode( stRC, "strftime"); 1033 1109 MAKERXSTRING( *prsResult, "", 0 ); 1110 if ( pszSetTZ ) free( pszSetTZ ); 1034 1111 return ( 0 ); 1035 1112 } … … 1038 1115 MAKERXSTRING( *prsResult, szTime, strlen(szTime) ); 1039 1116 1040 free( pszSetTZ );1117 if ( pszSetTZ ) free( pszSetTZ ); 1041 1118 return ( 0 ); 1042 1119 }
Note:
See TracChangeset
for help on using the changeset viewer.
