Package: CSV$1

CSV$1

nameinstructionbranchcomplexitylinemethod
createValue(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%
{...}
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%

Coverage

1: import java.io.BufferedReader;
2: import java.io.File;
3: import java.io.FileOutputStream;
4: import java.io.FileReader;
5: import java.io.IOException;
6: import java.io.Serializable;
7:
8: import org.overture.ast.expressions.PExp;
9: import org.overture.ast.lex.Dialect;
10: import org.overture.interpreter.messages.Console;
11: import org.overture.interpreter.runtime.Context;
12: import org.overture.interpreter.runtime.Interpreter;
13: import org.overture.interpreter.runtime.VdmRuntime;
14: import org.overture.interpreter.utilities.stdlibs.CsvParser;
15: import org.overture.interpreter.utilities.stdlibs.CsvResult;
16: import org.overture.interpreter.utilities.stdlibs.CsvValueBuilder;
17: import org.overture.interpreter.values.BooleanValue;
18: import org.overture.interpreter.values.CPUValue;
19: import org.overture.interpreter.values.IntegerValue;
20: import org.overture.interpreter.values.NilValue;
21: import org.overture.interpreter.values.SeqValue;
22: import org.overture.interpreter.values.TupleValue;
23: import org.overture.interpreter.values.Value;
24: import org.overture.interpreter.values.ValueList;
25: import org.overture.parser.lex.LexTokenReader;
26: import org.overture.parser.syntax.ExpressionReader;
27:
28: /**
29: * Basic CSV file support for VDM
30: *
31: * @author kela
32: */
33: public class CSV extends IO implements Serializable
34: {
35:         private static final long serialVersionUID = 1L;
36:         private static String lastError = "";
37:
38:         public static Value ferror()
39:         {
40:                 return new SeqValue(lastError);
41:         }
42:
43:         /**
44:          * Writes a seq of ? in a CSV format to the file specified
45:          *
46:          * @param fval
47:          * the filename
48:          * @param tval
49:          * the sequence to write
50:          * @param dval
51:          * append to or start a new file
52:          * @return
53:          */
54:         public static Value fwriteval(Value fval, Value tval, Value dval)
55:         {
56:                 File file = getFile(fval);
57:                 String fdir = dval.toString(); // <start>|<append>
58:                 StringBuffer text = new StringBuffer();
59:                 if (tval instanceof SeqValue)
60:                 {
61:                         for (Value val : ((SeqValue) tval).values)
62:                         {
63:                                 text.append(val.toString());
64:                                 text.append(",");
65:                         }
66:
67:                 }
68:
69:                 if (text.length() > 0 && text.charAt(text.length() - 1) == ',')
70:                 {
71:                         text.deleteCharAt(text.length() - 1);
72:                 }
73:                 text.append('\n');
74:
75:                 try
76:                 {
77:                         FileOutputStream fos = new FileOutputStream(file, fdir.equals("<append>"));
78:
79:                         fos.write(text.toString().getBytes(Console.charset));
80:                         fos.close();
81:                 } catch (IOException e)
82:                 {
83:                         lastError = e.getMessage();
84:                         return new BooleanValue(false);
85:                 }
86:
87:                 return new BooleanValue(true);
88:         }
89:
90:         /**
91:          * Read a CSV live as a seq of ? in VDM
92:          *
93:          * @param fval
94:          * name of the file to read from
95:          * @param indexVal
96:          * the line index
97:          * @return true + seq of ? or false and nil
98:          */
99:         public static Value freadval(Value fval, Value indexVal)
100:         {
101:                 ValueList result = new ValueList();
102:
103:                 try
104:                 {
105:                         File file = getFile(fval);
106:                         long index = indexVal.intValue(null);
107:                         SeqValue lineCells = new SeqValue();
108:
109:                         boolean success = false;
110:                         try
111:                         {
112:                                 CsvParser parser = new CsvParser(new CsvValueBuilder()
113:                                 {
114:                                         @Override
115:                                         public Value createValue(String value)
116:                                                         throws Exception
117:                                         {
118:                                                 return CSV.createValue("CSV", "freadval", value);
119:                                         }
120:                                 });
121:                                 
122:                                 CsvResult res = parser.parseValues(getLine(file, index));
123:                                 
124:                                 if(!res.dataOk())
125:                                 {
126:                                         lastError = res.getErrorMsg();
127:                                         success = false;
128:                                 }
129:                                 else
130:                                 {
131:                                         lineCells.values.addAll(res.getValues());
132:                                         success = true;
133:                                 }
134:                         } catch (Exception e)
135:                         {
136:                                 success = false;
137:                                 lastError = e.getMessage();
138:                                 // OK
139:                         }
140:
141:                         result.add(new BooleanValue(success));
142:                         result.add(lineCells);
143:                 } catch (Exception e)
144:                 {
145:                         lastError = e.toString();
146:                         result = new ValueList();
147:                         result.add(new BooleanValue(false));
148:                         result.add(new NilValue());
149:                 }
150:
151:                 return new TupleValue(result);
152:         }
153:
154:         /**
155:          * Gets the line count of the CSV file
156:          *
157:          * @param fval
158:          * name of the file
159:          * @return int value with count
160:          */
161:         public static Value flinecount(Value fval)
162:         {
163:                 ValueList result = new ValueList();
164:
165:                 try
166:                 {
167:                         File file = getFile(fval);
168:                         long count = getLineCount(file);
169:
170:                         result.add(new BooleanValue(true));
171:                         result.add(new IntegerValue(count));
172:                 } catch (Exception e)
173:                 {
174:                         lastError = e.toString();
175:                         result = new ValueList();
176:                         result.add(new BooleanValue(false));
177:                         result.add(new NilValue());
178:                 }
179:
180:                 return new TupleValue(result);
181:         }
182:
183:         private static int getLineCount(File file) throws IOException
184:         {
185:                 BufferedReader bufRdr = new BufferedReader(new FileReader(file));
186:                 int lines = 0;
187:                 try
188:                 {
189:                         while (bufRdr.readLine() != null)
190:                         {
191:                                 lines++;
192:                         }
193:                 } finally
194:                 {
195:                         bufRdr.close();
196:                 }
197:                 return lines;
198:         }
199:
200:         private static String getLine(File file, long index)
201:                         throws IOException
202:         {
203:                 BufferedReader bufRdr = new BufferedReader(new FileReader(file));
204:                 String line = null;
205:                 int lineIndex = 0;
206:
207:                 if (index < 1)
208:                 {
209:                         bufRdr.close();
210:                         throw new IOException("CSV line index before first entry");
211:                 }
212:
213:                 try
214:                 {
215:                         while ((line = bufRdr.readLine()) != null)
216:                         {
217:                                 lineIndex++;
218:                                 if (lineIndex == index)
219:                                 {
220:                                         break;
221:                                 }
222:
223:                         }
224:                 } finally
225:                 {
226:                         bufRdr.close();
227:                 }
228:
229:                 if (line == null)
230:                 {
231:                         throw new IOException("CSV no data read. Empty line.");
232:                 }
233:
234:                 return line;
235:         }
236:
237:         private static Value createValue(String module, String method, String value)
238:                         throws Exception
239:         {
240:                 LexTokenReader ltr = new LexTokenReader(value, Dialect.VDM_PP);
241:                 ExpressionReader reader = new ExpressionReader(ltr);
242:                 reader.setCurrentModule(module);
243:                 PExp exp = reader.readExpression();
244:
245:                 Interpreter ip = Interpreter.getInstance();
246:                 ip.typeCheck(exp, ip.getGlobalEnvironment());
247:
248:                 Context ectxt = new Context(ip.getAssistantFactory(), null, method, null);
249:                 ectxt.setThreadState(null, CPUValue.vCPU);
250:                 return exp.apply(VdmRuntime.getExpressionEvaluator(), ectxt);
251:         }
252: }