Skip to content

Commit

Permalink
Merge branch 'master' into jsdbg_gdb_support
Browse files Browse the repository at this point in the history
  • Loading branch information
cbiesinger committed May 2, 2019
2 parents f936c66 + f1773d5 commit 3e0d4ca
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
19 changes: 19 additions & 0 deletions server/JsDbg.Core/DllImports.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//--------------------------------------------------------------
//
// MIT License
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//--------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

namespace JsDbg.Core {
internal class DllImports {
[DllImport("shlwapi.dll")]
internal static extern bool PathIsNetworkPath(string path);
}
}
55 changes: 55 additions & 0 deletions server/JsDbg.Core/FileCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//--------------------------------------------------------------
//
// MIT License
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//--------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

namespace JsDbg.Core {
internal class FileCache {
internal FileCache() {
this.cachedFiles = new Dictionary<string, string>();
this.lastFlushDate = DateTime.Now;
}

internal static bool PathIsNetworkPath(string path) {
try {
return DllImports.PathIsNetworkPath(path);
} catch (DllNotFoundException) {
return false;
}
}

internal Stream ReadFile(string path) {
// Flush the cache every hour for the scenario where JsDbg is left open overnight etc.
if ((DateTime.Now - this.lastFlushDate).TotalSeconds > 3600) {
this.lastFlushDate = DateTime.Now;
this.cachedFiles = new Dictionary<string, string>();
}

if (FileCache.PathIsNetworkPath(path)) {
if (!cachedFiles.ContainsKey(path)) {
cachedFiles[path] = File.ReadAllText(path);
}

MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(cachedFiles[path]);
writer.Flush();
stream.Position = 0;
return stream;
} else {
return File.OpenRead(path);
}
}

private DateTime lastFlushDate;
private Dictionary<string, string> cachedFiles;
}
}

0 comments on commit 3e0d4ca

Please sign in to comment.