Skip to content

Commit 7db92c4

Browse files
committed
stdlib.dotnet namespace
1 parent 7ab348c commit 7db92c4

File tree

6 files changed

+54
-60
lines changed

6 files changed

+54
-60
lines changed

+stdlib/+dotnet/is_symlink.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function y = is_symlink(p)
2+
3+
try
4+
if stdlib.dotnet_api() >= 6
5+
y = ~isempty(System.IO.FileInfo(p).LinkTarget);
6+
else
7+
attr = string(System.IO.File.GetAttributes(p).ToString());
8+
% https://learn.microsoft.com/en-us/dotnet/api/system.io.fileattributes
9+
% ReparsePoint is for Linux, macOS, and Windows
10+
y = contains(attr, 'ReparsePoint');
11+
end
12+
catch e
13+
switch e.identifier
14+
case {'MATLAB:NET:CLRException:CreateObject', 'MATLAB:NET:CLRException:MethodInvoke'}
15+
y = false;
16+
otherwise, rethrow(e)
17+
end
18+
end
19+
20+
end

+stdlib/+sys/ram_free.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function bytes = ram_free()
2+
3+
if ispc()
4+
cmd = 'pwsh -c "(Get-CimInstance -ClassName CIM_OperatingSystem).FreePhysicalMemory * 1KB"';
5+
elseif ismac()
6+
cmd = 'sysctl -n hw.memsize';
7+
else
8+
cmd = "free -b | awk '/Mem:/ {print $4}'";
9+
end
10+
11+
[s, m] = system(cmd);
12+
if s == 0
13+
bytes = str2double(m);
14+
end
15+
16+
end

+stdlib/+sys/ram_total.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function bytes = ram_total()
2+
3+
if ispc()
4+
cmd = 'pwsh -c "(Get-CimInstance -ClassName Win32_ComputerSystem).TotalPhysicalMemory"';
5+
elseif ismac()
6+
cmd = 'sysctl -n hw.memsize';
7+
else
8+
cmd = "free -b | awk '/Mem:/ {print $4}'";
9+
end
10+
11+
[s, m] = system(cmd);
12+
if s == 0
13+
bytes = str2double(m);
14+
end
15+
16+
end

+stdlib/is_symlink.m

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
switch e.identifier
1212
case "MATLAB:UndefinedFunction"
1313
if stdlib.has_dotnet()
14-
ok = is_symlink_dotnet(p);
14+
ok = stdlib.dotnet.is_symlink(p);
1515
elseif stdlib.has_java()
1616
ok = java.nio.file.Files.isSymbolicLink(javaPathObject(stdlib.absolute(p)));
1717
elseif stdlib.has_python()
@@ -33,28 +33,6 @@
3333
end
3434

3535

36-
function y = is_symlink_dotnet(p)
37-
38-
try
39-
if stdlib.dotnet_api() >= 6
40-
y = ~isempty(System.IO.FileInfo(p).LinkTarget);
41-
else
42-
attr = string(System.IO.File.GetAttributes(p).ToString());
43-
% https://learn.microsoft.com/en-us/dotnet/api/system.io.fileattributes
44-
% ReparsePoint is for Linux, macOS, and Windows
45-
y = contains(attr, 'ReparsePoint');
46-
end
47-
catch e
48-
switch e.identifier
49-
case {'MATLAB:NET:CLRException:CreateObject', 'MATLAB:NET:CLRException:MethodInvoke'}
50-
y = false;
51-
otherwise, rethrow(e)
52-
end
53-
end
54-
55-
end
56-
57-
5836
%!test
5937
%! if !ispc
6038
%! p = tempname();

+stdlib/ram_free.m

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,6 @@
3737
end
3838

3939

40-
function bytes = ram_free_system()
41-
42-
if ispc()
43-
cmd = 'pwsh -c "(Get-CimInstance -ClassName CIM_OperatingSystem).FreePhysicalMemory * 1KB"';
44-
elseif ismac()
45-
cmd = 'sysctl -n hw.memsize';
46-
else
47-
cmd = "free -b | awk '/Mem:/ {print $4}'";
48-
end
49-
50-
[s, m] = system(cmd);
51-
if s == 0
52-
bytes = str2double(m);
53-
end
54-
55-
end
56-
57-
5840
function bytes = ram_free_java()
5941

6042
b = javaMethod("getOperatingSystemMXBean", "java.lang.management.ManagementFactory");

+stdlib/ram_total.m

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,14 @@
1919
end
2020

2121
if bytes <= 0
22-
bytes = ram_total_system();
22+
bytes = stdlib.sys.ram_total();
2323
end
2424

2525
bytes = uint64(bytes);
2626

2727
end
2828

2929

30-
function bytes = ram_total_system()
31-
32-
if ispc()
33-
cmd = 'pwsh -c "(Get-CimInstance -ClassName Win32_ComputerSystem).TotalPhysicalMemory"';
34-
elseif ismac()
35-
cmd = 'sysctl -n hw.memsize';
36-
else
37-
cmd = "free -b | awk '/Mem:/ {print $4}'";
38-
end
39-
40-
[s, m] = system(cmd);
41-
if s == 0
42-
bytes = str2double(m);
43-
end
44-
45-
end
46-
47-
4830
function bytes = ram_total_java()
4931
% https://docs.oracle.com/en/java/javase/21/docs/api/jdk.management/com/sun/management/OperatingSystemMXBean.html#getTotalMemorySize()
5032

0 commit comments

Comments
 (0)