Skip to content

Commit 5e10296

Browse files
kant2002ForNeVeR
authored andcommitted
Implement fopen, fgets, fclose, rewind, fseet, feof
1 parent 815bf9f commit 5e10296

File tree

9 files changed

+451
-30
lines changed

9 files changed

+451
-30
lines changed

Cesium.Compiler/stdlib/stdio.h

+43
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <stdarg.h>
3+
#include <stddef.h>
34

45
typedef void FILE;
56

@@ -27,3 +28,45 @@ int putchar(char _Character);
2728

2829
__cli_import("Cesium.Runtime.StdIoFunctions::PutC")
2930
int putc(char _Character, FILE* stream);
31+
32+
__cli_import("Cesium.Runtime.StdIoFunctions::FGetC")
33+
int fgetc(FILE* stream);
34+
35+
__cli_import("Cesium.Runtime.StdIoFunctions::FGetS")
36+
char* fgets(char* str, int count, FILE* stream);
37+
38+
__cli_import("Cesium.Runtime.StdIoFunctions::FEof")
39+
int feof(FILE* stream);
40+
41+
__cli_import("Cesium.Runtime.StdIoFunctions::FOpen")
42+
FILE* fopen(const char* filename, const char* mode);
43+
44+
__cli_import("Cesium.Runtime.StdIoFunctions::FOpenS")
45+
errno_t fopen_s(FILE* * streamptr,
46+
const char* filename,
47+
const char* mode);
48+
49+
__cli_import("Cesium.Runtime.StdIoFunctions::FClose")
50+
int fclose(FILE* stream);
51+
52+
__cli_import("Cesium.Runtime.StdIoFunctions::Rewind")
53+
int rewind(FILE* stream);
54+
55+
__cli_import("Cesium.Runtime.StdIoFunctions::FError")
56+
int ferror(FILE* stream);
57+
58+
#define SEEK_SET 0
59+
#define SEEK_CUR 1
60+
61+
#define SEEK_END 2
62+
63+
#define EOF (-1)
64+
65+
__cli_import("Cesium.Runtime.StdIoFunctions::FSeek")
66+
int fseek(FILE* stream, long offset, int origin);
67+
68+
__cli_import("Cesium.Runtime.StdIoFunctions::PError")
69+
void perror(const char* s);
70+
71+
__cli_import("Cesium.Runtime.StdIoFunctions::Remove")
72+
int remove(const char* pathname);

Cesium.Compiler/stdlib/string.h

+3
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ char* strdup(const char* src);
3636

3737
__cli_import("Cesium.Runtime.StringFunctions::StrNDup")
3838
char* strndup(const char* src, size_t size);
39+
40+
__cli_import("Cesium.Runtime.StringFunctions::StrError")
41+
char* strerror(int errnum);
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main(void)
5+
{
6+
FILE* tmpf = fopen("fgets_unique_name.txt", "w+"); // or tmpnam(NULL);
7+
fputs("Alan Turing\n", tmpf);
8+
fputs("John von Neumann\n", tmpf);
9+
fputs("Alonzo Church\n", tmpf);
10+
11+
rewind(tmpf);
12+
13+
char buf[8];
14+
while (fgets(buf, sizeof buf, tmpf) != NULL)
15+
printf("\"%s\"\n", buf);
16+
17+
if (feof(tmpf))
18+
puts("End of file reached");
19+
return 42;
20+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main(void)
5+
{
6+
const char* fname = "unique_name.txt"; // or tmpnam(NULL);
7+
int is_ok = EXIT_FAILURE;
8+
9+
FILE* fp = fopen(fname, "w+");
10+
if (!fp) {
11+
perror("File opening failed");
12+
return is_ok;
13+
}
14+
fputs("Hello, world!\n", fp);
15+
rewind(fp);
16+
17+
int c; // note: int, not char, required to handle EOF
18+
while ((c = fgetc(fp)) != EOF) // standard C I/O file reading loop
19+
putchar(c);
20+
21+
if (ferror(fp))
22+
puts("I/O error when reading");
23+
else if (feof(fp)) {
24+
puts("End of file is reached successfully");
25+
is_ok = 42;
26+
}
27+
28+
fclose(fp);
29+
remove(fname);
30+
return is_ok;
31+
}

Cesium.Runtime/CesiumFunctions.cs

+45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.InteropServices;
3+
using System.Text;
34

45
namespace Cesium.Runtime;
56
public static class CesiumFunctions
@@ -14,4 +15,48 @@ public static int GetOS()
1415
return 3;
1516
return 0;
1617
}
18+
19+
internal static unsafe string? Unmarshal(byte* str)
20+
{
21+
#if NETSTANDARD
22+
Encoding encoding = Encoding.UTF8;
23+
int byteLength = 0;
24+
byte* search = str;
25+
while (*search != '\0')
26+
{
27+
byteLength++;
28+
search++;
29+
}
30+
31+
int stringLength = encoding.GetCharCount(str, byteLength);
32+
string s = new('\0', stringLength);
33+
fixed (char* pTempChars = s)
34+
{
35+
encoding.GetChars(str, byteLength, pTempChars, stringLength);
36+
}
37+
38+
return s;
39+
#else
40+
return Marshal.PtrToStringUTF8((nint)str);
41+
#endif
42+
}
43+
44+
internal static unsafe UTF8String MarshalStr(string? str)
45+
{
46+
Encoding encoding = Encoding.UTF8;
47+
if (str is null)
48+
{
49+
return UTF8String.NullString;
50+
}
51+
52+
var bytes = encoding.GetBytes(str);
53+
var storage = (byte*)StdLibFunctions.Malloc((nuint)bytes.Length + 1);
54+
for (var i = 0; i < bytes.Length; i++)
55+
{
56+
storage[i] = bytes[i];
57+
}
58+
59+
storage[bytes.Length] = 0;
60+
return storage;
61+
}
1762
}

Cesium.Runtime/ErrNo.cs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Cesium.Runtime;
6+
7+
internal static class ErrNo
8+
{
9+
// Keep this file in sync with Cesium.Compiler/stdlib/errno.h
10+
public const int EPERM = 1;
11+
public const int ENOENT = 2;
12+
public const int ESRCH = 3;
13+
public const int EINTR = 4;
14+
public const int EIO = 5;
15+
public const int ENXIO = 6;
16+
public const int E2BIG = 7;
17+
public const int ENOEXEC = 8;
18+
public const int EBADF = 9;
19+
public const int ECHILD = 10;
20+
public const int EAGAIN = 11;
21+
public const int ENOMEM = 12;
22+
public const int EACCES = 13;
23+
public const int EFAULT = 14;
24+
public const int EBUSY = 16;
25+
public const int EEXIST = 17;
26+
public const int EXDEV = 18;
27+
public const int ENODEV = 19;
28+
public const int ENOTDIR = 20;
29+
public const int EISDIR = 21;
30+
public const int ENFILE = 23;
31+
public const int EMFILE = 24;
32+
public const int ENOTTY = 25;
33+
public const int EFBIG = 27;
34+
public const int ENOSPC = 28;
35+
public const int ESPIPE = 29;
36+
public const int EROFS = 30;
37+
public const int EMLINK = 31;
38+
public const int EPIPE = 32;
39+
public const int EDOM = 33;
40+
public const int EDEADLK = 36;
41+
public const int ENAMETOOLONG = 38;
42+
public const int ENOLCK = 39;
43+
public const int ENOSYS = 40;
44+
public const int ENOTEMPTY = 41;
45+
46+
public const int EINVAL = 22;
47+
public const int ERANGE = 34;
48+
public const int EILSEQ = 42;
49+
public const int STRUNCATE = 80;
50+
}

0 commit comments

Comments
 (0)