diff options
author | Emanuel Sprung <emanuel.sprung@gmail.com> | 2019-11-11 14:08:20 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-11-11 22:04:16 +0100 |
commit | e7affa24dca571911694e262b18f6de00f882c35 (patch) | |
tree | 5c38867e3cd9b69f231a21ee1eaefa378d14e03c /Libraries | |
parent | 3042c942d871b40a3d7dbb06079d29babefb780d (diff) | |
download | serenity-e7affa24dca571911694e262b18f6de00f882c35.zip |
LibC, LibM: Add functions needed to compile python3
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibC/stdlib.cpp | 23 | ||||
-rw-r--r-- | Libraries/LibC/stdlib.h | 1 | ||||
-rw-r--r-- | Libraries/LibC/time.cpp | 14 | ||||
-rw-r--r-- | Libraries/LibC/time.h | 4 | ||||
-rw-r--r-- | Libraries/LibC/wchar.cpp | 84 | ||||
-rw-r--r-- | Libraries/LibC/wchar.h | 5 | ||||
-rw-r--r-- | Libraries/LibM/math.cpp | 6 |
7 files changed, 136 insertions, 1 deletions
diff --git a/Libraries/LibC/stdlib.cpp b/Libraries/LibC/stdlib.cpp index 1ddbe66bfd..fee9604642 100644 --- a/Libraries/LibC/stdlib.cpp +++ b/Libraries/LibC/stdlib.cpp @@ -3,6 +3,7 @@ #include <AK/StdLibExtras.h> #include <AK/String.h> #include <AK/Types.h> +#include <AK/Utf8View.h> #include <Kernel/Syscall.h> #include <alloca.h> #include <assert.h> @@ -469,6 +470,28 @@ int wctomb(char*, wchar_t) ASSERT_NOT_REACHED(); } +size_t wcstombs(char* dest, const wchar_t* src, size_t max) +{ + char* originalDest = dest; + while ((size_t)(dest - originalDest) < max) { + StringView v { (const char*)src, sizeof(wchar_t) }; + + // FIXME: dependent on locale, for now utf-8 is supported. + Utf8View utf8 { v }; + if (*utf8.begin() == '\0') { + *dest = '\0'; + return (size_t)(dest - originalDest); // Exclude null character in returned size + } + + for (auto byte : utf8) { + if (byte != '\0') + *dest++ = byte; + } + ++src; + } + return max; +} + template<typename T, T min_value, T max_value> static T strtol_impl(const char* nptr, char** endptr, int base) { diff --git a/Libraries/LibC/stdlib.h b/Libraries/LibC/stdlib.h index 4301b703b3..3cf039abdc 100644 --- a/Libraries/LibC/stdlib.h +++ b/Libraries/LibC/stdlib.h @@ -46,6 +46,7 @@ void* bsearch(const void* key, const void* base, size_t nmemb, size_t size, int size_t mbstowcs(wchar_t*, const char*, size_t); size_t mbtowc(wchar_t*, const char*, size_t); int wctomb(char*, wchar_t); +size_t wcstombs(char*, const wchar_t*, size_t); #define RAND_MAX 32767 int rand(); diff --git a/Libraries/LibC/time.cpp b/Libraries/LibC/time.cpp index cdc48f0f08..4cd2e47fd9 100644 --- a/Libraries/LibC/time.cpp +++ b/Libraries/LibC/time.cpp @@ -134,4 +134,18 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec* reques __RETURN_WITH_ERRNO(rc, rc, -1); } +int clock_getres(clockid_t, struct timespec*) +{ + ASSERT_NOT_REACHED(); +} + +struct tm* gmtime_r(const time_t*, struct tm*) +{ + ASSERT_NOT_REACHED(); +} + +struct tm* localtime_r(const time_t*, struct tm*) +{ + ASSERT_NOT_REACHED(); +} } diff --git a/Libraries/LibC/time.h b/Libraries/LibC/time.h index fec9076e99..752ad58d06 100644 --- a/Libraries/LibC/time.h +++ b/Libraries/LibC/time.h @@ -48,11 +48,13 @@ typedef int clockid_t; int clock_gettime(clockid_t, struct timespec*); int clock_nanosleep(clockid_t, int flags, const struct timespec* requested_sleep, struct timespec* remaining_sleep); +int clock_getres(clockid_t, struct timespec* result); +struct tm* gmtime_r(const time_t* timep, struct tm* result); +struct tm* localtime_r(const time_t* timep, struct tm* result); double difftime(time_t, time_t); size_t strftime(char* s, size_t max, const char* format, const struct tm*); #define difftime(t1, t0) (double)(t1 - t0) - __END_DECLS diff --git a/Libraries/LibC/wchar.cpp b/Libraries/LibC/wchar.cpp index e1bc54a7c5..4308c6745b 100644 --- a/Libraries/LibC/wchar.cpp +++ b/Libraries/LibC/wchar.cpp @@ -1,3 +1,4 @@ +#include <AK/Assertions.h> #include <wchar.h> extern "C" { @@ -18,6 +19,14 @@ wchar_t* wcscpy(wchar_t* dest, const wchar_t* src) return originalDest; } +wchar_t* wcsncpy(wchar_t* dest, const wchar_t* src, size_t num) +{ + wchar_t* originalDest = dest; + while (((*dest++ = *src++) != '\0') && ((size_t)(dest - originalDest) < num)) + ; + return originalDest; +} + int wcscmp(const wchar_t* s1, const wchar_t* s2) { while (*s1 == *s2++) @@ -37,4 +46,79 @@ wchar_t* wcschr(const wchar_t* str, int c) } } +const wchar_t* wcsrchr(const wchar_t* str, wchar_t wc) +{ + wchar_t* last = nullptr; + wchar_t c; + for (; (c = *str); ++str) { + if (c == wc) + last = const_cast<wchar_t*>(str); + } + return last; +} + +wchar_t* wcscat(wchar_t* dest, const wchar_t* src) +{ + size_t dest_length = wcslen(dest); + size_t i; + for (i = 0; src[i] != '\0'; i++) + dest[dest_length + i] = src[i]; + dest[dest_length + i] = '\0'; + return dest; +} + +wchar_t* wcstok(wchar_t* str, const wchar_t* delim, wchar_t** ptr) +{ + wchar_t* used_str = str; + if (!used_str) { + used_str = *ptr; + } + + size_t token_start = 0; + size_t token_end = 0; + size_t str_len = wcslen(used_str); + size_t delim_len = wcslen(delim); + + for (size_t i = 0; i < str_len; ++i) { + bool is_proper_delim = false; + + for (size_t j = 0; j < delim_len; ++j) { + if (used_str[i] == delim[j]) { + // Skip beginning delimiters + if (token_end - token_start == 0) { + ++token_start; + break; + } + + is_proper_delim = true; + } + } + + ++token_end; + if (is_proper_delim && token_end > 0) { + --token_end; + break; + } + } + + if (used_str[token_start] == '\0') + return nullptr; + + if (token_end == 0) { + return &used_str[token_start]; + } + + used_str[token_end] = '\0'; + return &used_str[token_start]; +} + +wchar_t* wcsncat(wchar_t* dest, const wchar_t* src, size_t n) +{ + size_t dest_length = wcslen(dest); + size_t i; + for (i = 0; i < n && src[i] != '\0'; i++) + dest[dest_length + i] = src[i]; + dest[dest_length + i] = '\0'; + return dest; +} } diff --git a/Libraries/LibC/wchar.h b/Libraries/LibC/wchar.h index 0c69365e7f..67cbf1b82b 100644 --- a/Libraries/LibC/wchar.h +++ b/Libraries/LibC/wchar.h @@ -11,7 +11,12 @@ __BEGIN_DECLS size_t wcslen(const wchar_t*); wchar_t* wcscpy(wchar_t*, const wchar_t*); +wchar_t* wcsncpy(wchar_t*, const wchar_t*, size_t); int wcscmp(const wchar_t*, const wchar_t*); wchar_t* wcschr(const wchar_t*, int); +const wchar_t* wcsrchr(const wchar_t*, wchar_t); +wchar_t* wcscat(wchar_t*, const wchar_t*); +wchar_t* wcstok(wchar_t*, const wchar_t*, wchar_t**); +wchar_t* wcsncat(wchar_t*, const wchar_t*, size_t); __END_DECLS diff --git a/Libraries/LibM/math.cpp b/Libraries/LibM/math.cpp index f7d370cca4..a8c9b747e2 100644 --- a/Libraries/LibM/math.cpp +++ b/Libraries/LibM/math.cpp @@ -301,4 +301,10 @@ float ceilf(float value) } return as_int + 1; } + +double modf(double x, double* intpart) +{ + *intpart = (double)((int)(x)); + return x - (int)x; +} } |