소스 검색

feat: 检查文件

检查文件是否存在和是否具有权限
SongZihuan 4 년 전
부모
커밋
a07292e4fc
7개의 변경된 파일31개의 추가작업 그리고 20개의 파일을 삭제
  1. 2 2
      include/inter.h
  2. 1 0
      include/macro.h
  3. 1 1
      include/virtualmath.h
  4. 4 9
      main.c
  5. 4 2
      parser/grammar.c
  6. 2 1
      parser/token.c
  7. 17 5
      src/inter.c

+ 2 - 2
include/inter.h

@@ -23,6 +23,6 @@ typedef struct Inter Inter;
 Inter *makeInter(char *debug);
 void freeInter(Inter *inter, bool self);
 void setBaseInterData(struct Inter *inter);
-Inter *newInter(char *code_file, char *debug_dir, struct Result *global_result);
-Inter *runBaseInter(char *code_file, char *debug_dir);
+Inter *newInter(char *code_file, char *debug_dir,struct Result *global_result, int *status);
+Inter *runBaseInter(char *code_file, char *debug_dir, int *status);
 #endif //VIRTUALMATH_INTER_H

+ 1 - 0
include/macro.h

@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <stdarg.h>
 #include <getopt.h>
+#include <unistd.h>
 
 // 布尔逻辑的定义
 #define bool int

+ 1 - 1
include/virtualmath.h

@@ -5,6 +5,6 @@
 
 typedef struct Inter Inter;
 typedef struct Result Result;
-struct Inter *runBaseInter(char *code_file, char *debug_dir);
+Inter *runBaseInter(char *code_file, char *debug_dir, int *status);
 void freeInter(Inter *inter, int self);
 #endif //VIRTUALMATH_VIRTUALMATH_H

+ 4 - 9
main.c

@@ -2,25 +2,20 @@
 
 int main(int argc, char *argv[]) {
     Inter *global_inter = NULL;
+    int status = 1;
 
     if (getArgs(argc, argv))
         goto args_error_;
 
-    global_inter = runBaseInter(args.file, args.log_file);
+    global_inter = runBaseInter(args.file, args.log_file, &status);
 
     freeInter(global_inter, true);
-    freeArgs();
-    return 0;
-
-    args_error_:
-    freeArgs();
-    return 1;
+    args_error_: freeArgs();
+    return status;
 }
 
 
 /**
- * TODO-szh 使用inter的Data成员
  * TODO-szh 面向对象
  * TODO-szh import和include语句
- * TODO-szh 检查文件是否存在
  */

+ 4 - 2
parser/grammar.c

@@ -9,8 +9,10 @@ ParserMessage *makeParserMessage(char *file_dir, char *debug){
 #if OUT_LOG
     if (debug != NULL){
         char *debug_dir = memStrcat(debug, PASERS_LOG), *grammar_dir = memStrcat(debug, GRAMMAR_LOG);
-        tmp->paser_debug = fopen(debug_dir, "w");
-        tmp->grammar_debug = fopen(grammar_dir, "w");
+        if (access(debug_dir, F_OK) != 0 || access(debug_dir, W_OK) == 0)
+            tmp->paser_debug = fopen(debug_dir, "w");
+        if (access(grammar_dir, F_OK) != 0 || access(debug_dir, W_OK) == 0)
+            tmp->grammar_debug = fopen(grammar_dir, "w");
         memFree(debug_dir);
         memFree(grammar_dir);
     }

+ 2 - 1
parser/token.c

@@ -70,7 +70,8 @@ TokenMessage *makeTokenMessage(char *file_dir, char *debug) {
 #if OUT_LOG
     if (debug != NULL){
         char *debug_dir = memStrcat(debug, LEXICAL_LOG);
-        tm->debug = fopen(debug_dir, "w");
+        if (access(debug_dir, F_OK) != 0 || access(debug_dir, W_OK) == 0)
+            tm->debug = fopen(debug_dir, "w");
         memFree(debug_dir);
     }
     else{

+ 17 - 5
src/inter.c

@@ -1,13 +1,25 @@
 #include "__virtualmath.h"
 
-Inter *runBaseInter(char *code_file, char *debug_dir) {
+Inter *runBaseInter(char *code_file, char *debug_dir, int *status) {
     Result global_result;
-    return newInter(code_file, debug_dir, &global_result);
+    return newInter(code_file, debug_dir, &global_result, status);
 }
 
-Inter *newInter(char *code_file, char *debug_dir, Result *global_result) {
-    Inter *global_inter = makeInter(debug_dir);
-    ParserMessage *pm = makeParserMessage(code_file, debug_dir);
+Inter *newInter(char *code_file, char *debug_dir, Result *global_result, int *status) {
+    Inter *global_inter = NULL;
+    ParserMessage *pm = NULL;
+    *status = 0;
+
+    if (access(code_file, R_OK) != 0){
+        *status = 1;
+        return NULL;
+    }
+
+    if (access(debug_dir, R_OK) != 0)
+        debug_dir = NULL;
+
+    global_inter = makeInter(debug_dir);
+    pm = makeParserMessage(code_file, debug_dir);
 
     parserCommandList(pm, global_inter, true, global_inter->statement);
     if (pm->status != success){