Skip to content

Commit

Permalink
Merge pull request #158 from marcgurevitx/fix-file
Browse files Browse the repository at this point in the history
Fixes for `file` module
  • Loading branch information
JoeStrout authored Jul 28, 2024
2 parents 264f7dd + ce50a35 commit daabb9a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ Debug
miniscript-cpp.dir
*.vcxproj*
*.sln

# Tests fallout
_*.txt
9 changes: 7 additions & 2 deletions MiniScript-cpp/src/ShellIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,12 @@ static IntrinsicResult intrinsic_basename(Context *context, IntrinsicResult part
char extBuf[256];
_splitpath_s(pathStr.c_str(), driveBuf, sizeof(driveBuf), nullptr, 0, nameBuf, sizeof(nameBuf), extBuf, sizeof(extBuf));
String result = String(nameBuf) + String(extBuf);
#else
#elif defined(__APPLE__) || defined(__FreeBSD__)
String result(basename((char*)pathStr.c_str()));
#else
char *duplicate = strdup((char*)pathStr.c_str());
String result(basename(duplicate));
free(duplicate);
#endif
return IntrinsicResult(result);
}
Expand Down Expand Up @@ -726,7 +730,7 @@ static IntrinsicResult intrinsic_readLines(Context *context, IntrinsicResult par
partialLine = "";
}
list.Add(line);
if (buf[i] == '\n' && i+1 < bytesRead && buf[i+1] == '\r') i++;
if (buf[i] == '\r' && i+1 < bytesRead && buf[i+1] == '\n') i++;
if (i+1 < bytesRead && buf[i+1] == 0) i++;
lineStart = i + 1;
}
Expand All @@ -735,6 +739,7 @@ static IntrinsicResult intrinsic_readLines(Context *context, IntrinsicResult par
partialLine = String(&buf[lineStart], bytesRead - lineStart);
}
}
if (!partialLine.empty()) list.Add(partialLine);
fclose(handle);
return IntrinsicResult(list);
}
Expand Down
12 changes: 12 additions & 0 deletions MiniScript-cpp/tests/testFileName.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import "qa"

testDir = "tests/"

testFileName = function
p = "a/b/"
n = file.name(p)
qa.assertEqual n, "b"
qa.assertEqual p, "a/b/"
end function

if refEquals(locals, globals) then testFileName
25 changes: 25 additions & 0 deletions MiniScript-cpp/tests/testFileReadLines.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import "qa"

testDir = "tests/"

testFileReadLines = function
fn = file.child(testDir, "_cr.txt")
f = file.open(fn, "w")
f.write("a" + char(13) + "b" + char(13) + "c")
f.close
qa.assertEqual file.readLines("tests/_cr.txt"), ["a", "b", "c"]

fn = file.child(testDir, "_lf.txt")
f = file.open(fn, "w")
f.write("a" + char(10) + "b" + char(10) + "c")
f.close
qa.assertEqual file.readLines("tests/_lf.txt"), ["a", "b", "c"]

fn = file.child(testDir, "_crlf.txt")
f = file.open(fn, "w")
f.write("a" + char(13) + char(10) + "b" + char(13) + char(10) + "c")
f.close
qa.assertEqual file.readLines("tests/_crlf.txt"), ["a", "b", "c"]
end function

if refEquals(locals, globals) then testFileReadLines

0 comments on commit daabb9a

Please sign in to comment.