Lyon 2 лет назад
Родитель
Сommit
8ab5aab04b

+ 13 - 37
package/os/os_path.c

@@ -40,12 +40,12 @@ char* os_path_abspath(PikaObj* self, char* path) {
         return NULL;
     }
 #else
-    char* cwd = getcwd(NULL, 0);
+    char* cwd = pika_platform_getcwd(NULL, 0);
     if (cwd == NULL) {
         return NULL;
     }
 
-    abs_path = realpath(path, NULL);
+    abs_path = pika_platform_realpath(path, NULL);
     if (abs_path == NULL) {
         free(cwd);
         return NULL;
@@ -83,13 +83,15 @@ PIKA_BOOL os_path_exists(PikaObj* self, char* path) {
     }
 
     return PIKA_TRUE;
-#else
+#elif defined(__linux)
     struct stat statbuf;
     if (stat(path, &statbuf) == -1) {
         return PIKA_FALSE;
     }
 
     return PIKA_TRUE;
+#else
+    return pika_platform_path_exists(path);
 #endif
 }
 
@@ -115,38 +117,12 @@ PIKA_BOOL os_path_isabs(PikaObj* self, char* path) {
 
 // Returns true if the given path is a directory, false otherwise.
 PIKA_BOOL os_path_isdir(PikaObj* self, char* path) {
-    PIKA_BOOL is_dir = PIKA_FALSE;
-#ifdef _WIN32
-    DWORD attrs = GetFileAttributes((LPCWSTR)path);
-    if (attrs != INVALID_FILE_ATTRIBUTES) {
-        is_dir =
-            (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0 ? PIKA_TRUE : PIKA_FALSE;
-    }
-#else
-    struct stat st;
-    if (stat(path, &st) == 0) {
-        is_dir = S_ISDIR(st.st_mode) ? PIKA_TRUE : PIKA_FALSE;
-    }
-#endif
-    return is_dir;
+    return pika_platform_path_isdir(path);
 }
 
 // Returns true if the given path is a regular file, false otherwise.
 PIKA_BOOL os_path_isfile(PikaObj* self, char* path) {
-    PIKA_BOOL is_file = PIKA_FALSE;
-#ifdef _WIN32
-    DWORD attrs = GetFileAttributes(path);
-    if (attrs != INVALID_FILE_ATTRIBUTES) {
-        is_file =
-            (attrs & FILE_ATTRIBUTE_DIRECTORY) == 0 ? PIKA_TRUE : PIKA_FALSE;
-    }
-#else
-    struct stat st;
-    if (stat(path, &st) == 0) {
-        is_file = S_ISREG(st.st_mode) ? PIKA_TRUE : PIKA_FALSE;
-    }
-#endif
-    return is_file;
+    return pika_platform_path_isfile(path);
 }
 
 char* os_path_join(PikaObj* self, PikaTuple* paths) {
@@ -226,7 +202,7 @@ int _os_path_split(char* path, char** folder, char** file) {
         }
         strncpy(*folder, path, idx + 1);
         (*folder)[idx] = '\0';
-        *file = strdup(p + 1);
+        *file = pika_platform_strdup(p + 1);
         if (*file == NULL) {
             pika_platform_free(*folder);
             *folder = NULL;
@@ -234,11 +210,11 @@ int _os_path_split(char* path, char** folder, char** file) {
         }
         return 0;
     } else {
-        *folder = strdup(path);
+        *folder = pika_platform_strdup(path);
         if (*folder == NULL) {
             return -1;
         }
-        *file = strdup("");
+        *file = pika_platform_strdup("");
         if (*file == NULL) {
             pika_platform_free(*folder);
             *folder = NULL;
@@ -258,7 +234,7 @@ int _os_path_splitext(char* path, char** file, char** ext) {
         }
         strncpy(*file, path, idx);
         (*file)[idx] = '\0';
-        *ext = strdup(p);
+        *ext = pika_platform_strdup(p);
         if (!(*ext)) {
             pika_platform_free(*file);
             *file = NULL;
@@ -266,11 +242,11 @@ int _os_path_splitext(char* path, char** file, char** ext) {
         }
         return 0;
     } else {
-        *file = strdup(path);
+        *file = pika_platform_strdup(path);
         if (!(*file)) {
             return -1;
         }
-        *ext = strdup("");
+        *ext = pika_platform_strdup("");
         if (!(*ext)) {
             free(*file);
             *file = NULL;

+ 1 - 1
package/os/os_platform.c

@@ -138,7 +138,7 @@ int os_mkdir_platform(int mode, char* path) {
 
 int os_chdir_platform(char* path) {
     int ret = 0;
-    ret = chdir(path);
+    ret = pika_platform_chdir(path);
     if (ret == 0)
         ret = PIKA_TRUE;
     else

+ 2 - 2
port/linux/package/pikascript/pikascript-lib/os/os_path.c

@@ -91,7 +91,7 @@ PIKA_BOOL os_path_exists(PikaObj* self, char* path) {
 
     return PIKA_TRUE;
 #else
-		return pika_platform_path_exists(path);
+    return pika_platform_path_exists(path);
 #endif
 }
 
@@ -122,7 +122,7 @@ PIKA_BOOL os_path_isdir(PikaObj* self, char* path) {
 
 // Returns true if the given path is a regular file, false otherwise.
 PIKA_BOOL os_path_isfile(PikaObj* self, char* path) {
-		return pika_platform_path_isfile(path);
+    return pika_platform_path_isfile(path);
 }
 
 char* os_path_join(PikaObj* self, PikaTuple* paths) {

+ 18 - 16
src/PikaPlatform.c

@@ -295,27 +295,30 @@ PIKA_WEAK int pika_platform_mkdir(const char* pathname, int mode) {
 #elif defined(__linux) || PIKA_LINUX_COMPATIBLE
     return mkdir(pathname, mode);
 #else
-		WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
+    WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
 #endif
 }
 
-PIKA_WEAK char *pika_platform_realpath(const char *path, char *resolved_path) {
+PIKA_WEAK char* pika_platform_realpath(const char* path, char* resolved_path) {
 #if defined(_WIN32) || defined(__linux) || PIKA_LINUX_COMPATIBLE
-		return realpath(path, resolved_path);
+    return realpath(path, resolved_path);
 #else
-    if (!path || !resolved_path) return NULL;
+    if (!path || !resolved_path)
+        return NULL;
 
-    char *output = resolved_path;
-    const char *segment_start = path;
-    const char *segment_end = path;
+    char* output = resolved_path;
+    const char* segment_start = path;
+    const char* segment_end = path;
 
     while (*segment_end) {
         if (*segment_end == '/' || *(segment_end + 1) == '\0') {
-            size_t segment_len = segment_end - segment_start + (*segment_end != '/');
+            size_t segment_len =
+                segment_end - segment_start + (*segment_end != '/');
 
             if (segment_len == 1 && segment_start[0] == '.') {
                 // Skip single-dot segment
-            } else if (segment_len == 2 && segment_start[0] == '.' && segment_start[1] == '.') {
+            } else if (segment_len == 2 && segment_start[0] == '.' &&
+                       segment_start[1] == '.') {
                 // Handle double-dot segment by backtracking
                 if (output > resolved_path) {
                     output--;  // Move back one char to overwrite the last slash
@@ -339,7 +342,7 @@ PIKA_WEAK char *pika_platform_realpath(const char *path, char *resolved_path) {
             segment_end++;
         }
     }
-    
+
     if (output != resolved_path && *(output - 1) == '/') {
         output--;  // Remove trailing slash, if any
     }
@@ -350,7 +353,7 @@ PIKA_WEAK char *pika_platform_realpath(const char *path, char *resolved_path) {
 #endif
 }
 
-PIKA_WEAK int pika_platform_path_exists(const char *path){
+PIKA_WEAK int pika_platform_path_exists(const char* path) {
 #ifdef _WIN32
     DWORD attr = GetFileAttributesA((LPCWSTR)path);
     if (attr == INVALID_FILE_ATTRIBUTES) {
@@ -366,17 +369,16 @@ PIKA_WEAK int pika_platform_path_exists(const char *path){
 
     return 1;
 #else
-		WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
+    WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
 #endif
 }
 
-PIKA_WEAK int pika_platform_path_isdir(const char *path){
+PIKA_WEAK int pika_platform_path_isdir(const char* path) {
 #ifdef _WIN32
     int is_dir = 0;
     DWORD attrs = GetFileAttributes((LPCWSTR)path);
     if (attrs != INVALID_FILE_ATTRIBUTES) {
-        is_dir =
-            (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0 ? 1 : 0;
+        is_dir = (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0 ? 1 : 0;
     }
     return is_dir;
 #elif defined(__linux) || PIKA_LINUX_COMPATIBLE
@@ -387,7 +389,7 @@ PIKA_WEAK int pika_platform_path_isdir(const char *path){
     }
     return is_dir;
 #else
-		WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
+    WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
 #endif
 }
 

+ 2 - 2
src/PikaPlatform.h

@@ -203,8 +203,8 @@ char* pika_platform_getcwd(char* buf, size_t size);
 int pika_platform_chdir(const char* path);
 int pika_platform_rmdir(const char* pathname);
 int pika_platform_mkdir(const char* pathname, int mode);
-char *pika_platform_realpath(const char *path, char *resolved_path);
-int pika_platform_path_exists(const char *path);
+char* pika_platform_realpath(const char* path, char* resolved_path);
+int pika_platform_path_exists(const char* path);
 int pika_platform_path_isdir(const char* path);
 int pika_platform_path_isfile(const char* path);
 int pika_platform_remove(const char* pathname);

+ 1 - 1
src/PikaVersion.h

@@ -2,4 +2,4 @@
 #define PIKA_VERSION_MINOR 12
 #define PIKA_VERSION_MICRO 7
 
-#define PIKA_EDIT_TIME "2023/10/26 20:12:40"
+#define PIKA_EDIT_TIME "2023/10/26 21:36:47"