OSDN Git Service

c2wiki:複数ファイルに対応。
authorhikarupsp <hikarupsp@users.sourceforge.jp>
Mon, 25 Feb 2013 08:41:14 +0000 (17:41 +0900)
committerhikarupsp <hikarupsp@users.sourceforge.jp>
Mon, 25 Feb 2013 08:41:14 +0000 (17:41 +0900)
chnlib:const char *CHNLIB_Environment_GetFilenameFromPath(const char path[])の処理を修正。

CHNOSProject/c2wiki/c2wiki/main.c
CHNOSProject/chn/chnlib.h
CHNOSProject/chn/chnlib03.c

index f4cba72..8de3544 100644 (file)
@@ -7,15 +7,20 @@
 //
 
 #include <stdio.h>
+#include <time.h>
 #include "chnlib.h"
 
 int main(int argc, const char * argv[])
 {
-    FILE *fp;
+    FILE *fp, *outfile;
     CHNLIB_String *line;
     CHNLIB_UIPArray *root, *wordlist;
+    int files;
     int i, i_max;
     int level, recording;
+    time_t now;
+    struct tm *pnow;
+    char s[1024];
     
     CHNLIB_Environment_SetCurrentWorkingDirectory(argv[0]);
     
@@ -24,63 +29,73 @@ int main(int argc, const char * argv[])
         return 0;
     }
     
-    fp = fopen(argv[1], "rb");
-    if(fp == NULL){
-        printf("fopen error. Abort.\n");
-        return 1;
-    }
-    
+    //解析用リストの初期化
     wordlist = CHNLIB_UIPArray_Initialize();
     CHNLIB_UIPArray_AppendLast(&wordlist, 1, CHNLIB_String_Initialize("{"));
     CHNLIB_UIPArray_AppendLast(&wordlist, 2, CHNLIB_String_Initialize("}"));
     CHNLIB_UIPArray_AppendLast(&wordlist, 3, CHNLIB_String_Initialize("//"));
     
-    root = CHNLIB_UIPArray_Initialize();
-    for(;;){
-        line = CHNLIB_ReadLine(fp);
-        if(line == NULL){
-            break;
-        }
-        CHNLIB_UIPArray_GetSeparatedStringByUIPArray(&root, wordlist, line);
-    }
+    outfile = fopen("out.txt", "wb");
     
-    fclose(fp);
+    now = time(NULL);
+    pnow = localtime(&now);
+    strftime(s, sizeof(s), "-%G/%m/%d %T 現在\n", pnow);
+    fputs(s, outfile);
     
-    i_max = CHNLIB_UIPArray_GetNumberOfDatas(root);
-    fp = fopen("out.txt", "wb");
-    
-    recording = False;
-    level = 0;
-    for(i = 0; i < i_max; i++){
-        switch(CHNLIB_UIPArray_GetData32ByIndex(root, i)){
-            case 1: //"{"
-                if(level == 0){
-                    fprintf(fp, "***%s\n", CHNLIB_String_GetReferencePointerOfCString(CHNLIB_UIPArray_GetPointerByIndex(root, i - 1)));
-                    recording = True;
-                } else if(recording){
-                    recording = False;
-                    fprintf(fp, "\n");
-                }
-                level++;
-                break;
-            case 2: //"}"
-                if(recording){
-                    recording = False;
-                    fprintf(fp, "\n");
+    for(files = 1; files < argc; files++){
+        fp = fopen(argv[files], "rb");
+        if(fp != NULL){
+            //ファイルの読み込み
+            fprintf(outfile, "**%s\n", CHNLIB_Environment_GetFilenameFromPath(argv[files]));
+            root = CHNLIB_UIPArray_Initialize();
+            for(;;){
+                line = CHNLIB_ReadLine(fp);
+                if(line == NULL){
+                    break;
                 }
-                level--;
-                break;
-            case 3: //"//"
-                if(recording){
-                    i++;
-                    fprintf(fp, "%s\n", CHNLIB_String_GetReferencePointerOfCString(CHNLIB_UIPArray_GetPointerByIndex(root, i)));
+                CHNLIB_UIPArray_GetSeparatedStringByUIPArray(&root, wordlist, line);
+            }
+            fclose(fp);
+            
+            i_max = CHNLIB_UIPArray_GetNumberOfDatas(root);
+            recording = False;
+            level = 0;
+            for(i = 0; i < i_max; i++){
+                switch(CHNLIB_UIPArray_GetData32ByIndex(root, i)){
+                    case 1: //"{"
+                        if(level == 0){
+                            fprintf(outfile, "***%s\n", CHNLIB_String_GetReferencePointerOfCString(CHNLIB_UIPArray_GetPointerByIndex(root, i - 1)));
+                            recording = True;
+                        } else if(recording){
+                            recording = False;
+                            fprintf(outfile, "\n");
+                        }
+                        level++;
+                        break;
+                    case 2: //"}"
+                        if(recording){
+                            recording = False;
+                            fprintf(outfile, "\n");
+                        }
+                        level--;
+                        break;
+                    case 3: //"//"
+                        if(recording){
+                            i++;
+                            fprintf(outfile, "%s\n", CHNLIB_String_GetReferencePointerOfCString(CHNLIB_UIPArray_GetPointerByIndex(root, i)));
+                        }
+                        break;
                 }
-                break;
+            }
+            
+            CHNLIB_UIPArray_FreeSelectedAll(&root);
+        } else{
+            printf("Can't open file [%s].\n", argv[files]);
         }
     }
-    fflush(fp);
-    fclose(fp);
     
+    fclose(outfile);
+
     return 0;
 }
 
index 27fdf28..3bca6ed 100755 (executable)
@@ -117,6 +117,7 @@ CHNLIB_String *CHNLIB_ReadLine(FILE *fp);
 \r
 //@chnlib03.c\r
 void CHNLIB_Environment_SetCurrentWorkingDirectory(const char apppath[]);\r
+const char *CHNLIB_Environment_GetFilenameFromPath(const char path[]);\r
 \r
 //@chnlib05.c\r
 int CHNLIB_UTF8_GetCharacterType(char c);\r
index 3eb6b5c..1884a6e 100644 (file)
@@ -17,6 +17,7 @@
 #include <unistd.h>\r
 //+---int chdir(const char *path);  カレントディレクトリの変更\r
 //+---char *getcwd(char *buf, size_t size); 現在のカレントディレクトリを取得\r
+#define CHNLIB_PATH_SEPARATOR   '/'\r
 #endif\r
 \r
 \r
@@ -25,6 +26,7 @@
 #define getcwd(a,b)    _getcwd(a,b)\r
 //+---int _chdir(const char *dirname);\r
 //+---char *_getcwd(char *buffer, int maxlen);\r
+#define CHNLIB_PATH_SEPARATOR   '\'\r
 #endif\r
 \r
 \r
 \r
 void CHNLIB_Environment_SetCurrentWorkingDirectory(const char apppath[])\r
 {\r
+    //カレントディレクトリを実行ファイルのディレクトリに変更\r
     char path[FILENAME_MAX];\r
     int i, last;\r
-    \r
-    //カレントディレクトリを実行ファイルのディレクトリに変更\r
+\r
     snprintf(path, sizeof(path), "%s", apppath);\r
     last = 0;\r
     for(i = 0; i < FILENAME_MAX; i++){\r
@@ -56,3 +58,22 @@ void CHNLIB_Environment_SetCurrentWorkingDirectory(const char apppath[])
     \r
     return;\r
 }\r
+\r
+const char *CHNLIB_Environment_GetFilenameFromPath(const char path[])\r
+{\r
+    //path文字列から、ファイル名を指し示す文字列の先頭部分のポインタを返す。\r
+    int i, p;\r
+    \r
+    if(path == NULL){\r
+        return NULL;\r
+    }\r
+    \r
+    p = 0;\r
+    for(i = 0; path[i] != '\0'; i++){\r
+        if(path[i] == CHNLIB_PATH_SEPARATOR){\r
+            p = i + 1;\r
+        }\r
+    }\r
+    \r
+    return &path[p];\r
+}\r