@@ -10,6 +10,7 @@ cJSON *currency__get_json_value(char* api_key, char* value) {
10
10
11
11
char * response = req (url );
12
12
size_t response_length = (size_t )strlen (response );
13
+ printf ("response length: %lu" , response_length );
13
14
14
15
cJSON * json = cJSON_ParseWithLength (response , response_length );
15
16
if (! json ) {
@@ -19,28 +20,11 @@ cJSON *currency__get_json_value(char* api_key, char* value) {
19
20
}
20
21
return 0 ;
21
22
}
22
- cJSON * json_value = cJSON_GetObjectItemCaseSensitive (json , value );
23
+ cJSON * json_value = cJSON_GetObjectItemCaseSensitive (json , value );
24
+ free (response );
23
25
return json_value ;
24
26
}
25
27
26
- char * * * allocate_2D_string_array (int totalStrings , int stringSize ) {
27
- // Allocate memory for the outer array
28
- printf ("allocating %lu to array\n" , totalStrings * sizeof (char * * ));
29
- char * * * stringList = (char * * * )malloc (totalStrings * sizeof (char * * ));
30
-
31
- // Allocate memory for each inner array
32
- for (int i = 0 ; i < totalStrings ; i ++ ) {
33
- stringList [i ] = (char * * )malloc ((2 * sizeof (char * )));
34
-
35
- // Allocate memory for each string
36
- for (int j = 0 ; j < 2 ; j ++ ) {
37
- stringList [i ][j ] = (char * )malloc (stringSize );
38
- }
39
- }
40
-
41
- return stringList ;
42
- }
43
-
44
28
void free_2D_string_array (char * * * stringList , int totalStrings ) {
45
29
for (int i = 0 ; i < totalStrings ; i ++ ) {
46
30
for (int j = 0 ; j < 2 ; j ++ ) {
@@ -63,9 +47,8 @@ char*** currency__get_codes(char *api_key) {
63
47
}
64
48
// Determine the size of the JSON array
65
49
int size = cJSON_GetArraySize (codes );
66
-
67
50
// Allocate memory for the 2D array
68
- char * * * currency_codes = (char * * * )malloc ((size * sizeof (char * * * )));
51
+ char * * * currency_codes = (char * * * )malloc ((size * sizeof (char * * )));
69
52
// Iterate over the JSON array and store the strings in the 2D array
70
53
for (int i = 0 ; i < size ; i ++ ) {
71
54
cJSON * item = cJSON_GetArrayItem (codes , i );
@@ -78,28 +61,24 @@ char*** currency__get_codes(char *api_key) {
78
61
int code_size = (strlen (code_str ) + 1 );
79
62
int name_size = (strlen (name_str ) + 1 );
80
63
81
- currency_codes [i ] = (char * * )malloc ((2 * sizeof (char * * )));
64
+ currency_codes [i ] = (char * * )malloc ((2 * sizeof (char * )));
82
65
printf ("Allocating currency_codes[%d][0].." , i );
83
66
currency_codes [i ][0 ] = (char * )malloc (code_size ); // +1 for the null terminator
84
67
printf ("Allocating currency_codes[%d][1].." , i );
85
68
currency_codes [i ][1 ] = (char * )malloc (name_size ); // +1 for the null terminator
86
69
70
+
87
71
strncpy (currency_codes [i ][0 ], code_str , code_size );
88
72
strncpy (currency_codes [i ][1 ], name_str , name_size );
89
- cJSON_free (item );
90
- cJSON_free (code );
91
- cJSON_free (name );
92
- cJSON_free (code_str );
93
- cJSON_free (name_str );
94
73
}
95
74
96
75
// Print the 2D array to verify
97
- //for (int i = 0; i < size; i++) {
76
+ // for (int i = 0; i < size; i++) {
98
77
// printf("[\"%s\",\"%s\"]\n", currency_codes[i][0], currency_codes[i][1]);
99
78
//}
100
79
101
80
// Free the allocated memory
102
- cJSON_Delete (codes );
81
+ cJSON_Delete (codes );
103
82
return currency_codes ;
104
83
}
105
84
0 commit comments