private static IO ioForOption(String option, SnippetInfo info) { option = option.TrimStart(); if (String.IsNullOrEmpty(option)) { throw new Exception("No IO destination specified"); } if (option.StartsWith("console", StringComparison.OrdinalIgnoreCase)) { return(frmMyDlg.getIOToConsole()); } else if (option.StartsWith("append", StringComparison.OrdinalIgnoreCase)) { return(new IOAppendCurrentDoc()); } else if (option.StartsWith("insert", StringComparison.OrdinalIgnoreCase)) { return(new IOInsertAtPosition()); } else if (option.StartsWith("new", StringComparison.OrdinalIgnoreCase)) { return(IONewDoc.NewDocFactory()); } else if (option.StartsWith("file", StringComparison.OrdinalIgnoreCase)) { string filename = option.Substring(4, option.Length - 4); if (!Path.IsPathRooted(filename)) { filename = Path.Combine(info.workingDirectory, filename); } //create an IO to that document return(IOFileDoc.FileDocFactory(filename)); } else { //TODO: new IOWriteDoc(); throw new NotImplementedException("silent file"); } }
/// <summary> /// Returns an IO to the file specified in path. /// The file will be opened in the view when it is first written to. If the file doesnt exist it will be created. /// The file will be saved when all references to this IO have been disposed. /// </summary> /// <param name="path"></param> /// <returns></returns> public static IOFileDoc FileDocFactory(string path) { IOFileDoc ret = null; string key = string.Concat("File:", path); lock (IODoc.docs) { if (docs.ContainsKey(key)) { ret = (IOFileDoc)docs[key]; } if (ret == null || !ret.isOpen) { ret = new IOFileDoc(-1, path); ret.isOpen = false; docs[key] = ret; } ret.refs++; return(ret); } }