public SaveResult AddFile(out ProjectFile file, string filePath) { string fn = Path.GetFileName(filePath); string ext = Path.GetExtension(fn).ToLowerInvariant(); if (project.FileList.TryGetValue(fn.ToLowerInvariant(), out file)) { if (file.FileAbsPath != filePath && file.Exists) { // name conflict, do not allow MessageBox.Show("Error, Cannot Add File " + file.FileName + " Due To Name Conflict"); return(SaveResult.Failed); } else { // added file already in list, maybe it was missing, so refresh the list to update icons PopulateList(); return(SaveResult.Cancelled); } } else { if (ext == ".c" || ext == ".cpp" || ext == ".cxx" || ext == ".s" || ext == ".h" || ext == ".hpp") { // check for space if it's a source or header file, we don't care about the other files if (fn.Contains(" ")) { MessageBox.Show("Error, File Name May Not Contain Spaces"); return(SaveResult.Failed); } } file = new ProjectFile(filePath, this.project); if (file.Exists == false) { try { StreamWriter newFile = new StreamWriter(file.FileAbsPath); if (file.FileExt == "h" || file.FileExt == "hpp") { newFile.WriteLine(FileTemplate.CreateFile(file.FileName, project.FileNameNoExt, "defaultheader.txt")); } else if (file.FileExt == "c" || file.FileExt == "cpp") { newFile.WriteLine(FileTemplate.CreateFile(file.FileName, project.FileNameNoExt, "defaultcode.txt")); } else { newFile.WriteLine(FileTemplate.CreateFile(file.FileName, project.FileNameNoExt, "default_" + file.FileExt + ".txt")); } newFile.Close(); } catch (Exception ex) { ErrorReportWindow.Show(ex, "Error Creating New File " + file.FileName); } } project.FileList.Add(fn.ToLowerInvariant(), file); if (project.Save() == SaveResult.Failed) { MessageBox.Show("Error saving project"); } PopulateList(); return(SaveResult.Successful); } }
private void btnCreate_Click(object sender, EventArgs e) { string f = txtFileName.Text.Trim(); if (string.IsNullOrEmpty(f)) { MessageBox.Show("File name must not be blank"); return; } string fileName; if (f.EndsWith(".")) { fileName = f + this.SelectedExtension.TrimStart('.'); } else if (f.Contains(".")) { fileName = f; } else { fileName = f + "." + this.SelectedExtension.TrimStart('.'); } if ( fileName.Contains(" ") || fileName.Contains("\t") || fileName.Contains("\r") || fileName.Contains("\n") || fileName.Contains("\0") || fileName.Contains("\\") || fileName.Contains("/") ) { MessageBox.Show("Invalid Character in File Name"); return; } foreach (char c in Path.GetInvalidFileNameChars()) { if (fileName.Contains(char.ToString(c))) { MessageBox.Show("Invalid Character '" + c + "' in File Name"); return; } } string fileAbsPath = txtDirLoc.Text + fileName; foreach (char c in Path.GetInvalidPathChars()) { if (fileAbsPath.Contains(char.ToString(c))) { MessageBox.Show("Invalid Character '" + c + "' in File Path"); return; } } string fileNameL = fileName.ToLowerInvariant(); if (project.FileList.ContainsKey(fileNameL)) { if (project.FileList[fileNameL].Exists) { MessageBox.Show("File already exists in the project"); return; } } else { project.FileList.Add(fileNameL, new ProjectFile(fileAbsPath, project)); } if (project.FileList[fileNameL].Exists == false) { if (((string)dropTemplates.Items[dropTemplates.SelectedIndex]) != "none") { try { File.WriteAllText(fileAbsPath, FileTemplate.CreateFile(fileName, project.FileNameNoExt, ((string)dropTemplates.Items[dropTemplates.SelectedIndex]) + ".txt")); } catch (Exception ex) { MessageBox.Show("Error writing file from template, " + ex.Message); } } else { try { File.WriteAllText(fileAbsPath, ""); } catch (Exception ex) { MessageBox.Show("Error creating file, " + ex.Message); } } } createdFile = project.FileList[fileNameL]; if (project.FileList.Count == 1) { project.FileList[fileNameL].IsOpen = true; } if (project.Save() == SaveResult.Failed) { MessageBox.Show("Error saving project"); } SettingsManagement.LastFileExt = createdFile.FileExt; this.DialogResult = DialogResult.OK; this.Close(); }
private void btnCreate_Click(object sender, EventArgs e) { string iniFilename = txtInitialFilename.Text.Trim(); bool hasIniFile = !string.IsNullOrEmpty(iniFilename); hasIniFile = false; string projFilename = txtProjName.Text.Trim(); if (string.IsNullOrEmpty(projFilename)) { MessageBox.Show("The project name can't be blank"); return; } char[] forbidChars = Path.GetInvalidFileNameChars(); foreach (char c in forbidChars) { if (hasIniFile && iniFilename.Contains(c)) { MessageBox.Show("Illegal Character in Initial File Name"); return; } if (projFilename.Contains(c)) { MessageBox.Show("Illegal Character in Project File Name"); return; } } if (hasIniFile) { if (iniFilename.Contains('/') || iniFilename.Contains('\\') || iniFilename.Contains(Path.DirectorySeparatorChar) || iniFilename.Contains(Path.AltDirectorySeparatorChar)) { MessageBox.Show("Illegal Character in Initial File Name"); return; } if (iniFilename.Contains('.') || iniFilename.Contains(' ') || iniFilename.Contains('\t')) { MessageBox.Show("No Spaces or Dots are Allowed in Initial File Name"); return; } } if (projFilename.Contains('/') || projFilename.Contains('\\') || projFilename.Contains(Path.DirectorySeparatorChar) || projFilename.Contains(Path.AltDirectorySeparatorChar)) { MessageBox.Show("Illegal Character in Project File Name"); return; } if (projFilename.Contains('.')) { MessageBox.Show("No Dots are Allowed in Project File Name"); return; } string folderPath = Program.CleanFilePath(txtFolderPath.Text); if (Program.MakeSurePathExists(folderPath) == false) //if (Directory.Exists(folderPath)) { MessageBox.Show("Error Creating Folder"); //MessageBox.Show("Error: Folder Invalid"); return; } string projFilePath = folderPath + Path.DirectorySeparatorChar + projFilename + ".avrproj"; project.FilePath = projFilePath; string ext = "c"; if (((string)dropFileType.Items[dropFileType.SelectedIndex]).Contains("C++")) { ext = "cpp"; } else if (((string)dropFileType.Items[dropFileType.SelectedIndex]).Contains("C")) { ext = "c"; } else if (((string)dropFileType.Items[dropFileType.SelectedIndex]).Contains("Arduino")) { ext = "pde"; } string iniFilePath = folderPath + Path.DirectorySeparatorChar + iniFilename + "." + ext; if (File.Exists(projFilePath)) { if (MessageBox.Show("Project File Already Exists at the Location, Overwrite it?", "Overwrite?", MessageBoxButtons.YesNo) == DialogResult.No) { return; } } bool merge = false; if (hasIniFile && File.Exists(iniFilePath)) { if (MessageBox.Show("Initial File Already Exists at the Location, Merge it with your project?", "Merge File?", MessageBoxButtons.YesNo) == DialogResult.Yes) { merge = true; } else if (MessageBox.Show("Maybe you'd rather overwrite it with a blank file?", "Overwrite File?", MessageBoxButtons.YesNo) == DialogResult.No) { return; } } if (hasIniFile) { if (merge == false) { try { StreamWriter writer = new StreamWriter(iniFilePath); if (ext == "pde") { writer.Write(FileTemplate.CreateFile(iniFilename + "." + ext, projFilename, "initialpde.txt")); } else { writer.Write(FileTemplate.CreateFile(iniFilename + "." + ext, projFilename, "initialmain.txt")); } writer.WriteLine(); writer.Close(); } catch (Exception ex) { ErrorReportWindow.Show(ex, "Error while creating initial file"); } } ProjectFile newFile = new ProjectFile(iniFilePath, project); newFile.IsOpen = true; project.FileList.Add(newFile.FileName.ToLowerInvariant(), newFile); } ProjTemplate.ApplyTemplate((string)dropTemplates.Items[dropTemplates.SelectedIndex], project); project.FilePath = projFilePath; FileAddWizard faw = new FileAddWizard(project); faw.ShowDialog(); if (project.Save(projFilePath) == false) { MessageBox.Show("Error While Saving Project"); } else { if (project.Open(projFilePath) == false) { MessageBox.Show("Error While Opening Project"); } } this.DialogResult = DialogResult.OK; this.Close(); }
static public bool ApplyTemplate(string name, AVRProject proj) { int appCnt = 0; XmlElement template; if (templates.TryGetValue(name, out template)) { XmlElement docx = (XmlElement)template.CloneNode(true); List <string> alreadyInherited = new List <string>(); alreadyInherited.Add(name); bool foundNew; do { foundNew = false; string inheritedInnerXml = ""; foreach (XmlElement param in docx.GetElementsByTagName("Inherit")) { string inheritName = param.InnerText.Trim(); if (alreadyInherited.Contains(inheritName) == false) { XmlElement inherited; if (templates.TryGetValue(inheritName, out inherited)) { inheritedInnerXml += inherited.InnerXml; alreadyInherited.Add(inheritName); foundNew = true; } } } docx.InnerXml += inheritedInnerXml; } while (foundNew); AVRProject.LoadTemplateCommonProperties(ref appCnt, docx, proj); if (appCnt >= 2) { proj.HasBeenConfigged = true; } try { foreach (XmlElement i in docx.GetElementsByTagName("CreateFile")) { string fname = i.GetAttribute("name"); if (string.IsNullOrEmpty(fname) == false) { try { ProjectFile f = new ProjectFile(Program.AbsPathFromRel(proj.DirPath, fname), proj); if (proj.FileList.ContainsKey(fname.ToLowerInvariant()) == false) { proj.FileList.Add(fname.ToLowerInvariant(), f); proj.ShouldReloadFiles = true; if (f.Exists == false) { Program.MakeSurePathExists(f.FileDir); File.WriteAllText(f.FileAbsPath, " "); foreach (XmlElement k in i.GetElementsByTagName("Template")) { File.WriteAllText(f.FileAbsPath, FileTemplate.CreateFile(f.FileName, proj.FileNameNoExt, k.InnerText)); break; } } } } catch { } } } } catch { return(false); } return(true); } else { return(false); } }