Package: IO

IO

nameinstructionbranchcomplexitylinemethod
IO()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
echo(String)
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%
echo(VDMSeq)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
fecho(String, String, Object)
M: 17 C: 26
60%
M: 4 C: 2
33%
M: 3 C: 1
25%
M: 4 C: 8
67%
M: 0 C: 1
100%
fecho(VDMSeq, VDMSeq, Object)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
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%
formatArg(Object)
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
formatList(VDMSeq)
M: 21 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
freadval(String)
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(VDMSeq)
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%
fwriteval(String, Object, Object)
M: 31 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
fwriteval(VDMSeq, Object, Object)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getFile(String)
M: 1 C: 33
97%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 9
90%
M: 0 C: 1
100%
getFile(VDMSeq)
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%
print(Object)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
printf(String, VDMSeq)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
printf(VDMSeq, VDMSeq)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
println(Object)
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
sprintf(String, List)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
sprintf(VDMSeq, VDMSeq)
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%
static {...}
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
writeval(Object)
M: 10 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: * VDM Code Generator Runtime
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.codegen.runtime;
23:
24: import java.io.File;
25: import java.io.FileOutputStream;
26: import java.io.IOException;
27: import java.nio.charset.Charset;
28: import java.util.List;
29:
30:
31: public class IO
32: {
33:
34:         private static File BaseDIr = new File(".").getParentFile();
35:
36:         private static final String NOT_SUPPORTED_MSG = "Operation is currently not supported";
37:
38:         public static <p> boolean writeval(p val)
39:         {
40:
41:                 String text = formatArg(val);
42:
43:                 System.out.print(text);
44:                 System.out.flush();
45:
46:                 return true;
47:         }
48:
49:         public static <p> boolean fwriteval(String filename, p val, Object fdir)
50:         {
51:                 File file = getFile(filename);
52:                 String text = Utils.toString(val);
53:                 String fdirStr = Utils.toString(fdir); // <start>|<append>
54:
55:                 try
56:                 {
57:                         FileOutputStream fos = new FileOutputStream(file, fdirStr.equals("<append>"));
58:
59:                         fos.write(text.getBytes(Charset.defaultCharset().name()));
60:                         fos.close();
61:                 } catch (IOException e)
62:                 {
63:                         return false;
64:                 }
65:
66:                 return true;
67:         }
68:
69:         public static <p> boolean fwriteval(VDMSeq filename, p val, Object fdir)
70:         {
71:                 return fwriteval(SeqUtil.toStr(filename), val, fdir);
72:         }
73:
74:         public static <p> Tuple freadval(String filename)
75:         {
76:
77:                 throw new UnsupportedOperationException(NOT_SUPPORTED_MSG);
78:         }
79:
80:         public static <p> Tuple freadval(VDMSeq filename)
81:         {
82:
83:                 throw new UnsupportedOperationException(NOT_SUPPORTED_MSG);
84:         }
85:
86:         private static File getFile(String fileStr)
87:         {
88:                 String path = fileStr.replace('/', File.separatorChar);
89:                 File file = new File(path);
90:
91:•                if (!file.isAbsolute())
92:                 {
93:                         file = new File(BaseDIr, path);
94:                 }
95:
96:                 /**
97:                  * The VDM IO library requires the parent structure of a file to exist before the file is accessed. However,
98:                  * when a model is code generated to some location the parent folder structure for a file will not exists. To
99:                  * overcome this (and for convenience) the Java code generator runtime library tries to create the folder
100:                  * structure
101:                  */
102:
103:                 file.getAbsoluteFile().getParentFile().mkdirs();
104:
105:•                if (!file.exists())
106:                 {
107:                         try
108:                         {
109:                                 file.createNewFile();
110:                         } catch (IOException e)
111:                         {
112:                         }
113:                 }
114:
115:                 return file;
116:         }
117:
118:         protected static File getFile(VDMSeq fval)
119:         {
120:                 throw new UnsupportedOperationException(NOT_SUPPORTED_MSG);
121:         }
122:
123:         /*
124:          * Unlike the VDMPP/VDM-RT versions, echo, fecho (including the overloaded
125:          * versions) and ferror are declared *static* in the code-generation runtime.
126:          * This ensures that they also work for code-generated VDM-SL models (where
127:          * everything is static)
128:          */
129:         public static boolean echo(String text)
130:         {
131:                 return fecho("[]", text, null);
132:         }
133:
134:         public static boolean echo(VDMSeq text)
135:         {
136:                 return fecho("[]", SeqUtil.toStr(text), null);
137:         }
138:
139:         public static boolean fecho(String filename, String text, Object fdir)
140:         {
141:
142:•                if (filename.equals("[]"))
143:                 {
144:                         System.out.print(text);
145:                         System.out.flush();
146:                 } else
147:                 {
148:                         try
149:                         {
150:                                 File file = getFile(filename);
151:•                                FileOutputStream fos = new FileOutputStream(file, fdir != null
152:•                                                && fdir.getClass().getName().endsWith("quotes.appendQuote"));
153:
154:                                 fos.write(text.getBytes(Charset.defaultCharset().name()));
155:                                 fos.close();
156:                         } catch (IOException e)
157:                         {
158:                                 return false;
159:                         }
160:                 }
161:
162:                 return true;
163:         }
164:
165:         public static boolean fecho(VDMSeq filename, VDMSeq text, Object fdir)
166:         {
167:
168:                 return fecho(filename.toString(), text.toString(), fdir);
169:         }
170:
171:         public static String ferror()
172:         {
173:                 throw new UnsupportedOperationException(NOT_SUPPORTED_MSG);
174:         }
175:
176:         public static void print(Object arg)
177:         {
178:
179:                 System.out.printf("%s", formatArg(arg));
180:                 System.out.flush();
181:         }
182:
183:         public static void println(Object arg)
184:         {
185:
186:                 System.out.printf("%s", formatArg(arg));
187:                 System.out.printf("%s", "\n");
188:                 System.out.flush();
189:
190:         }
191:
192:         public static void printf(String format, VDMSeq args)
193:         {
194:
195:                 System.out.printf(format, formatList(args));
196:                 System.out.flush();
197:         }
198:
199:         public static void printf(VDMSeq seq, VDMSeq args)
200:         {
201:
202:                 System.out.printf(seq.toString(), formatList(args));
203:                 System.out.flush();
204:         }
205:
206:         public static String sprintf(String format, List<Object> args)
207:         {
208:
209:                 return String.format(format, args.toArray());
210:         }
211:
212:         public static VDMSeq sprintf(VDMSeq seq, VDMSeq args)
213:         {
214:
215:                 throw new UnsupportedOperationException("sprintf is only supported for formats of type String");
216:         }
217:
218:         @SuppressWarnings("unchecked")
219:         private static Object[] formatList(VDMSeq args)
220:         {
221:•                for (int i = 0; i < args.size(); i++)
222:                 {
223:                         Object arg = args.get(i);
224:                         args.set(i, formatArg(arg));
225:                 }
226:
227:                 return args.toArray();
228:         }
229:
230:         private static String formatArg(Object arg)
231:         {
232:                 return Utils.toString(arg);
233:         }
234: }