-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserTest.c
More file actions
97 lines (86 loc) · 2.35 KB
/
ParserTest.c
File metadata and controls
97 lines (86 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int main(
int argc,
char *argv[])
{
/*
Instruction *list;
int ok = Parse(list);*/
return 0;
}
/*
struct
{
char *instruction;
int src1;
int src2;
int dest;
int imm;
} typedef Instruction;
int Parse(Instruction *list)
{
// Open the JSON file
FILE *file = fopen("input.json", "r");
if (file == NULL)
{
printf("Failed to open the file.\n");
return 1;
}
// Get the file size
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
rewind(file);
// Allocate memory to store the file contents
char *json_data = (char *)malloc(file_size + 1);
list = (Instruction *)malloc(file_size + 1);
if (json_data == NULL)
{
printf("Memory allocation failed.\n");
fclose(file);
return 1;
}
// Read the file contents into the allocated memory
fread(json_data, 1, file_size, file);
json_data[file_size] = '\0'; // Null-terminate the string
// Close the file
fclose(file);
// Parse the JSON data
cJSON *root = cJSON_Parse(json_data);
if (root == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
printf("Error before: %s\n", error_ptr);
}
cJSON_free(json_data); // Free memory
return 1;
}
// Iterate over the array and print each string
cJSON *instruction;
cJSON_ArrayForEach(instruction, root)
{
char *instruction_string = strdup(instruction->valuestring); // Duplicate the string
char *token = strtok(instruction_string, " "); // Split the string by spaces
while (token != NULL)
{
// Remove commas from the token
char *comma_ptr = strchr(token, ',');
while (comma_ptr != NULL)
{
memmove(comma_ptr, comma_ptr + 1, strlen(comma_ptr));
comma_ptr = strchr(comma_ptr, ',');
}
printf("%s\n okok", token);
token = strtok(NULL, " ");
free(instruction_string); // Free the duplicated string
}
// Free memory
cJSON_Delete(root);
free(json_data);
return 0;
}
}
*/