Path Config - helper functions for forming absolute paths from relative or special identifiers

This commit is contained in:
andres.delikat 2011-05-03 20:48:51 +00:00
parent 255818b801
commit dae63bc513
2 changed files with 148 additions and 20 deletions

View File

@ -176,7 +176,7 @@
//
this.Cancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.Cancel.Location = new System.Drawing.Point(380, 390);
this.Cancel.Location = new System.Drawing.Point(381, 390);
this.Cancel.Name = "Cancel";
this.Cancel.Size = new System.Drawing.Size(75, 23);
this.Cancel.TabIndex = 0;
@ -187,7 +187,7 @@
// OK
//
this.OK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.OK.Location = new System.Drawing.Point(299, 390);
this.OK.Location = new System.Drawing.Point(300, 390);
this.OK.Name = "OK";
this.OK.Size = new System.Drawing.Size(75, 23);
this.OK.TabIndex = 1;
@ -547,9 +547,8 @@
//
// tabControl1
//
this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage3);
@ -1573,13 +1572,13 @@
| System.Windows.Forms.AnchorStyles.Right)));
this.BasePathBox.Location = new System.Drawing.Point(29, 27);
this.BasePathBox.Name = "BasePathBox";
this.BasePathBox.Size = new System.Drawing.Size(202, 20);
this.BasePathBox.Size = new System.Drawing.Size(203, 20);
this.BasePathBox.TabIndex = 23;
//
// BrowseBase
//
this.BrowseBase.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.BrowseBase.Location = new System.Drawing.Point(237, 25);
this.BrowseBase.Location = new System.Drawing.Point(238, 25);
this.BrowseBase.Name = "BrowseBase";
this.BrowseBase.Size = new System.Drawing.Size(54, 23);
this.BrowseBase.TabIndex = 24;
@ -1590,7 +1589,7 @@
//
this.label14.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.label14.AutoSize = true;
this.label14.Location = new System.Drawing.Point(297, 31);
this.label14.Location = new System.Drawing.Point(298, 31);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(31, 13);
this.label14.TabIndex = 25;
@ -1613,7 +1612,7 @@
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.Cancel;
this.ClientSize = new System.Drawing.Size(467, 425);
this.ClientSize = new System.Drawing.Size(468, 425);
this.Controls.Add(this.RecentForROMs);
this.Controls.Add(this.BasePathBox);
this.Controls.Add(this.BrowseBase);

View File

@ -21,9 +21,10 @@ namespace BizHawk.MultiClient
//If "always use recent path for roms" is checked then base path of each platorm should be disabled
//Path text boxes shoudl be anchored L + R and the remaining widgets anchored R
//Find a way for base to always be absolute
//Make all base path text boxes not allow %recent%
//All path text boxes should do some kind of error checking
string EXEPath; //TODO: public variable in main, populated at run time
string BasePath; //TODO: needs to be in config of course, but populated with EXEPath (absolute) if ".", . and .. in the context of this box are relative to EXE
public PathConfig()
{
@ -32,27 +33,155 @@ namespace BizHawk.MultiClient
private void PathConfig_Load(object sender, EventArgs e)
{
EXEPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
EXEPath = GetExePathAbsolute();
WatchBox.Text = Global.Config.WatchPath;
}
//--------------------------------------
//TODO: Move these to Main (or util if EXE path is same
private string GetExePathAbsolute()
{
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
}
private string GetBasePathAbsolute()
{
//Gets absolute base as derived from EXE
if (Global.Config.BasePath.Length < 1) //If empty, then EXE path
return GetExePathAbsolute();
if (Global.Config.BasePath.Substring(0,5) == "%exe%")
return GetExePathAbsolute();
if (Global.Config.BasePath[0] == '.')
{
if (Global.Config.BasePath.Length == 1)
return GetExePathAbsolute();
else
{
if (Global.Config.BasePath.Length == 2 &&
Global.Config.BasePath == ".\\")
return GetExePathAbsolute();
else
{
string tmp = Global.Config.BasePath;
tmp = tmp.Remove(0, 1);
tmp = tmp.Insert(0, GetExePathAbsolute());
}
}
}
if (Global.Config.BasePath.Substring(0, 2) == "..")
return RemoveParents(Global.Config.BasePath, GetExePathAbsolute());
//In case of error, return EXE path
return GetExePathAbsolute();
}
private string MakeAbsolutePath(string path)
{
//This function translates relative path and special identifiers in absolute paths
if (path == "recent")
return Global.Config.LastRomPath; //TODO: Don't use this, shoudl be an Environment one instead?
if (path == "%base%")
return MakeAbsolutePath(BasePathBox.Text);
if (path == "%exe%")
return MakeAbsolutePath(EXEPath);
if (path.Length < 1)
return GetBasePathAbsolute();
if (path == ".")
return BasePathBox.Text;
if (path == "%recent%")
{
//return last used directory (environment path)
}
return path;
if (path.Substring(0, 5) == "%exe%")
{
if (path.Length == 5)
return GetExePathAbsolute();
else
{
string tmp = path.Remove(0, 5);
tmp = tmp.Insert(0, GetExePathAbsolute());
return tmp;
}
}
if (path[0] == '.')
{
if (path.Length == 1)
return GetBasePathAbsolute();
else
{
string tmp = path.Remove(0, 1);
tmp = tmp.Insert(0, GetBasePathAbsolute());
return tmp;
}
}
//If begins wtih .. do alorithm to determine how many ..\.. combos and deal with accordingly, return drive letter only if too many ..
if ((path[0] > 'A' && path[0] < 'Z') || (path[0] > 'a' && path[0] < 'z'))
{
if (path.Length > 2 && path[1] == ':' && path[2] == '\\')
return path;
else
return GetExePathAbsolute(); //bad path
}
//all pad paths default to EXE
return GetExePathAbsolute();
}
private string RemoveParents(string path, string workingpath)
{
//determines number of parents, then removes directories from working path, return absolute path result
//Ex: "..\..\Bob\", "C:\Projects\Emulators\Bizhawk" will return "C:\Projects\Bob\"
int x = NumParentDirectories(path);
if (x > 0)
{
int y = HowMany(path, "..\\");
int z = HowMany(workingpath, "\\");
if (y >= z)
{
//Return drive letter only, working path must be absolute?
}
return "";
}
else return path;
}
private int NumParentDirectories(string path)
{
//determine the number of parent directories in path and return result
int x = HowMany(path, '\\');
if (x > 0)
{
return HowMany(path, "..\\");
}
return 0;
}
public int HowMany(string str, string s)
{
int count = 0;
for (int x = 0; x < (str.Length - s.Length); x++)
{
if (str.Substring(x, s.Length) == s)
count++;
}
return count;
}
public int HowMany(string str, char c)
{
int count = 0;
for (int x = 0; x < str.Length; x++)
{
if (str[x] == c)
count++;
}
return count;
}
//-------------------------------------------------------
private void SaveSettings()
{
Global.Config.WatchPath = WatchBox.Text;