72
|
1 module dmd.FileExp;
|
|
2
|
114
|
3 import dmd.common;
|
72
|
4 import dmd.Expression;
|
163
|
5 import dmd.File;
|
72
|
6 import dmd.UnaExp;
|
163
|
7 import dmd.StringExp;
|
|
8 import dmd.WANT;
|
|
9 import dmd.Global;
|
|
10 import dmd.FileName;
|
72
|
11 import dmd.OutBuffer;
|
|
12 import dmd.Loc;
|
|
13 import dmd.Scope;
|
|
14 import dmd.HdrGenState;
|
|
15 import dmd.TOK;
|
|
16
|
163
|
17 import core.stdc.stdio;
|
|
18
|
0
|
19 class FileExp : UnaExp
|
|
20 {
|
|
21 this(Loc loc, Expression e)
|
|
22 {
|
178
|
23 register();
|
162
|
24 super(loc, TOKmixin, FileExp.sizeof, e);
|
0
|
25 }
|
|
26
|
72
|
27 override Expression semantic(Scope sc)
|
0
|
28 {
|
163
|
29 StringExp se;
|
|
30
|
|
31 version (LOGSEMANTIC) {
|
|
32 printf("FileExp.semantic('%.*s')\n", toChars());
|
|
33 }
|
|
34 UnaExp.semantic(sc);
|
|
35 e1 = resolveProperties(sc, e1);
|
|
36 e1 = e1.optimize(WANTvalue);
|
|
37 if (e1.op != TOKstring)
|
|
38 {
|
|
39 error("file name argument must be a string, not (%s)", e1.toChars());
|
|
40 goto Lerror;
|
|
41 }
|
|
42 se = cast(StringExp)e1;
|
|
43 se = se.toUTF8(sc);
|
|
44
|
|
45 string name = (cast(immutable(char)*)se.string_)[0..se.len];
|
|
46
|
|
47 if (!global.params.fileImppath)
|
|
48 {
|
|
49 error("need -Jpath switch to import text file %s", name);
|
|
50 goto Lerror;
|
|
51 }
|
|
52
|
|
53 if (name != FileName.name(name))
|
|
54 {
|
|
55 error("use -Jpath switch to provide path for filename %s", name);
|
|
56 goto Lerror;
|
|
57 }
|
|
58
|
|
59 name = FileName.searchPath(global.filePath, name, 0);
|
|
60 if (!name)
|
|
61 {
|
|
62 error("file %s cannot be found, check -Jpath", se.toChars());
|
|
63 goto Lerror;
|
|
64 }
|
|
65
|
|
66 if (global.params.verbose)
|
|
67 printf("file %s\t(%s)\n", cast(char*)se.string_, name);
|
|
68
|
|
69 {
|
|
70 scope File f = new File(name);
|
|
71 if (f.read())
|
|
72 {
|
|
73 error("cannot read file %s", f.toChars());
|
|
74 goto Lerror;
|
|
75 }
|
|
76 else
|
|
77 {
|
|
78 f.ref_ = 1;
|
|
79 se = new StringExp(loc, (cast(immutable(char)*)f.buffer)[0..f.len]);
|
|
80 }
|
|
81 }
|
|
82 Lret:
|
|
83 return se.semantic(sc);
|
|
84
|
|
85 Lerror:
|
|
86 se = new StringExp(loc, "");
|
|
87 goto Lret;
|
0
|
88 }
|
|
89
|
72
|
90 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
91 {
|
|
92 assert(false);
|
|
93 }
|
|
94 }
|
|
95
|