Skip to content

Commit

Permalink
Fix truncated entries on print
Browse files Browse the repository at this point in the history
Relative paths is annoying with dirEntries
  • Loading branch information
dd86k committed Mar 31, 2024
1 parent 5d87541 commit 39ba8e3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ ubyte[] hashFile(Digest digest, ref File file)
// NOTE: This is called from another thread
void mtDirEntry(DirEntry entry, immutable(void)* uobj)
{
string path = entry.name[2..$];
string path = fixpath( entry.name );

if (entry.isDir())
{
Expand All @@ -412,7 +412,7 @@ void mtDirEntry(DirEntry entry, immutable(void)* uobj)
{
Digest digest = cast(Digest)uobj; // Get thread-assigned instance
digest.reset();
printHash(hashFile(digest, entry.name), path);
printHash(hashFile(digest, path), path);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -567,7 +567,7 @@ int processList(string path, bool autodetect, Style style)

void processAgainstEntry(DirEntry entry, immutable(void)* uobj)
{
string path = entry.name[2..$];
string path = fixpath( entry.name );

if (entry.isDir())
{
Expand Down
36 changes: 33 additions & 3 deletions src/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
module utils;

import std.conv : text;
import std.datetime : Duration, dur;
import std.format.read : formattedRead;
import std.string : toStringz;
import std.datetime : Duration, dur;
import std.path : dirSeparator;
import core.stdc.stdio : sscanf;
import core.stdc.ctype : toupper;

Expand All @@ -17,15 +18,25 @@ import core.stdc.ctype : toupper;
// Use OS functions (GetFileAttributes/GetFileType, stat_t, etc.)

/// Parse string into a 32-bit unsigned integer.
/// Params: input =
/// Params: input = String user input.
/// Returns: Unformatted number.
uint cparse(string input)
int cparse(string input)
{
int u = void;
if (sscanf(input.toStringz, "%i", &u) != 1)
throw new Exception("Could not parse input");
return cast(uint)u;
}
unittest
{
import std.conv : octal;
assert(cparse("0") == 0);
assert(cparse("1") == 1);
assert(cparse("010") == octal!10);
assert(cparse("0x10") == 0x10);
assert(cparse("0x7fffffff") == 0x7fff_ffff);
assert(cparse("0x80000000") == 0x8000_0000);
}

private enum
{
Expand Down Expand Up @@ -267,4 +278,23 @@ unittest
assert(compareList("ABC", &cmp) == 3);
assert(compareList("ABCD", &cmp) == 6);
assert(compareList("ABCDE", &cmp) == 10);
}

// Better than the dirSeparator string
private immutable string pf = "."~dirSeparator;

// Fixes the annoying relative path that dirEntries *might* introduce.
// Safer than preemptively truncating it.
string fixpath(string entry)
{
if (entry.length <= pf.length)
return entry;

return entry[0..pf.length] != pf ? entry : entry[pf.length..$];
}
unittest
{
assert(fixpath("a") == "a");
assert(fixpath("abc") == "abc");
assert(fixpath(pf~"abc") == "abc");
}

0 comments on commit 39ba8e3

Please sign in to comment.