- private class Editor
- {
- private String id;
- private String name;
-
- private File sourceFile;
- private File directory;
-
-
- public Editor(ProblemComponentModel component, Language language, Renderer renderer) throws IOException
- {
- this.id = String.valueOf(component.getProblem().getProblemID());
- this.name = component.getClassName();
-
- File topDir = new File(System.getProperty("user.home"), ".vimcoder");
- if (!topDir.isDirectory())
- {
- if (!topDir.mkdirs()) throw new IOException(topDir.getPath());
- }
-
- this.directory = new File(topDir, String.valueOf(component.getProblem().getProblemID()));
- if (!directory.isDirectory())
- {
- if (!directory.mkdirs()) throw new IOException(directory.getPath());
- }
-
- String lang = language.getName();
- String ext = languageExtension.get(lang);
-
- HashMap<String,String> terms = new HashMap<String,String>();
- terms.put("RETURNTYPE", component.getReturnType().getDescriptor(language));
- terms.put("CLASSNAME", component.getClassName());
- terms.put("METHODNAME", component.getMethodName());
- terms.put("METHODPARAMS", getMethodParams(component.getParamTypes(), component.getParamNames(), language));
- terms.put("METHODPARAMNAMES", join(component.getParamNames(), ", "));
-
- File problemFile = new File(directory, "Problem.html");
- if (!problemFile.canRead())
- {
- FileWriter writer = new FileWriter(problemFile);
- try
- {
- writer.write(renderer.toHTML(language));
- }
- catch (Exception exception)
- {
- }
- writer.close();
- }
-
- sourceFile = new File(directory, terms.get("CLASSNAME") + "." + ext);
- if (!sourceFile.canRead())
- {
- String text = expandTemplate(readResource(lang + "Template"), terms);
- FileWriter writer = new FileWriter(sourceFile);
- writer.write(text);
- writer.close();
- }
-
- File driverFile = new File(directory, "driver" + "." + ext);
- if (!driverFile.canRead())
- {
- StringBuilder testCases = new StringBuilder();
- if (component.hasTestCases())
- {
- HashMap<String,String> testTerms = new HashMap<String,String>();
- testTerms.putAll(terms);
- String template = readResource(lang + "Test");
- for (TestCase testCase : component.getTestCases())
- {
- testTerms.put("TESTOUTPUT", "\"" + quote(testCase.getOutput()) + "\"");
- testTerms.put("TESTINPUTS", join(testCase.getInput(), ", "));
- testCases.append(expandTemplate(template, testTerms));
- }
- }
- terms.put("TESTCASES", testCases.toString());
-
- String text = expandTemplate(readResource(lang + "Driver"), terms);
- FileWriter writer = new FileWriter(driverFile);
- writer.write(text);
- writer.close();
- }
-
- File makeFile = new File(directory, "Makefile");
- {
- String text = expandTemplate(readResource(lang + "Makefile"), terms);
- FileWriter writer = new FileWriter(makeFile);
- writer.write(text);
- writer.close();
- }
- }
-
- public void setSource(String source) throws IOException
- {
- String actualSource = readFile(sourceFile);
- if (!actualSource.equals(source))
- {
- File actualFile = new File(directory, name);
- FileWriter writer = new FileWriter(actualFile);
- writer.write(source);
- writer.close();
- }
- doVimCommand("--remote-tab-silent", sourceFile.getPath());
- doVimCommand("--remote-send", "<C-\\><C-N>:if search('\\$CARAT\\\\$') != 0<CR>normal df$<CR>endif<CR>:redraw<CR>");
- }
-
- public String getSource() throws IOException
- {
- return readFile(sourceFile) + "\n// Edited by " + version + "\n// " + website + "\n\n";
- }
-
- public void setTextEnabled(boolean enable)
- {
- doVimCommand("--remote-send", "<C-\\><C-N>:set readonly<CR>:echo \"The contest is over.\"<CR>");
- }
-
-
- private boolean doVimCommand(String command, String argument)
- {
- String[] arguments = {argument};
- return doVimCommand(command, arguments);
- }
-
- private boolean doVimCommand(String command, String[] arguments)
- {
- try
- {
- String[] exec = {"gvim", "--servername", "VimCoder" + id, command};
- exec = concat(exec, arguments);
-
- Process child = Runtime.getRuntime().exec(exec);
- if (child.waitFor() == 0)
- {
- return true;
- }
- else
- {
- logError("vim command failed");
- }
- }
- catch (IOException exception)
- {
- logError("failed to launch external vim process");
- return false;
- }
- catch (InterruptedException exception)
- {
- logWarning("interrupted while waiting on vim process");
- }
- return false;
- }
-
- private String getMethodParams(DataType[] types, String[] names, Language language)
- {
- StringBuilder text = new StringBuilder();
-
- text.append(types[0].getDescriptor(language) + " " + names[0]);
- for (int i = 1; i < names.length; ++i)
- {
- text.append(", " + types[i].getDescriptor(language) + " " + names[i]);
- }
-
- return text.toString();
- }
-
- private String readFile(File file) throws IOException
- {
- StringBuilder text = new StringBuilder();
-
- BufferedReader reader = new BufferedReader(new FileReader(file.getPath()));
- try
- {
- String line = null;
-
- while ((line = reader.readLine()) != null)
- {
- text.append(line + System.getProperty("line.separator"));
- }
- }
- finally
- {
- reader.close();
- }
-
- return text.toString();
- }
-
- private String readResource(String path) throws IOException
- {
- StringBuilder text = new StringBuilder();
-
- InputStream stream = getClass().getResourceAsStream("resources/" + path);
- if (stream != null)
- {
- try
- {
- byte[] buffer = new byte[4096];
- int numBytes = 0;
- while (0 < (numBytes = stream.read(buffer))) text.append(new String(buffer, 0, numBytes));
- }
- finally
- {
- stream.close();
- }
- }
-
- return text.toString();
- }
-
- private String expandTemplate(String template, Map<String,String> terms)
- {
- String text = template;
- for (String key : terms.keySet())
- {
- text = text.replaceAll("\\$" + key + "\\$", quote(terms.get(key)));
- }
- return text;
- }
- }