Skip to content

Commit af4a021

Browse files
committed
Part 1 OK
1 parent e135218 commit af4a021

24 files changed

+161
-2
lines changed
11.5 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
40 KB
Binary file not shown.
409 KB
Binary file not shown.
532 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PlatformToolSet=v142:VCToolArchitecture=Native32Bit:VCToolsVersion=14.27.29110:TargetPlatformVersion=10.0.18362.0:
2+
Debug|Win32|G:\code\C\Let's Build a Simple Database\|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project>
3+
<ProjectOutputs>G:\code\C\Let's Build a Simple Database\Debug\Let's Build a Simple Database-zh.exe</ProjectOutputs>
4+
<ContentFiles></ContentFiles>
5+
<SatelliteDlls></SatelliteDlls>
6+
<NonRecipeFileRefs></NonRecipeFileRefs>
7+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
 main.c
2+
Let's Build a Simple Database.vcxproj -> G:\code\C\Let's Build a Simple Database\Debug\Let's Build a Simple Database-zh.exe
13.2 KB
Binary file not shown.
51 KB
Binary file not shown.
76 KB
Binary file not shown.

Let's Build a Simple Database/Let's Build a Simple Database.vcxproj

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
<Platform>x64</Platform>
1919
</ProjectConfiguration>
2020
</ItemGroup>
21+
<ItemGroup>
22+
<ClCompile Include="main.c" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
<ClInclude Include="getline.h" />
26+
</ItemGroup>
2127
<PropertyGroup Label="Globals">
2228
<VCProjectVersion>16.0</VCProjectVersion>
2329
<Keyword>Win32Proj</Keyword>
@@ -60,15 +66,19 @@
6066
</ImportGroup>
6167
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
6268
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
69+
<Import Project="..\..\..\..\Program.props" />
6370
</ImportGroup>
6471
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
6572
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
73+
<Import Project="..\..\..\..\Program.props" />
6674
</ImportGroup>
6775
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
6876
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
77+
<Import Project="..\..\..\..\Program.props" />
6978
</ImportGroup>
7079
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
7180
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
81+
<Import Project="..\..\..\..\Program.props" />
7282
</ImportGroup>
7383
<PropertyGroup Label="UserMacros" />
7484
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">

Let's Build a Simple Database/Let's Build a Simple Database.vcxproj.filters

+10
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@
66
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
77
</Filter>
88
</ItemGroup>
9+
<ItemGroup>
10+
<ClCompile Include="main.c">
11+
<Filter>Source</Filter>
12+
</ClCompile>
13+
</ItemGroup>
14+
<ItemGroup>
15+
<ClInclude Include="getline.h">
16+
<Filter>Source</Filter>
17+
</ClInclude>
18+
</ItemGroup>
919
</Project>
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef _LETS_BUILD_A_SIMPLE_DATABASE_ZH_GETLINE_H_
2+
#define _LETS_BUILD_A_SIMPLE_DATABASE_ZH_GETLINE_H_
3+
4+
#include <malloc.h>
5+
#include <stdio.h>
6+
7+
#if !(defined linux) && !(defined __linux) && !(defined __linux__)
8+
typedef intptr_t ssize_t;
9+
#endif
10+
11+
#ifndef __GUNC__
12+
ssize_t getline(char** line, size_t* n, FILE* fp)
13+
{
14+
char* buf = *line;
15+
ssize_t c, i = 0;
16+
if (buf == NULL || *n == 0)
17+
{
18+
*line = malloc(10);
19+
buf = *line;
20+
*n = 10;
21+
}
22+
23+
while ((c = fgetc(fp)) != '\n')
24+
{
25+
if (c == EOF)
26+
return -1;
27+
if (i < (int)*n - 2)
28+
{
29+
*(buf + i++) = c;
30+
}
31+
else
32+
{
33+
*n = *n + 10;
34+
buf = realloc(buf, *n);
35+
*(buf + i++) = c;
36+
}
37+
}
38+
*(buf + i++) = '\n';
39+
*(buf + i) = '\0';
40+
return i;
41+
}
42+
#endif // __GUNC__
43+
44+
#endif // _LETS_BUILD_A_SIMPLE_DATABASE_ZH_GETLINE_H_

Let's Build a Simple Database/main.c

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <stdbool.h>
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include "getline.h"
6+
7+
typedef struct
8+
{
9+
char* buffer;
10+
size_t buffer_length;
11+
ssize_t input_length;
12+
} InputBuffer;
13+
14+
InputBuffer* new_input_buffer(void);
15+
void print_prompt(void);
16+
void read_input(InputBuffer* input_buffer);
17+
void close_input_buffer(InputBuffer* input_buffer);
18+
19+
int main(void)
20+
{
21+
InputBuffer* input_buffer = new_input_buffer();
22+
if (input_buffer == NULL)
23+
{
24+
puts("open failed.");
25+
return EXIT_FAILURE;
26+
}
27+
28+
while (true)
29+
{
30+
print_prompt();
31+
read_input(input_buffer);
32+
33+
if (!strcmp(input_buffer->buffer, ".exit"))
34+
{
35+
close_input_buffer(input_buffer);
36+
exit(EXIT_SUCCESS);
37+
}
38+
else
39+
{
40+
printf("Unrecognized command '%s'.\n", input_buffer->buffer);
41+
}
42+
}
43+
44+
return EXIT_SUCCESS;
45+
}
46+
47+
InputBuffer* new_input_buffer(void)
48+
{
49+
InputBuffer* input_buffer = (InputBuffer*)malloc(sizeof(InputBuffer));
50+
if (input_buffer != NULL)
51+
{
52+
input_buffer->buffer = NULL;
53+
input_buffer->buffer_length = 0;
54+
input_buffer->input_length = 0;
55+
}
56+
57+
return input_buffer;
58+
}
59+
60+
void print_prompt(void)
61+
{
62+
fputs("db > ", stdout);
63+
}
64+
65+
void read_input(InputBuffer* input_buffer)
66+
{
67+
ssize_t bytes_read =
68+
getline(&(input_buffer->buffer), &(input_buffer->buffer_length), stdin);
69+
70+
if (bytes_read <= 0)
71+
{
72+
puts("Error reading input");
73+
exit(EXIT_FAILURE);
74+
}
75+
76+
input_buffer->input_length = bytes_read - 1;
77+
input_buffer->buffer[bytes_read - 1] = 0;
78+
}
79+
80+
void close_input_buffer(InputBuffer* input_buffer)
81+
{
82+
free(input_buffer->buffer);
83+
free(input_buffer);
84+
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Let's Build a Simple Database-zh
22
===
33
简体中文 | [繁体中文](README.zh-TW.md)
44

5-
[db_tutorual](https://github.com/cstack/db_tutorial) 的中文翻译 :cn:
5+
[db_tutorual](https://github.com/cstack/db_tutorial) 的中文翻译 :cn: (欢迎提 Issues 和 Pull request!)
66

77
为什么做这个项目?
88
===

README.zh-TW.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Let's Build a Simple Database-zh
22
===
33
[简体中文](README.md) | 繁體中文
44

5-
[db_tutorual](https://github.com/cstack/db_tutorial) 的中文翻譯 :cn:
5+
[db_tutorual](https://github.com/cstack/db_tutorial) 的中文翻譯 :cn: (歡迎提 Issues 和 Pull request!)
66

77
為什麼做這個項目?
88
===

0 commit comments

Comments
 (0)