X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fvimcoder;a=blobdiff_plain;f=src%2Fcom%2Fdogcows%2FEditor.java;h=87403240fa43c7de09fcd36b3035729dabadb312;hp=2f2896248113e30da45d75d3d68995bc04141389;hb=482e4985ec29af24fd5dfaae71a107444641287f;hpb=28c8fae03b2294d6486233cd62f4d4d9d11603ca diff --git a/src/com/dogcows/Editor.java b/src/com/dogcows/Editor.java index 2f28962..8740324 100644 --- a/src/com/dogcows/Editor.java +++ b/src/com/dogcows/Editor.java @@ -1,12 +1,9 @@ package com.dogcows; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import java.io.InputStream; import java.util.*; import com.topcoder.client.contestant.ProblemComponentModel; @@ -27,22 +24,32 @@ public class Editor /** * The problem ID number. */ - private String id; + private String id; /** * The name of the class. */ - private String name; + private String name; + + /** + * The name of the contest. + */ + private String contestName; + + /** + * The point value. + */ + private String points; /** * The path of the current source file. */ - private File sourceFile; + private File sourceFile; /** * The path of the problem directory. */ - private File directory; + private File directory; /** @@ -51,10 +58,10 @@ public class Editor private static final Map languageExtension = new HashMap(); static { - languageExtension.put("Java", "java"); - languageExtension.put("C++", "cc"); - languageExtension.put("C#", "cs"); - languageExtension.put("VB", "vb"); + languageExtension.put("Java", "java"); + languageExtension.put("C++", "cc"); + languageExtension.put("C#", "cs"); + languageExtension.put("VB", "vb"); languageExtension.put("Python", "py"); } @@ -67,11 +74,12 @@ public class Editor * @throws Exception If the editor could not set itself up. */ public Editor(ProblemComponentModel component, - Language language, - Renderer renderer) throws Exception + Language language, Renderer renderer) throws Exception { this.id = String.valueOf(component.getProblem().getProblemID()); this.name = component.getClassName(); + this.contestName = component.getProblem().getRound().getContestName().replaceAll(" ", "-"); + this.points = String.valueOf(component.getPoints().intValue()); // Make sure the top-level vimcoder directory exists. File topDir = VimCoder.getStorageDirectory(); @@ -81,29 +89,43 @@ public class Editor } // Make sure the problem directory exists. - this.directory = new File(topDir, id); - if (!directory.isDirectory()) + File newStyleDirectory = new File(new File(topDir, contestName), points); + File oldStyleDirectory = new File(topDir, id); + if (newStyleDirectory.isDirectory()) + { + this.directory = newStyleDirectory; + } + else if (oldStyleDirectory.isDirectory()) + { + this.directory = oldStyleDirectory; + } + else if (VimCoder.isContestDirNames()) + { + this.directory = newStyleDirectory; + if (!directory.mkdirs()) throw new IOException(directory.getPath()); + } + else { + this.directory = oldStyleDirectory; if (!directory.mkdirs()) throw new IOException(directory.getPath()); } String lang = language.getName(); - String ext = languageExtension.get(lang); + String ext = languageExtension.get(lang); // Set up the terms used for the template expansion. HashMap terms = new HashMap(); - terms.put("RETURNTYPE", component.getReturnType().getDescriptor(language).replaceAll("\\s+", "")); - terms.put("CLASSNAME", name); - terms.put("METHODNAME", component.getMethodName()); - terms.put("METHODPARAMS", getMethodParams(component.getParamTypes(), - component.getParamNames(), - language)); + terms.put("RETURNTYPE", component.getReturnType().getDescriptor(language)); + terms.put("CLASSNAME", name); + terms.put("METHODNAME", component.getMethodName()); + terms.put("METHODPARAMS", getMethodParams(component.getParamTypes(), + component.getParamNames(), language)); terms.put("METHODPARAMNAMES", Util.join(component.getParamNames(), ", ")); terms.put("METHODPARAMSTREAMIN", Util.join(component.getParamNames(), " >> ")); - terms.put("METHODPARAMSTREAMOUT", Util.join(component.getParamNames(), " << ")); + terms.put("METHODPARAMSTREAMOUT", Util.join(component.getParamNames(), " << \", \" << ")); terms.put("METHODPARAMDECLARES", getMethodParamDeclarations(component.getParamTypes(), - component.getParamNames(), - language)); + component.getParamNames(), language)); + terms.put("VIMCODER", VimCoder.version); // Write the problem statement as an HTML file in the problem directory. File problemFile = new File(directory, "Problem.html"); @@ -122,11 +144,10 @@ public class Editor // Expand the template for the main class and write it to the current // source file. - sourceFile = new File(directory, name + "." + ext); + this.sourceFile = new File(directory, name + "." + ext); if (!sourceFile.canRead()) { - String text = Util.expandTemplate(readTemplate(lang + "Template"), - terms); + String text = Util.expandTemplate(readTemplate(lang + "Template"), terms); FileWriter writer = new FileWriter(sourceFile); writer.write(text); writer.close(); @@ -136,8 +157,7 @@ public class Editor File driverFile = new File(directory, "driver." + ext); if (!driverFile.canRead()) { - String text = Util.expandTemplate(readTemplate(lang + "Driver"), - terms); + String text = Util.expandTemplate(readTemplate(lang + "Driver"), terms); FileWriter writer = new FileWriter(driverFile); writer.write(text); writer.close(); @@ -167,9 +187,9 @@ public class Editor // Finally, expand the Makefile template and write it. File makeFile = new File(directory, "Makefile"); + if (!makeFile.canRead()) { - String text = Util.expandTemplate(readTemplate(lang + "Makefile"), - terms); + String text = Util.expandTemplate(readTemplate(lang + "Makefile"), terms); FileWriter writer = new FileWriter(makeFile); writer.write(text); writer.close(); @@ -198,8 +218,7 @@ public class Editor */ public String getSource() throws IOException { - return Util.readFile(sourceFile) + "\n// Edited by " + - VimCoder.version + "\n// " + VimCoder.website + "\n\n"; + return Util.readFile(sourceFile); } @@ -211,8 +230,7 @@ public class Editor * @param argument A single argument for the remote command. * @throws Exception If the command could not be sent. */ - private void sendVimCommand(String command, - String argument) throws Exception + private void sendVimCommand(String command, String argument) throws Exception { String[] arguments = {argument}; sendVimCommand(command, arguments); @@ -226,8 +244,7 @@ public class Editor * @param argument Arguments for the remote command. * @throws Exception If the command could not be sent. */ - private void sendVimCommand(String command, - String[] arguments) throws Exception + private void sendVimCommand(String command, String[] arguments) throws Exception { String[] vimCommand = VimCoder.getVimCommand().split("\\s"); String[] flags = {"--servername", "VimCoder" + id, command}; @@ -246,7 +263,7 @@ public class Editor * before the timeout, we will assume it is not backgrounding and * that everything worked. This works as long as the Vim server is * able to start within the stall period. */ - long expire = System.currentTimeMillis() + 250; + long expire = System.currentTimeMillis() + 2500; while (System.currentTimeMillis() < expire) { Thread.yield(); @@ -298,7 +315,7 @@ public class Editor String[] strings = new String[types.length]; for (int i = 0; i < types.length; ++i) { - strings[i] = types[i].getDescriptor(language).replaceAll("\\s+", ""); + strings[i] = types[i].getDescriptor(language); } return strings; } @@ -313,9 +330,7 @@ public class Editor * @param language The language used for representing the data types. * @return The list of parameters. */ - private String getMethodParams(DataType[] types, - String[] names, - Language language) + private String getMethodParams(DataType[] types, String[] names, Language language) { String[] typeStrings = getStringTypes(types, language); return Util.join(Util.combine(typeStrings, names, " "), ", "); @@ -331,9 +346,7 @@ public class Editor * @param language The language used for representing the data types. * @return The parameters as a block of declarations. */ - private String getMethodParamDeclarations(DataType[] types, - String[] names, - Language language) + private String getMethodParamDeclarations(DataType[] types, String[] names, Language language) { final String end = ";" + System.getProperty("line.separator"); String[] typeStrings = getStringTypes(types, language); @@ -341,3 +354,4 @@ public class Editor } } +// vim:noet:ts=8