Skip to content

Commit d94c994

Browse files
committed
去除硬编码
1 parent 95d5d5b commit d94c994

File tree

4 files changed

+94
-17
lines changed

4 files changed

+94
-17
lines changed

cli/cli.vcxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
</ImportGroup>
7272
<PropertyGroup Label="UserMacros" />
7373
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
74-
<LibraryPath>D:\code\homework\json-parser\x64;$(LibraryPath)</LibraryPath>
74+
<LibraryPath>$(SolutionDir)$(PlatformTarget);$(LibraryPath)</LibraryPath>
7575
<TargetName>json</TargetName>
7676
</PropertyGroup>
7777
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -111,13 +111,13 @@
111111
<SDLCheck>false</SDLCheck>
112112
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
113113
<ConformanceMode>true</ConformanceMode>
114-
<AdditionalIncludeDirectories>D:\code\homework\json-parser\core;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
114+
<AdditionalIncludeDirectories>$(SolutionDir)core;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
115115
<SuppressStartupBanner>false</SuppressStartupBanner>
116116
</ClCompile>
117117
<Link>
118118
<SubSystem>Console</SubSystem>
119119
<GenerateDebugInformation>true</GenerateDebugInformation>
120-
<AdditionalLibraryDirectories>D:\code\homework\json-parser\cli\x64\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
120+
<AdditionalLibraryDirectories>$(SolutionDir)$(PreferredToolArchitecture)\$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
121121
<AdditionalDependencies>core.lib;%(AdditionalDependencies)</AdditionalDependencies>
122122
</Link>
123123
</ItemDefinitionGroup>

core/core.vcxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
105105
<ConformanceMode>true</ConformanceMode>
106106
<SDLCheck>false</SDLCheck>
107-
<CallingConvention>Cdecl</CallingConvention>
108107
</ClCompile>
109108
<Link>
110109
<SubSystem>Console</SubSystem>

core/parser.c

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#pragma once
21
#include "parser.h"
32
#include <stdio.h>
43
#include <stdlib.h>
@@ -18,6 +17,21 @@ void ignoreWhiteCharactor() {
1817
}
1918
}
2019

20+
int hexCharToInt(char c) {
21+
if ('0' <= c && c <= '9') {
22+
return c - '0';
23+
}
24+
else if ('a' <= c && c <= 'f') {
25+
return 10 + c - 'a';
26+
}
27+
else if ('A' <= c && c <= 'F') {
28+
return 10 + c - 'A';
29+
}
30+
else {
31+
fprintf(stderr, "Invalid hexadecimal character: %c\n", c);
32+
exit(1);
33+
}
34+
}
2135

2236
struct JsonVal* parseValue() {
2337
ignoreWhiteCharactor();
@@ -47,31 +61,95 @@ struct JsonVal* parseValue() {
4761
}
4862

4963
struct JsonVal* parseString() {
50-
char c; struct JsonString* str = JsonString_New();
64+
char c;
65+
struct JsonString* str = JsonString_New();
5166
size_t pos = ftell(f) / sizeof(char);
67+
5268
while ((c = fgetc(f)) && c != EOF && c != '"' && c != '\'') {
53-
JsonStringPushBackChar(c, str);
69+
if (c == '\\') { // Check for escape character
70+
c = fgetc(f); // Read the next character after '\'
71+
if (c == EOF) {
72+
fprintf(stderr, "Unexpected EOF after escape character.\tString value parse begin with %llu\n", pos);
73+
exit(1);
74+
}
75+
// Process escaped character
76+
switch (c) {
77+
case '"':
78+
case '\'':
79+
case '\\':
80+
// These characters are escaped as themselves
81+
JsonStringPushBackChar(c, str);
82+
break;
83+
case 'n':
84+
JsonStringPushBackChar('\n', str);
85+
break;
86+
case 't':
87+
JsonStringPushBackChar('\t', str);
88+
break;
89+
case 'u': {
90+
// Unicode escape sequence: \uXXXX
91+
int unicodeValue = 0;
92+
for (int i = 0; i < 4; ++i) {
93+
c = fgetc(f);
94+
if (c == EOF) {
95+
fprintf(stderr, "Unexpected EOF in Unicode escape sequence.\tString value parse begin with %llu\n", pos);
96+
exit(1);
97+
}
98+
unicodeValue = unicodeValue * 16 + hexCharToInt(c);
99+
}
100+
// Assuming JsonStringPushBackUnicode is a function that appends a Unicode character to the string
101+
JsonStringPushBackChar(unicodeValue, str);
102+
break;
103+
}
104+
case '0':
105+
// Null character
106+
JsonStringPushBackChar('\0', str);
107+
break;
108+
case 'x': {
109+
// Hexadecimal escape sequence: \xXX
110+
int hexValue = 0;
111+
for (int i = 0; i < 2; ++i) {
112+
c = fgetc(f);
113+
if (c == EOF) {
114+
fprintf(stderr, "Unexpected EOF in hexadecimal escape sequence.\tString value parse begin with %llu\n", pos);
115+
exit(1);
116+
}
117+
hexValue = hexValue * 16 + hexCharToInt(c);
118+
}
119+
JsonStringPushBackChar((char)hexValue, str);
120+
break;
121+
}
122+
// Add more cases for other escape sequences as needed
123+
default:
124+
fprintf(stderr, "Invalid escape sequence: \\%c\tString value parse begin with %llu\n", c, pos);
125+
exit(1);
126+
}
127+
}
128+
else {
129+
JsonStringPushBackChar(c, str);
130+
}
54131
}
55-
132+
56133
if (c != '"' && c != '\'') {
57-
fprintf(
58-
stderr,
59-
"Excepted charactor \" or ', but got EOF.\tString value parse begin with %llu\n",
60-
pos
61-
); // 写入报错到标准错误流
134+
fprintf(stderr, "Expected character \" or ', but got EOF.\tString value parse begin with %llu\n", pos);
62135
exit(1);
63136
}
137+
64138
struct JsonVal* res = (struct JsonVal*)malloc(sizeof(struct JsonVal));
65139
if (res == NULL) {
66-
// 内存分配失败 OOM (?)
67-
// 异常退出, OS进行内存回收
140+
// Memory allocation failure (OOM)
141+
// Handle the error and exit
68142
exit(1);
69143
}
70-
res->type = STRING; res->val = str;
144+
145+
res->type = STRING;
146+
res->val = str;
71147
return res;
72148
}
73149

74150

151+
152+
75153
struct JsonVal* parseNumber() {
76154
char c;
77155
struct JsonString* str = JsonString_New();

test/test.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
<SDLCheck>true</SDLCheck>
105105
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
106106
<ConformanceMode>true</ConformanceMode>
107-
<AdditionalIncludeDirectories>D:\code\homework\json-parser\core;D:\code\homework\json-parser\cutest;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
107+
<AdditionalIncludeDirectories>$(SolutionDir)core;$(SolutionDir)cutest;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
108108
</ClCompile>
109109
<Link>
110110
<SubSystem>Console</SubSystem>

0 commit comments

Comments
 (0)