Skip to content

Commit

Permalink
Added options to control line endings in output
Browse files Browse the repository at this point in the history
* Default: whatever is in the clipboard
* --lf: Converts \r\n line endings to \n
* --crlf: Converts \n line endings to \r\n safely
  • Loading branch information
mqudsi committed Jul 14, 2017
1 parent d3413b3 commit 8321bfe
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 10 deletions.
92 changes: 87 additions & 5 deletions paste/paste.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,42 @@ enum class ExitReason : int
SystemError
};

enum class LineEnding : int
{
AsIs,
CrLf,
Lf
};

void Write(const wchar_t *text, DWORD length = -1)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE || hOut == nullptr)
{
ExitProcess((UINT)ExitReason::SystemError);
}
DWORD charsWritten = -1;
WriteConsoleW(hOut, text, length != -1 ? length : lstrlen(text), &charsWritten, 0);
CloseHandle(hOut);
}

void Write(const char *text, DWORD length = -1)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE || hOut == nullptr)
{
ExitProcess((UINT)ExitReason::SystemError);
}
DWORD charsWritten = -1;
WriteConsoleA(hOut, text, length != -1 ? length : lstrlenA(text), &charsWritten, 0);
CloseHandle(hOut);
}

void Write(const wchar_t text)
{
return Write(&text, 1);
}

void WriteError(const wchar_t *error)
{
HANDLE hErr = GetStdHandle(STD_ERROR_HANDLE);
Expand All @@ -34,8 +70,57 @@ bool ClipboardContainsFormat(UINT format)
return false;
}

int main(int argc, const char *argv[])
void print(const WCHAR *text, LineEnding lineEnding)
{
DWORD charsWritten = -1;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

switch (lineEnding)
{
case LineEnding::AsIs:
WriteConsoleW(hOut, text, lstrlen(text), &charsWritten, nullptr);
break;
case LineEnding::Lf:
for (auto ptr = text; ptr && *ptr; ++ptr)
{
if (*ptr != '\r')
{
WriteConsoleW(hOut, ptr, 1, &charsWritten, nullptr);
}
}
break;
case LineEnding::CrLf:
for (auto ptr = text; ptr && *ptr; ++ptr)
{
if (*ptr == '\n' && (ptr == text || *(ptr -1) != '\r'))
{
WriteConsoleW(hOut, L"\r", 1, &charsWritten, nullptr);
}
WriteConsoleW(hOut, ptr, 1, &charsWritten, nullptr);
}
break;
}
CloseHandle(hOut);
}

int wmain(int argc, const WCHAR *argv[], const WCHAR *envp[])
{
LineEnding lineEnding = LineEnding::AsIs;
Write(L"wstring");
Write("string");

if (argc == 2)
{
if (lstrcmpi(argv[1], L"--lf") == 0)
{
lineEnding = LineEnding::Lf;
}
else if (lstrcmpi(argv[1], L"--crlf") == 0)
{
lineEnding = LineEnding::CrLf;
}
}

if (!OpenClipboard(nullptr))
{
WriteError(L"Failed to open system clipboard!\n");
Expand Down Expand Up @@ -65,10 +150,7 @@ int main(int argc, const char *argv[])
ExitProcess((UINT)ExitReason::ClipboardError);
}

DWORD charsWritten = -1;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsoleW(hOut, text, lstrlen(text), &charsWritten, 0);
CloseHandle(hOut);
print(text, lineEnding);

GlobalUnlock(hData);
CloseClipboard();
Expand Down
10 changes: 5 additions & 5 deletions paste/paste.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
<SubSystem>Console</SubSystem>
<AdditionalDependencies>user32.lib;kernel32.lib</AdditionalDependencies>
<IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
<EntryPointSymbol>main</EntryPointSymbol>
<EntryPointSymbol>wmain</EntryPointSymbol>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand All @@ -115,7 +115,7 @@
<SubSystem>Console</SubSystem>
<AdditionalDependencies>user32.lib;kernel32.lib</AdditionalDependencies>
<IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
<EntryPointSymbol>main</EntryPointSymbol>
<EntryPointSymbol>wmain</EntryPointSymbol>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -137,7 +137,7 @@
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>user32.lib;kernel32.lib</AdditionalDependencies>
<IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
<EntryPointSymbol>main</EntryPointSymbol>
<EntryPointSymbol>wmain</EntryPointSymbol>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -159,7 +159,7 @@
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>user32.lib;kernel32.lib</AdditionalDependencies>
<IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
<EntryPointSymbol>main</EntryPointSymbol>
<EntryPointSymbol>wmain</EntryPointSymbol>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand All @@ -168,4 +168,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

0 comments on commit 8321bfe

Please sign in to comment.