comparison th_string.c @ 479:77ad030e82c9

Add th_get_int() helper function.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 27 Nov 2018 07:48:58 +0200
parents 694c85f4e354
children e4ce60239d16
comparison
equal deleted inserted replaced
478:b1e80180818a 479:77ad030e82c9
1012 else 1012 else
1013 return FALSE; 1013 return FALSE;
1014 } 1014 }
1015 1015
1016 1016
1017 BOOL th_get_int(const char *str, unsigned int *value, BOOL *neg)
1018 {
1019 int ch;
1020 BOOL hex = FALSE;
1021
1022 // Is the value negative?
1023 if (*str == '-')
1024 {
1025 if (neg == NULL)
1026 return FALSE;
1027
1028 *neg = TRUE;
1029 str++;
1030 }
1031 else
1032 if (neg != NULL)
1033 *neg = FALSE;
1034
1035 // Is it hexadecimal?
1036 if (*str == '$')
1037 {
1038 hex = TRUE;
1039 str++;
1040 }
1041 else
1042 if (str[0] == '0' && str[1] == 'x')
1043 {
1044 hex = TRUE;
1045 str += 2;
1046 }
1047
1048 // Parse the value
1049 *value = 0;
1050 if (hex)
1051 {
1052 while ((ch = *str++))
1053 {
1054 if (ch >= '0' && ch <= '9')
1055 {
1056 *value <<= 4;
1057 *value |= ch - '0';
1058 }
1059 else
1060 if (ch >= 'A' && ch <= 'F')
1061 {
1062 *value <<= 4;
1063 *value |= ch - 'A' + 10;
1064 }
1065 else
1066 if (ch >= 'a' && ch <= 'f')
1067 {
1068 *value <<= 4;
1069 *value |= ch - 'a' + 10;
1070 }
1071 else
1072 return FALSE;
1073 }
1074 }
1075 else
1076 {
1077 while ((ch = *str++))
1078 {
1079 if (ch >= '0' && ch <= '9')
1080 {
1081 *value *= 10;
1082 *value += ch - '0';
1083 }
1084 else
1085 return FALSE;
1086 }
1087 }
1088 return TRUE;
1089 }
1090
1091
1017 static void th_pad(FILE *outFile, int count) 1092 static void th_pad(FILE *outFile, int count)
1018 { 1093 {
1019 while (count--) 1094 while (count--)
1020 fputc(' ', outFile); 1095 fputc(' ', outFile);
1021 } 1096 }