Skip to content

Commit

Permalink
Filesystem work.
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesbetros committed Mar 2, 2016
1 parent 5b35e17 commit 559ee85
Show file tree
Hide file tree
Showing 20 changed files with 1,337 additions and 551 deletions.
603 changes: 388 additions & 215 deletions Tests/Cosmos.Kernel.Tests.Fat/Kernel.cs

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions source/Cosmos.Common/StringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,39 @@ public static string GetNumberString(int aValue)
return xResult;
}

public static string GetNumberString(long aValue)
{
string[] xChars = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
string xResult = string.Empty;
long xValue = aValue;

if (aValue < 0)
{
xValue *= -1;
}

if (aValue == 0)
{
xResult = string.Concat(xResult, "0");
}
else
{
while (xValue > 0)
{
long xValue2 = xValue % 10;
xResult = string.Concat(xChars[xValue2], xResult);
xValue /= 10;
}
}

if (aValue < 0)
{
xResult = string.Concat("-", xResult);
}

return xResult;
}

public static int GetStringToNumber(string aString)
{
bool xIsNegative = false;
Expand Down
6 changes: 6 additions & 0 deletions source/Cosmos.Debug.Kernel/Debugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public void SendInternal(string aText)
DoSend(aText);
}

[Conditional("COSMOSDEBUG")]
public void SendInternal(uint aCount)
{
DoSendNumber(aCount);
}

//public void OldSend(string aText) {
// // TODO: Need to fix this so it can send empty strings.
// // Sending empty strings locks it up right now
Expand Down
4 changes: 1 addition & 3 deletions source/Cosmos.IL2CPU/ILScanner.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#define COSMOSDEBUG

using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down
5 changes: 5 additions & 0 deletions source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="JetBrains.Annotations, Version=10.0.0.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
<HintPath>..\packages\JetBrains.Annotations.10.0.0\lib\net20\JetBrains.Annotations.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
Expand Down Expand Up @@ -141,6 +145,7 @@
</ItemGroup>
<ItemGroup>
<None Include="Cosmos.snk" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
9 changes: 8 additions & 1 deletion source/Cosmos.System.Plugs/System/IO/DirectoryImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
//#define COSMOSDEBUG

using System;
using System.IO;
using System.Collections.Generic;

Expand Down Expand Up @@ -62,6 +64,8 @@ public static DirectoryInfo CreateDirectory(string aPath)

public static DirectoryInfo GetParent(string aPath)
{
Global.mFileSystemDebugger.SendInternal("Directory.GetParent:");

if (aPath == null)
{
Global.mFileSystemDebugger.SendInternal("Directory.GetParent : aPath is null");
Expand All @@ -74,6 +78,9 @@ public static DirectoryInfo GetParent(string aPath)
throw new ArgumentException("Path must not be empty.", "aPath");
}

Global.mFileSystemDebugger.SendInternal("aPath =");
Global.mFileSystemDebugger.SendInternal(aPath);

string xFullPath = Path.GetFullPath(aPath);
string xParentDirectory = Path.GetDirectoryName(xFullPath);
if (xParentDirectory == null)
Expand Down
30 changes: 14 additions & 16 deletions source/Cosmos.System.Plugs/System/IO/DirectoryInfoImpl.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System.IO;
//#define COSMOSDEBUG

using System;
using System.Collections.Generic;
using System.IO;

using Cosmos.Debug.Kernel;
using Cosmos.IL2CPU.Plugs;
using Cosmos.System.FileSystem;
using Cosmos.System.FileSystem.Listing;
using Cosmos.System.FileSystem.VFS;

namespace Cosmos.System.Plugs.System.IO
{
using Cosmos.System.FileSystem;

using global::System;
using global::System.Collections.Generic;

[Plug(Target = typeof(DirectoryInfo))]
[PlugField(FieldId = "$$Storage$$", FieldType = typeof(DirectoryEntry))]
[PlugField(FieldId = "$$FullPath$$", FieldType = typeof(string))]
Expand All @@ -25,21 +25,19 @@ public static void Ctor(
[FieldAccess(Name = "$$FullPath$$")] ref string aFullPath,
[FieldAccess(Name = "$$Name$$")] ref string aName)
{
Global.mFileSystemDebugger.SendInternal($"DirectoryInfo.ctor : aPath = = {aPath}");
if (aPath == null)
{
throw new ArgumentNullException("aPath is null in DirectoryInfo ctor");
}
Global.mFileSystemDebugger.SendInternal("DirectoryInfo.ctor:");

if (!VFSManager.DirectoryExists(aPath))
if (string.IsNullOrEmpty(aPath))
{
throw new DirectoryNotFoundException("Unable to find directory " + aPath);
throw new ArgumentNullException(nameof(aPath));
}

aStorage = VFSManager.GetDirectory(aPath);
aFullPath = VFSManager.GetFullPath(aStorage);
aName = Path.GetDirectoryName(aFullPath);
Global.mFileSystemDebugger.SendInternal("aPath =");
Global.mFileSystemDebugger.SendInternal(aPath);

aStorage = VFSManager.GetDirectory(aPath);
aFullPath = aStorage.mFullPath;
aName = aStorage.mName;
}

public static string get_Name(DirectoryInfo aThis)
Expand Down
86 changes: 62 additions & 24 deletions source/Cosmos.System.Plugs/System/IO/FileImpl.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
using global::System;
//#define COSMOSDEBUG

using global::System;
using global::System.IO;

using Cosmos.Common.Extensions;
using Cosmos.Debug.Kernel;
using Cosmos.IL2CPU.Plugs;
using Cosmos.System.FileSystem;
using Cosmos.System.FileSystem.VFS;
using JetBrains.Annotations;

namespace Cosmos.System.Plugs.System.IO
{
// TODO A lot of these methods should be implemented using StreamReader / StreamWriter
[Plug(Target = typeof(File))]
public static class FileImpl
{
public static bool Exists(string aFile)
public static bool Exists([NotNull] string aFile)
{
Global.mFileSystemDebugger.SendInternal("File.Exists:");

if (string.IsNullOrEmpty(aFile))
{
throw new ArgumentException("Argument is null or empty", nameof(aFile));
}
Global.mFileSystemDebugger.SendInternal("aFile =");
Global.mFileSystemDebugger.SendInternal(aFile);

return VFSManager.FileExists(aFile);
}

public static string ReadAllText(string aFile)
public static string ReadAllText([NotNull] string aFile)
{
Global.mFileSystemDebugger.SendInternal("In FileImpl.ReadAllText");
Global.mFileSystemDebugger.SendInternal("File.ReadAllText:");

if (string.IsNullOrEmpty(aFile))
{
throw new ArgumentException("Argument is null or empty", nameof(aFile));
}
Global.mFileSystemDebugger.SendInternal("aFile =");
Global.mFileSystemDebugger.SendInternal(aFile);

using (var xFS = new FileStream(aFile, FileMode.Open))
{
var xBuff = new byte[(int)xFS.Length];
Expand All @@ -36,16 +56,40 @@ public static string ReadAllText(string aFile)
}
}

public static void WriteAllText(string aFile, string aText)
public static void WriteAllText([NotNull] string aFile, [NotNull] string aText)
{
Global.mFileSystemDebugger.SendInternal("Creating stream with file " + aFile);

if (string.IsNullOrEmpty(aFile))
{
throw new ArgumentException("Argument is null or empty", nameof(aFile));
}
Global.mFileSystemDebugger.SendInternal("aFile =");
Global.mFileSystemDebugger.SendInternal(aFile);

if (string.IsNullOrEmpty(aText))
{
throw new ArgumentException("Argument is null or empty", nameof(aText));
}
Global.mFileSystemDebugger.SendInternal("aText =");
Global.mFileSystemDebugger.SendInternal(aText);

using (var xFS = new FileStream(aFile, FileMode.Create))
{
Global.mFileSystemDebugger.SendInternal("Converting " + aText + " to UFT8");
var xBuff = aText.GetUtf8Bytes(0, (uint)aText.Length);
Global.mFileSystemDebugger.SendInternal("Writing bytes");
xFS.Write(xBuff, 0, xBuff.Length);
Global.mFileSystemDebugger.SendInternal("Bytes written");
if ((xBuff != null) && (xBuff.Length > 0))
{
Global.mFileSystemDebugger.SendInternal("xBuff.Length =");
Global.mFileSystemDebugger.SendInternal((uint) xBuff.Length);
Global.mFileSystemDebugger.SendInternal("Writing bytes");
xFS.Write(xBuff, 0, xBuff.Length);
Global.mFileSystemDebugger.SendInternal("Bytes written");
}
else
{
throw new Exception("No text data to write.");
}
}
}

Expand All @@ -69,12 +113,14 @@ public static string[] ReadAllLines(string aFile)
Global.mFileSystemDebugger.SendInternal("Read contents");
Global.mFileSystemDebugger.SendInternal(text);

String []result = text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string []result = text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

Global.mFileSystemDebugger.SendInternal("content as array of lines:");
#if COSMOSDEBUG
for (int i = 0; i < result.Length; i++)
{
Global.mFileSystemDebugger.SendInternal(result[i]);
}
#endif

return result;
Expand Down Expand Up @@ -107,7 +153,7 @@ public static byte[] ReadAllBytes(string aFile)
throw new Exception("Couldn't read complete file!");
}
Global.mFileSystemDebugger.SendInternal("Bytes read");

return xBuff;
}
}
Expand All @@ -116,39 +162,31 @@ public static void WriteAllBytes(string aFile, byte[] aBytes)
{
using (var xFS = new FileStream(aFile, FileMode.Create))
{
// This variable is not needed 'aBytes' is already a Byte[]
//var xBuff = aBytes;

xFS.Write(aBytes, 0, aBytes.Length);
}
}

public static void Copy(string srcFile, string destFile)
{
byte[] xBuff;
using (var xFS = new FileStream(srcFile, FileMode.Open))
{
xBuff = new byte[(int)xFS.Length];
var s1 = xFS.Read(xBuff, 0, xBuff.Length);
var xBuff = new byte[(int)xFS.Length];
var yFS = new FileStream(destFile, FileMode.Create);
yFS.Write(xBuff, 0, xBuff.Length);

}
}

public static FileStream Create(string aFile)
public static FileStream Create([NotNull] string aFile)
{
if (aFile == null)
{
throw new ArgumentNullException("aFile");
}
Global.mFileSystemDebugger.SendInternal("File.Create:");

if (aFile.Length == 0)
if (string.IsNullOrEmpty(aFile))
{
throw new ArgumentException("File path must not be empty.", "aFile");
throw new ArgumentException("Argument is null or empty", nameof(aFile));
}

Global.mFileSystemDebugger.SendInternal($"File.Create : aFile = {aFile}");

var xEntry = VFSManager.CreateFile(aFile);
if (xEntry == null)
{
Expand Down
9 changes: 7 additions & 2 deletions source/Cosmos.System.Plugs/System/IO/FileStreamImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Cosmos.Debug.Kernel;
//#define COSMOSDEBUG

using global::System;
using global::System.IO;
Expand All @@ -20,7 +20,8 @@ public class FileStreamImpl
public static void Ctor(FileStream aThis, string aPathname, FileMode aMode,
[FieldAccess(Name = "$$InnerStream$$")] ref Stream innerStream)
{
Global.mFileSystemDebugger.SendInternal("In FileStream.Ctor");
Global.mFileSystemDebugger.SendInternal("FileStream.Ctor:");

innerStream = InitializeStream(aPathname, aMode);
}

Expand All @@ -32,12 +33,16 @@ public static void CCtor()
public static int Read(FileStream aThis, byte[] aBuffer, int aOffset, int aCount,
[FieldAccess(Name = "$$InnerStream$$")] ref Stream innerStream)
{
Global.mFileSystemDebugger.SendInternal("FileStream.Read:");

return innerStream.Read(aBuffer, aOffset, aCount);
}

public static void Write(FileStream aThis, byte[] aBuffer, int aOffset, int aCount,
[FieldAccess(Name = "$$InnerStream$$")] ref Stream innerStream)
{
Global.mFileSystemDebugger.SendInternal("FileStream.Write:");

innerStream.Write(aBuffer, aOffset, aCount);
}

Expand Down
Loading

0 comments on commit 559ee85

Please sign in to comment.