OSDN Git Service

c2wiki:複数ファイルに対応。
[chnosproject/CHNOSProject.git] / CHNOSProject / chn / chnlib03.c
1 //\r
2 //  chnlib03.c\r
3 //  AI003\r
4 //\r
5 //  Created by 西田 耀 on 13/02/09.\r
6 //  Copyright (c) 2013年 Hikaru Nishida. All rights reserved.\r
7 //\r
8 \r
9 //\r
10 //Include headers\r
11 //\r
12 \r
13 #include <stdio.h>\r
14 #include "chnlib.h"\r
15 \r
16 #ifdef CHNLIB_MAKE_GCC_MAC\r
17 #include <unistd.h>\r
18 //+---int chdir(const char *path);  カレントディレクトリの変更\r
19 //+---char *getcwd(char *buf, size_t size); 現在のカレントディレクトリを取得\r
20 #define CHNLIB_PATH_SEPARATOR   '/'\r
21 #endif\r
22 \r
23 \r
24 #ifdef CHNLIB_MAKE_BCC_WIN\r
25 #include <direct.h>\r
26 #define getcwd(a,b)     _getcwd(a,b)\r
27 //+---int _chdir(const char *dirname);\r
28 //+---char *_getcwd(char *buffer, int maxlen);\r
29 #define CHNLIB_PATH_SEPARATOR   '\'\r
30 #endif\r
31 \r
32 \r
33 //\r
34 //Functions\r
35 //\r
36 \r
37 void CHNLIB_Environment_SetCurrentWorkingDirectory(const char apppath[])\r
38 {\r
39     //カレントディレクトリを実行ファイルのディレクトリに変更\r
40     char path[FILENAME_MAX];\r
41     int i, last;\r
42 \r
43     snprintf(path, sizeof(path), "%s", apppath);\r
44     last = 0;\r
45     for(i = 0; i < FILENAME_MAX; i++){\r
46         if(path[i] == '\0'){\r
47             break;\r
48         }\r
49         if(path[i] == '/'){\r
50             last = i;\r
51         }\r
52     }\r
53     path[last + 1] = '\0';\r
54     printf("%s\n", path);\r
55     chdir(path);\r
56     getcwd(path, sizeof(path));\r
57     printf("%s\n", path);\r
58     \r
59     return;\r
60 }\r
61 \r
62 const char *CHNLIB_Environment_GetFilenameFromPath(const char path[])\r
63 {\r
64     //path文字列から、ファイル名を指し示す文字列の先頭部分のポインタを返す。\r
65     int i, p;\r
66     \r
67     if(path == NULL){\r
68         return NULL;\r
69     }\r
70     \r
71     p = 0;\r
72     for(i = 0; path[i] != '\0'; i++){\r
73         if(path[i] == CHNLIB_PATH_SEPARATOR){\r
74             p = i + 1;\r
75         }\r
76     }\r
77     \r
78     return &path[p];\r
79 }\r