Skip to content

Commit daabb9a

Browse files
authored
Merge pull request #158 from marcgurevitx/fix-file
Fixes for `file` module
2 parents 264f7dd + ce50a35 commit daabb9a

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,6 @@ Debug
5252
miniscript-cpp.dir
5353
*.vcxproj*
5454
*.sln
55+
56+
# Tests fallout
57+
_*.txt

MiniScript-cpp/src/ShellIntrinsics.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,12 @@ static IntrinsicResult intrinsic_basename(Context *context, IntrinsicResult part
336336
char extBuf[256];
337337
_splitpath_s(pathStr.c_str(), driveBuf, sizeof(driveBuf), nullptr, 0, nameBuf, sizeof(nameBuf), extBuf, sizeof(extBuf));
338338
String result = String(nameBuf) + String(extBuf);
339-
#else
339+
#elif defined(__APPLE__) || defined(__FreeBSD__)
340340
String result(basename((char*)pathStr.c_str()));
341+
#else
342+
char *duplicate = strdup((char*)pathStr.c_str());
343+
String result(basename(duplicate));
344+
free(duplicate);
341345
#endif
342346
return IntrinsicResult(result);
343347
}
@@ -726,7 +730,7 @@ static IntrinsicResult intrinsic_readLines(Context *context, IntrinsicResult par
726730
partialLine = "";
727731
}
728732
list.Add(line);
729-
if (buf[i] == '\n' && i+1 < bytesRead && buf[i+1] == '\r') i++;
733+
if (buf[i] == '\r' && i+1 < bytesRead && buf[i+1] == '\n') i++;
730734
if (i+1 < bytesRead && buf[i+1] == 0) i++;
731735
lineStart = i + 1;
732736
}
@@ -735,6 +739,7 @@ static IntrinsicResult intrinsic_readLines(Context *context, IntrinsicResult par
735739
partialLine = String(&buf[lineStart], bytesRead - lineStart);
736740
}
737741
}
742+
if (!partialLine.empty()) list.Add(partialLine);
738743
fclose(handle);
739744
return IntrinsicResult(list);
740745
}

MiniScript-cpp/tests/testFileName.ms

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import "qa"
2+
3+
testDir = "tests/"
4+
5+
testFileName = function
6+
p = "a/b/"
7+
n = file.name(p)
8+
qa.assertEqual n, "b"
9+
qa.assertEqual p, "a/b/"
10+
end function
11+
12+
if refEquals(locals, globals) then testFileName
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import "qa"
2+
3+
testDir = "tests/"
4+
5+
testFileReadLines = function
6+
fn = file.child(testDir, "_cr.txt")
7+
f = file.open(fn, "w")
8+
f.write("a" + char(13) + "b" + char(13) + "c")
9+
f.close
10+
qa.assertEqual file.readLines("tests/_cr.txt"), ["a", "b", "c"]
11+
12+
fn = file.child(testDir, "_lf.txt")
13+
f = file.open(fn, "w")
14+
f.write("a" + char(10) + "b" + char(10) + "c")
15+
f.close
16+
qa.assertEqual file.readLines("tests/_lf.txt"), ["a", "b", "c"]
17+
18+
fn = file.child(testDir, "_crlf.txt")
19+
f = file.open(fn, "w")
20+
f.write("a" + char(13) + char(10) + "b" + char(13) + char(10) + "c")
21+
f.close
22+
qa.assertEqual file.readLines("tests/_crlf.txt"), ["a", "b", "c"]
23+
end function
24+
25+
if refEquals(locals, globals) then testFileReadLines

0 commit comments

Comments
 (0)