Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for file module #158

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -337,8 +337,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 @@ -727,7 +731,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 @@ -736,6 +740,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