1 /**
2  * Copyright: Copyright (c) 2012 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Jun 15, 2012
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module dstep.translator.IncludeHandler;
8 
9 import Path = std.path;
10 
11 import mambo.core._;
12 
13 private IncludeHandler includeHandler_;
14 
15 @property IncludeHandler includeHandler ()
16 {
17     return includeHandler_;
18 }
19 
20 static this ()
21 {
22     includeHandler_ = new IncludeHandler;
23 }
24 
25 class IncludeHandler
26 {
27     private string[] rawIncludes;
28     private string[] imports;
29     static string[string] knownIncludes;
30 
31     static this ()
32     {
33         knownIncludes = [
34             "complex" : "core.stdc.complex",
35             "config" : "core.stdc.config",
36             "ctype" : "core.stdc.ctype",
37             "errno" : "core.stdc.errno",
38             "fenv" : "core.stdc.fenv",
39             "float" : "core.stdc.float",
40             "inttypes" : "core.stdc.inttypes",
41             "limits" : "core.stdc.limits",
42             "locale" : "core.stdc.locale",
43             "math" : "core.stdc.math",
44             "signal" : "core.stdc.signal",
45             "stdarg" : "core.stdc.stdarg",
46             "stddef" : "core.stdc.stddef",
47             "stdint" : "core.stdc.stdint",
48             "stdio" : "core.stdc.stdio",
49             "stdlib" : "core.stdc.stdlib",
50             "string" : "core.stdc.string",
51             "tgmath" : "core.stdc.tgmath",
52             "time" : "core.stdc.time",
53             "wchar" : "core.stdc.wchar_",
54             "wctype" : "core.stdc.wctype",
55 
56             "dirent" : "core.sys.posix.dirent",
57             "dlfcn" : "core.sys.posix.dlfcn",
58             "fcntl" : "core.sys.posix.fcntl",
59             "netdb" : "core.sys.posix.netdb",
60             "poll" : "core.sys.posix.poll",
61             "pthread" : "core.sys.posix.pthread",
62             "pwd" : "core.sys.posix.pwd",
63             "sched" : "core.sys.posix.sched",
64             "semaphore" : "core.sys.posix.semaphore",
65             "setjmp" : "core.sys.posix.setjmp",
66             "signal" : "core.sys.posix.signal",
67             "termios" : "core.sys.posix.termios",
68             "ucontext" : "core.sys.posix.ucontext",
69             "unistd" : "core.sys.posix.unistd",
70             "utime" : "core.sys.posix.utime",
71 
72             "arpa/inet" : "core.sys.posix.arpa.inet",
73 
74             "net/if" : "core.sys.posix.net.if_",
75 
76             "netinet/in" : "core.sys.posix.netinet.in_",
77             "netinet/tcp" : "core.sys.posix.netinet.tcp",
78 
79             "sys/ipc" : "core.sys.posix.sys.ipc",
80             "sys/mman" : "core.sys.posix.sys.mman",
81             "sys/select" : "core.sys.posix.sys.select",
82             "sys/shm" : "core.sys.posix.sys.shm",
83             "sys/socket" : "core.sys.posix.sys.socket",
84             "sys/stat" : "core.sys.posix.sys.stat",
85             "sys/time" : "core.sys.posix.sys.time",
86             "sys/types" : "core.sys.posix.sys.types",
87             "sys/_types" : "core.sys.posix.sys.types",
88             "sys/uio" : "core.sys.posix.sys.uio",
89             "sys/un" : "core.sys.posix.sys.un",
90             "sys/utsname" : "core.sys.posix.sys.utsname",
91             "sys/wait" : "core.sys.posix.sys.wait",
92 
93             "windows" : "core.sys.windows.windows"
94         ];
95     }
96 
97     void addInclude (string include)
98     {
99         rawIncludes ~= include;
100     }
101 
102     void addImport (string imp)
103     {
104         imports ~= imp;
105     }
106 
107     void addCompatible ()
108     {
109         imports ~= "core.stdc.config";
110     }
111 
112     string[] toImports ()
113     {
114         auto r =  rawIncludes.map!((e) {
115             if (auto i = isKnownInclude(e))
116                 return toImport(i);
117 
118             else
119                 return "";
120         });
121 
122         auto imps = imports.map!(e => toImport(e));
123 
124         return r.append(imps).filter!(e => e.any).unique.toArray;
125     }
126 
127 private:
128 
129     string toImport (string str)
130     {
131         return "import " ~ str ~ ";";
132     }
133 
134     string isKnownInclude (string include)
135     {
136         include = Path.stripExtension(include);
137 
138         if (auto r = knownIncludes.find!((k, _) => include.endsWith(k)))
139             return r.value;
140 
141         return null;
142     }
143 }