Skip to content

Commit

Permalink
Merge pull request #4 from SqlQuantumLeap/Save-To-Clipboard
Browse files Browse the repository at this point in the history
Save to clipboard
  • Loading branch information
SqlQuantumLeap committed Dec 10, 2017
2 parents 563731a + fa4b79e commit ee41a1a
Show file tree
Hide file tree
Showing 20 changed files with 650 additions and 142 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -301,3 +301,4 @@ $RECYCLE.BIN/

# Windows shortcuts
*.lnk
BinaryFormatter/Tests/SampleBinary.sql
22 changes: 21 additions & 1 deletion BinaryFormatter/BinaryFormatter.csproj
Expand Up @@ -7,7 +7,7 @@
<ProjectGuid>{74A1E9C4-ED9B-4F12-93F7-AD7D1EE02A2B}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BinaryFormatter</RootNamespace>
<RootNamespace>SqlQuantumLeap.BinaryFormatter</RootNamespace>
<AssemblyName>BinaryFormatter</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
Expand All @@ -33,17 +33,37 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Configuration.cs" />
<Compile Include="Display.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="Tests\01 - Test No Inputs.cmd" />
<None Include="Tests\02 - Test Help Parameters.cmd" />
<None Include="Tests\03 - Test All Defaults.cmd" />
<None Include="Tests\04 - Test User Entered 7.cmd" />
<None Include="Tests\05 - Test Passed-In 7.cmd" />
<None Include="Tests\06 - Test Passed-In 8 %28Fail%29.cmd" />
<None Include="Tests\07 - Test Console and File.cmd" />
<None Include="Tests\08 - Test Console and Clipboard and NoFile.cmd" />
<None Include="Tests\09 - Test Clipboard and NoFile.cmd" />
<None Include="Tests\SampleBinary.bin" />
</ItemGroup>
<ItemGroup>
<Content Include="Tests\SampleOutput_ChunkSize-100%28orMore%29.sql" />
<Content Include="Tests\SampleOutput_ChunkSize-7.sql" />
</ItemGroup>
<ItemGroup>
<None Include="Tests\CreateSampleFile.sql" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
179 changes: 179 additions & 0 deletions BinaryFormatter/Configuration.cs
@@ -0,0 +1,179 @@
/*
* BinaryFormatter
* Version 2.0
* Copyright (c) 2009-2017 Solomon Rutzky. All rights reserved.
*
* Location: https://github.com/SqlQuantumLeap/BinaryFormatter
*
* Blog: https://SqlQuantumLeap.com/
*
*/
using System;
using System.IO;

namespace SqlQuantumLeap.BinaryFormatter
{
internal class Configuration
{
private int _ChunkSize = 10000;
private bool _ChunkSizeIsCustom = false;
private string _FilePath = String.Empty;
private string _OutFile = String.Empty;
private bool _SaveToClipboard = false;
private bool _SendToConsole = false;
private bool _NoOutputFile = false;


public int ChunkSize
{
get
{
return _ChunkSize;
}
}
public bool ChunkSizeIsCustom
{
get
{
return _ChunkSizeIsCustom;
}
}
public string FilePath
{
get
{
return _FilePath;
}
}
public string OutFile
{
get
{
return _OutFile;
}
}
public bool SaveToClipboard
{
get
{
return _SaveToClipboard;
}
}
public bool SendToConsole
{
get
{
return _SendToConsole;
}
}
public bool NoOutputFile
{
get
{
return _NoOutputFile;
}
}


public Configuration(string[] InputParameters)
{
foreach (string _Arg in InputParameters)
{
int _TempSize;

if (!_ChunkSizeIsCustom // Skip test if already found
&& int.TryParse(_Arg, out _TempSize))
{
_ChunkSize = _TempSize;
_ChunkSizeIsCustom = true;
}
else
{
switch (_Arg.ToUpperInvariant())
{
case "-CLIPBOARD":
case "/CLIPBOARD":
_SaveToClipboard = true;
break;

case "-CONSOLE":
case "/CONSOLE":
_SendToConsole = true;
break;

case "-NOFILE":
case "/NOFILE":
_NoOutputFile = true;
break;

default:
if (_FilePath == String.Empty)
{
_FilePath = _Arg;
}
else if (_OutFile == String.Empty)
{
_OutFile = _Arg;
}
break;
} /* switch() */
} /* if (!_ChunkSizeIsCustom... */
} /* foreach(string _Arg... */


if (_FilePath == String.Empty)
{
throw new ArgumentNullException("You must specify a source file.");
}


// Set default output file name
if (!_NoOutputFile // Skip if user requested no output file
&& _OutFile == String.Empty)
{
_OutFile = Path.GetDirectoryName(_FilePath) + @"\" +
Path.GetFileNameWithoutExtension(_FilePath) + @".sql";
}


if (!_ChunkSizeIsCustom)
{
string _CustomChunkSizeString;
int _CustomChunkSizeInt;
bool _GotValidAnswer = false;

while (!_GotValidAnswer)
{
Console.Write("Chunk Size [ {0} ]: ", _ChunkSize);
_CustomChunkSizeString = Console.ReadLine();
if (String.IsNullOrWhiteSpace(_CustomChunkSizeString))
{
_GotValidAnswer = true;
}
else
{
if (int.TryParse(_CustomChunkSizeString, out _CustomChunkSizeInt))
{
if (_CustomChunkSizeInt > 0)
{
_GotValidAnswer = true;
_ChunkSize = _CustomChunkSizeInt;
_ChunkSizeIsCustom = true; // not used below, but don't leave in inconsistent state
}
else
{
Display.Error("ChunkSize must be > 0.");
}
}
else
{
Display.Error("ChunkSize must be a number.");
}
} /* if (String.IsNullOrWhiteSpace(_CustomChunkSizeString)) ... else */
} /* while (!_GotValidAnswer) */
} /* if (!_ChunkSizeIsCustom) */

return;
}
}
}
61 changes: 61 additions & 0 deletions BinaryFormatter/Display.cs
@@ -0,0 +1,61 @@
/*
* BinaryFormatter
* Version 2.0
* Copyright (c) 2009-2017 Solomon Rutzky. All rights reserved.
*
* Location: https://github.com/SqlQuantumLeap/BinaryFormatter
*
* Blog: https://SqlQuantumLeap.com/
*
*/
using System;

namespace SqlQuantumLeap.BinaryFormatter
{
internal class Display
{
internal static void Usage()
{
Console.WriteLine("\nBinaryFormatter");
Console.WriteLine("Version {0}",
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(2));
Console.WriteLine("Copyright (c) 2009-2017 Solomon Rutzky. All rights reserved.");
Console.WriteLine("https://SqlQuantumLeap.com/\n");

Console.WriteLine("Format binary file into hex string for SQL script.");
Console.WriteLine("Output broken into multiple lines of \"ChunkSize\" bytes.");
Console.WriteLine("If there are multiple lines, all lines except the final");
Console.WriteLine("line are appended with the line-continuation character of: \\");

Console.Write("\nUsage:");
Console.WriteLine("\tBinaryFormatter [ /? | /help ]");
Console.WriteLine("\n\tDisplay this help info");

Console.Write("\nUsage:");
Console.WriteLine("\tBinaryFormatter path\\to\\binary_file.ext [path\\to\\OutputFile.sql] [ChunkSize]");
Console.WriteLine("\t\t[ /Clipboard ] [ /Console ] [ /NoFile ]\n");
Console.WriteLine("\tChunkSize = the number of bytes per row. A byte is 2 characters: 00 - FF.");
Console.WriteLine("\t/Clipboard = Copy output to clipboard (to then paste with Control-V)");
Console.WriteLine("\t/Console = Send output to console");
Console.WriteLine("\t/NoFile = Do not save to file, even if specified");
Console.WriteLine("");

Console.WriteLine("\tDefault ChunkSize = 10000");
Console.WriteLine("\tDefault OutputFile = {path\\to\\binary_file_name}.sql");
Console.WriteLine("\tMaximum line length = (ChunkSize * 2) + 1.");

Console.WriteLine("\nVisit https://SqlQuantumLeap.com/ for other useful utilities and more.");

return;
}

internal static void Error(string ErrorMessage)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ErrorMessage);
Console.ResetColor();

return;
}
}
}

0 comments on commit ee41a1a

Please sign in to comment.