Procházet zdrojové kódy

feat: 添加本地化设置参数

link #7
SongZihuan před 4 roky
rodič
revize
556c3c8528
5 změnil soubory, kde provedl 14 přidání a 6 odebrání
  1. 9 2
      argument/argument.c
  2. 1 0
      include/arguement.h
  3. 1 1
      main.c
  4. 1 1
      vmcore/include/virtualmath.h
  5. 2 2
      vmcore/src/virtualmath.c

+ 9 - 2
argument/argument.c

@@ -10,12 +10,13 @@ static const struct option long_option[]={
         {"stdin",required_argument,NULL,'i'},
         {"not-run-cl",no_argument,NULL,'n'},
         {"print-clock",no_argument,NULL,'p'},
+        {"locale",optional_argument,NULL,'l'},  // 可选参数
         {NULL,0,NULL,0}
 };
 
-static const char *short_option = "o:e:i:np";
+static const char *short_option = "o:e:i:npl::";  // l::可选参数
 
-Args args = {.out_file=NULL, .error_file=NULL, .in_file=NULL, .run_commandLine=true, .p_clock=false};
+Args args = {.out_file=NULL, .error_file=NULL, .in_file=NULL, .run_commandLine=true, .p_clock=false, .locale=""};
 
 /**
  * 参数设置, args是全局结构体, 保存全局的参数设置
@@ -46,6 +47,12 @@ int getArgs(const int argc, char **argv)
                 args.run_commandLine = false;
                 break;
             case 'p':
+                if (optarg != NULL)
+                    args.locale = optarg;  // 不需要复制
+                else
+                    args.locale = "";
+                break;
+            case 'l':
                 args.p_clock = true;
                 break;
             case '?':

+ 1 - 0
include/arguement.h

@@ -9,6 +9,7 @@ struct Args{
     char *in_file;
     bool run_commandLine;
     bool p_clock;
+    char *locale;
 };
 extern struct Args args;
 typedef struct Args Args;

+ 1 - 1
main.c

@@ -20,7 +20,7 @@ int main(int argc, char *argv[]) {
     if (getArgs(argc, argv) == -1)  // 命令行参数设定
         return 2;
 
-    initVirtualMath();
+    initVirtualMath(args.locale);
     inter = makeInter(args.out_file, args.error_file, args.in_file, NULL);
     runCodeFile(inter, argv + optind);  // 从文件中运行代码
     if (args.run_commandLine)

+ 1 - 1
vmcore/include/virtualmath.h

@@ -14,6 +14,6 @@
 #include "inter.h"
 #include "run.h"
 
-bool initVirtualMath();
+bool initVirtualMath(char const *locale);
 
 #endif //VIRTUALMATH_VIRTUALMATH_H

+ 2 - 2
vmcore/src/virtualmath.c

@@ -1,6 +1,6 @@
 #include "virtualmath.h"
 
-bool initVirtualMath() {
-    setlocale(LC_ALL, "zh_CN.UTF-8");
+bool initVirtualMath(char const *local) {
+    setlocale(LC_ALL, local);
     return true;
 }