Skip to content

Commit

Permalink
simplified parsing (removed id option)
Browse files Browse the repository at this point in the history
  • Loading branch information
creedflan738 committed Oct 3, 2024
1 parent f12ad32 commit 48b0f23
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 112 deletions.
78 changes: 7 additions & 71 deletions yahb/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class Config
public string destinationDirectory;
public bool copySubDirectories;
public int maxLvel;
public string configDirectory;
public List<String> inputDirectories;
public bool useVss;
public List<String> filePatternsToIgnore;
public List<String> directoriesToIgnore;
Expand All @@ -30,7 +28,7 @@ class Config
public List<String> commonDirsToIgnore;
public List<String> commonFilePatternsToIgnore;

private int logsCached;
//private int logsCached;

private const char _block = '#';

Expand All @@ -40,8 +38,6 @@ public Config()
this.destinationDirectory = "";
this.copySubDirectories = false;
this.maxLvel = 2147483647; // max 32 int value, should be enough
this.fnInputDirectories = "";
this.inputDirectories = new List<String>();
this.useVss = false;
this.filePatternsToIgnore = new List<String>();
this.directoriesToIgnore = new List<String>();
Expand All @@ -68,64 +64,24 @@ public Config()
commonFilePatternsToIgnore.Add("*.~");
commonFilePatternsToIgnore.Add("*.tmp");

this.logsCached = 0;
//this.logsCached = 0;
}

public void checkConsistency()
{
// check if we have either an input directory, or supplied file names via /if or /id
// with valid directory or file lists
bool hasInput = false;
if(!string.IsNullOrEmpty(this.sourceDirectory))
// check if we have a valid source directory
if(!string.IsNullOrEmpty(this.sourceDirectory) || !System.IO.Directory.Exists(this.sourceDirectory))
{
if(System.IO.Directory.Exists(this.sourceDirectory))
{
hasInput = true;
// create full path from relative path
this.sourceDirectory = System.IO.Path.GetFullPath(this.sourceDirectory);
} else
{
throw new ArgumentException("error: " + this.sourceDirectory + " is not a valid directory");
}
}

// check if the supplied list of input directories (if any) is valid
if (!string.IsNullOrEmpty(this.fnInputDirectories))
{
try
{
string[] lines = System.IO.File.ReadAllLines(this.fnInputDirectories);
foreach (string line in lines)
{
if(!System.IO.Directory.Exists(line))
{
throw new ArgumentException("error: " + line + " defined in " +
this.fnInputDirectories + " is not a valid directory");
}
// create full path from relative path
String absLine = System.IO.Path.GetFullPath(line);
this.inputDirectories.Add(absLine);
}
hasInput = true;
}
catch (Exception e)
{
throw new ArgumentException("Error: could not load input directories from: " + this.fnInputDirectories + " "+e.Message);
}
}

if(!hasInput)
{
throw new ArgumentException("error: no valid input directory defined, and no valid " +
"directory list supplied via /id");
throw new ArgumentException("error: no valid source directory defined or directory does not exist");
}
// create full path from relative path
this.sourceDirectory = System.IO.Path.GetFullPath(this.sourceDirectory);

// check if we have a valid destination directory
if(string.IsNullOrEmpty(this.destinationDirectory) || !System.IO.Directory.Exists(this.destinationDirectory)) {
throw new ArgumentException("error: no valid output directory defined or directory " +
"does not exist");
}

// create full path from relative path
this.destinationDirectory = System.IO.Path.GetFullPath(this.destinationDirectory);

Expand Down Expand Up @@ -263,25 +219,6 @@ public void WriteProgressBar(string pre, string post, int percent, bool update =
Console.Write("] {0,3:##0}% " + post, percent);
}

/*
private bool IsAdmin
{
get
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
if (identity != null)
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
List<Claim> list = new List<Claim>(principal.UserClaims);
Claim c = list.Find(p => p.Value.Contains("S-1-5-32-544"));
this.addToLog("seems to be admin!");
if (c != null)
return true;
}
return false;
}
}*/

public bool IsAdministrator()
{
var identity = WindowsIdentity.GetCurrent();
Expand All @@ -304,7 +241,6 @@ public override string ToString()
currentCfg += "file endings.........: " + String.Join(", ", this.fileEndings) + "\n";
currentCfg += "copy sub dirs........: " + this.copySubDirectories + "\n";
currentCfg += "max dir level........: " + this.maxLvel + "\n";
currentCfg += "input dirs list......: " + String.Join(", ", this.inputDirectories) + "\n";
currentCfg += "use vss..............: " + this.useVss + "\n";
currentCfg += "ignore patterns......: " + String.Join(", ", this.filePatternsToIgnore) + "\n";
currentCfg += "ignore dirs..........: " + String.Join(", ", this.directoriesToIgnore) + "\n";
Expand Down
18 changes: 0 additions & 18 deletions yahb/CopyModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,6 @@ public List<String> createDirectoryList() {
dir_stack.Push(cfg.sourceDirectory);
}

// add directories from file here to stack
// /s is used, in order to get all
// subdirs
if(cfg.copySubDirectories)
{
foreach( string dir_i in cfg.inputDirectories)
{
dir_stack.Push(dir_i);
}
}

List<string> subdirs;
while (dir_stack.Count > 0)
{
Expand Down Expand Up @@ -132,13 +121,6 @@ public List<String> createDirectoryList() {
{
dirs.Add(cfg.sourceDirectory);
}
// get directories from provided input file
// but only if /s was not provided (otherwise)
// above stack operation added them incl. subdirs
if (!cfg.copySubDirectories)
{
dirs.AddRange(cfg.inputDirectories);
}

// filter directories according to input given
List<String> dirs_filtered = new List<String>();
Expand Down
74 changes: 51 additions & 23 deletions yahb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,61 @@ static void Main(string[] args)
{
// Create an array of all possible command-line parameters
// and how to parse them.
object[,] mySwitches = new object[2, 17] {
{ "source", "target", "s", "copyall" , "config", "pause",
"xf", "xd", "list", "verbose",
"log", "+log", "tee", "?",
object[,] mySwitches = new object[2, 14] {
{"s", "copyall" , "pause",
"xf", "xd", "list", "verbose",
"log", "+log", "tee", "?",
"files", "vss", "help"},
{ ArgType.Complex, ArgType.Compound, ArgType.SimpleSwitch, ArgType.SimpleSwitch, ArgType.Compound, ArgType.SimpleSwitch,
ArgType.Complex, ArgType.Complex, ArgType.SimpleSwitch, ArgType.SimpleSwitch,
ArgType.Compound, ArgType.Compound, ArgType.SimpleSwitch, ArgType.SimpleSwitch,
{ArgType.SimpleSwitch, ArgType.SimpleSwitch, ArgType.SimpleSwitch,
ArgType.Complex, ArgType.Complex, ArgType.SimpleSwitch, ArgType.SimpleSwitch,
ArgType.Compound, ArgType.Compound, ArgType.SimpleSwitch, ArgType.SimpleSwitch,
ArgType.Complex, ArgType.SimpleSwitch, ArgType.SimpleSwitch}};

for (int counter = 0; counter < args.Length; counter++)
{
if (counter == 0)
{
if (!args[counter].StartsWith("/"))
{
// should be the input directory. Check validity later
cfg.sourceDirectory = args[counter];
}
else
{
// cannot be - we need a source directory
// only exception: user requested /help or /?
if (args[counter].Equals("/help") || args[counter].Equals("/?"))
{
// display help and exit
parse.DisplayVerboseHelp();
System.Environment.Exit(0);
}
else
{
throw (new ArgumentException(
"Cmd-Line parameter error: " + args[counter] +
" is a parameter, but a directory is expected.")
);
}
}
}

if (counter == 1)
{
if (!args[counter].StartsWith("/"))
{
cfg.destinationDirectory = args[counter];
}
else
{
// cannot be - we need a target directory
throw (new ArgumentException(
"Cmd-Line parameter error: " + args[counter] +
" is a parameter, but a directory is expected.")
);
}
}

if (args[counter].StartsWith("/"))
{
args[counter] = args[counter].TrimStart(new char[1] { '/' });
Expand Down Expand Up @@ -78,29 +121,14 @@ static void Main(string[] args)
// command-line parameter.
switch ((string)mySwitches[0, index])
{
case "source":
foreach (string inputDir in theArguments)
{
cfg.inputDirectories.Add(inputDir);
}
break;

case "target:":
cfg.destinationDirectory = theArgument;
break;

case "s":
cfg.copySubDirectories = true;
break;

case "lev":
cfg.maxLvel = System.Int32.Parse(theArgument);
break;

case "config":
cfg.configDirectory = theArgument;
break;


case "vss":
cfg.useVss = true;
break;
Expand Down

0 comments on commit 48b0f23

Please sign in to comment.