Package: IO

IO

nameinstructionbranchcomplexitylinemethod
IO()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
fecho(Value, Value, Value)
M: 31 C: 24
44%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 9 C: 6
40%
M: 0 C: 1
100%
ferror()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
freadval(Value, Context)
M: 74 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
fwriteval(Value, Value, Value)
M: 39 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
getFile(Value)
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
print(Value)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
printf(Value, Value)
M: 19 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
println(Value)
M: 27 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
sprintf(Value, Value)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
stringOf(Value)
M: 4 C: 46
92%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 1 C: 13
93%
M: 0 C: 1
100%
toString(Value)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
writeval(Value)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (C) 2008, 2009 Fujitsu Services Ltd.
4: *
5: *        Author: Nick Battle
6: *
7: *        This file is part of VDMJ.
8: *
9: *        VDMJ is free software: you can redistribute it and/or modify
10: *        it under the terms of the GNU General Public License as published by
11: *        the Free Software Foundation, either version 3 of the License, or
12: *        (at your option) any later version.
13: *
14: *        VDMJ is distributed in the hope that it will be useful,
15: *        but WITHOUT ANY WARRANTY; without even the implied warranty of
16: *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: *        GNU General Public License for more details.
18: *
19: *        You should have received a copy of the GNU General Public License
20: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21: *
22: ******************************************************************************/
23:
24: // This must be in the default package to work with VDMJ's native delegation.
25:
26: import java.io.File;
27: import java.io.FileOutputStream;
28: import java.io.IOException;
29: import java.io.Serializable;
30:
31: import org.overture.ast.expressions.PExp;
32: import org.overture.ast.lex.Dialect;
33: import org.overture.config.Settings;
34: import org.overture.interpreter.VDMJ;
35: import org.overture.interpreter.messages.Console;
36: import org.overture.interpreter.runtime.Context;
37: import org.overture.interpreter.runtime.Interpreter;
38: import org.overture.interpreter.runtime.ValueException;
39: import org.overture.interpreter.runtime.VdmRuntime;
40: import org.overture.interpreter.values.BooleanValue;
41: import org.overture.interpreter.values.CharacterValue;
42: import org.overture.interpreter.values.NilValue;
43: import org.overture.interpreter.values.SeqValue;
44: import org.overture.interpreter.values.TupleValue;
45: import org.overture.interpreter.values.Value;
46: import org.overture.interpreter.values.ValueList;
47: import org.overture.interpreter.values.VoidValue;
48: import org.overture.parser.lex.LexTokenReader;
49: import org.overture.parser.syntax.ExpressionReader;
50:
51: /**
52: * This class contains the code for native IO operations.
53: */
54:
55: public class IO implements Serializable
56: {
57:         private static final long serialVersionUID = 1L;
58:         private static String lastError = "";
59:
60:         public static Value writeval(Value tval)
61:         {
62:                 String text = tval.toString();
63:
64:                 Console.out.print(text);
65:                 Console.out.flush();
66:
67:                 return new BooleanValue(true);
68:         }
69:
70:         public static Value fwriteval(Value fval, Value tval, Value dval)
71:         {
72:                 File file = getFile(fval);
73:                 String text = tval.toString();// stringOf(tval);
74:                 String fdir = dval.toString(); // <start>|<append>
75:
76:                 try
77:                 {
78:                         FileOutputStream fos = new FileOutputStream(file, fdir.equals("<append>"));
79:
80:                         fos.write(text.getBytes(Console.charset));
81:                         fos.close();
82:                 } catch (IOException e)
83:                 {
84:                         lastError = e.getMessage();
85:                         return new BooleanValue(false);
86:                 }
87:
88:                 return new BooleanValue(true);
89:         }
90:
91:         // Note that this method is not callable via the native interface, since it
92:         // need access to the Context to call any type invariants involved while
93:         // reading the data.
94:
95:         public static Value freadval(Value fval, Context ctxt)
96:         {
97:                 ValueList result = new ValueList();
98:
99:                 try
100:                 {
101:                         File file = getFile(fval);
102:
103:                         LexTokenReader ltr = new LexTokenReader(file, Dialect.VDM_PP, VDMJ.filecharset);
104:                         ExpressionReader reader = new ExpressionReader(ltr);
105:                         reader.setCurrentModule("IO");
106:                         PExp exp = reader.readExpression();
107:
108:                         Interpreter ip = Interpreter.getInstance();
109:                         ip.typeCheck(exp, ip.getGlobalEnvironment());
110:                         result.add(new BooleanValue(true));
111:                         result.add(exp.apply(VdmRuntime.getExpressionEvaluator(), ctxt));
112:                 } catch (Exception e)
113:                 {
114:                         lastError = e.toString();
115:                         result = new ValueList();
116:                         result.add(new BooleanValue(false));
117:                         result.add(new NilValue());
118:                 }
119:
120:                 return new TupleValue(result);
121:         }
122:
123:         /**
124:          * Gets the absolute path the file based on the filename parsed and the working dir of the IDE or the execution dir
125:          * of VDMJ
126:          *
127:          * @param fval
128:          * file name
129:          * @return
130:          */
131:         protected static File getFile(Value fval)
132:         {
133:                 String path = stringOf(fval).replace('/', File.separatorChar);
134:                 File file = new File(path);
135:
136:•                if (!file.isAbsolute())
137:                 {
138:                         file = new File(Settings.baseDir, path);
139:                 }
140:                 return file;
141:         }
142:
143:         public static Value fecho(Value fval, Value tval, Value dval)
144:         {
145:                 String text = stringOf(tval);
146:                 String filename = fval.toString().replace("\"", "");
147:
148:•                if (filename.equals("[]"))
149:                 {
150:                         Console.out.print(text);
151:                         Console.out.flush();
152:                 } else
153:                 {
154:                         String fdir = dval.toString(); // <start>|<append>
155:
156:                         try
157:                         {
158:                                 File file = getFile(fval);
159:                                 FileOutputStream fos = new FileOutputStream(file, fdir.equals("<append>"));
160:
161:                                 fos.write(text.getBytes(Console.charset));
162:                                 fos.close();
163:                         } catch (IOException e)
164:                         {
165:                                 lastError = e.getMessage();
166:                                 return new BooleanValue(false);
167:                         }
168:                 }
169:
170:                 return new BooleanValue(true);
171:         }
172:
173:         public static Value ferror()
174:         {
175:                 return new SeqValue(lastError);
176:         }
177:
178:         // We need this because the toString of the Value converts special
179:         // characters back to their quoted form.
180:
181:         protected static String stringOf(Value val)
182:         {
183:                 StringBuilder s = new StringBuilder();
184:                 val = val.deref();
185:
186:•                if (val instanceof SeqValue)
187:                 {
188:                         SeqValue sv = (SeqValue) val;
189:
190:•                        for (Value v : sv.values)
191:                         {
192:                                 v = v.deref();
193:
194:•                                if (v instanceof CharacterValue)
195:                                 {
196:                                         CharacterValue cv = (CharacterValue) v;
197:                                         s.append(cv.unicode);
198:                                 } else
199:                                 {
200:                                         s.append("?");
201:                                 }
202:                         }
203:
204:                         return s.toString();
205:                 } else
206:                 {
207:                         return val.toString();
208:                 }
209:         }
210:
211:         public static Value print(Value v)
212:         {
213:                 Console.out.printf("%s", stringOf(v));
214:                 Console.out.flush();
215:                 return new VoidValue();
216:         }
217:
218:         public static Value println(Value v)
219:         {
220:                 Console.out.printf("%s", stringOf(v));
221:                 Console.out.printf("%s", "\n");
222:                 Console.out.flush();
223:                 return new VoidValue();
224:         }
225:
226:         public static Value printf(Value fv, Value vs) throws ValueException
227:         {
228:                 String format = stringOf(fv);
229:                 ValueList values = vs.seqValue(null);
230:                 Console.out.printf(format, values.toArray());
231:                 Console.out.flush();
232:                 return new VoidValue();
233:         }
234:         
235:         public static Value sprintf(Value fv, Value vs) throws ValueException
236:         {
237:                 String format = stringOf(fv);
238:                 ValueList values = vs.seqValue(null);
239:                 return new SeqValue(String.format(format, values.toArray()));
240:         }
241:
242:         public static Value toString(Value v)
243:         {
244:                 String text = v.toString().replaceAll("\"", "");
245:                 return new SeqValue(text);
246:         }
247: }