TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
RUtil.cs
Go to the documentation of this file.
1 // RPlugin - A framework for running R scripts in .NET
2 // Copyright © 2012-2013 SEMERU
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 using System;
18 using System.Collections.Generic;
19 using System.IO;
20 using System.Linq;
21 using System.Reflection;
22 using System.Text;
23 
24 namespace RPlugin.Core
25 {
29  public static class RUtil
30  {
31  private static object UniqueIDLock = new object();
32 
38  public static DirectoryInfo CreateCacheDirectory(string name)
39  {
40  DirectoryInfo info;
41  lock (UniqueIDLock)
42  {
43  int index = 0;
44  while (Directory.Exists(Path.Combine(RController.Cache, index.ToString() + "." + name)))
45  {
46  index++;
47  }
48  info = RController.CreateCacheDirectory(index.ToString() + "." + name, true);
49  }
50  return info;
51  }
52 
59  public static FileStream CreateCacheFile(string name)
60  {
61  FileStream info;
62  lock (UniqueIDLock)
63  {
64  int index = 0;
65  while (File.Exists(Path.Combine(RController.Cache, index.ToString() + "." + name)))
66  {
67  index++;
68  }
69  info = RController.CreateCacheFile(index.ToString() + "." + name, false);
70  }
71  return info;
72  }
73 
80  public static string ReserveCacheFile(string name)
81  {
82  FileStream info;
83  lock (UniqueIDLock)
84  {
85  int index = 0;
86  while (File.Exists(Path.Combine(RController.Cache, index.ToString() + "." + name)))
87  {
88  index++;
89  }
90  info = RController.CreateCacheFile(index.ToString() + "." + name, false);
91  info.Close();
92  }
93  return info.Name;
94  }
95 
102  public static bool RegisterScript(Assembly assembly, string resourceName)
103  {
104  string destination = Path.Combine(RController.Scripts, resourceName);
105  if (!File.Exists(destination))
106  {
107  File.WriteAllText(destination, new StreamReader(assembly.GetManifestResourceStream(resourceName)).ReadToEnd());
108  return true;
109  }
110  else
111  {
112  return false;
113  }
114  }
115 
121  public static bool RegisterScript(string scriptPath)
122  {
123  string destination = Path.Combine(RController.Scripts, Path.GetFileName(scriptPath));
124  if (!File.Exists(destination))
125  {
126  File.Copy(scriptPath, destination);
127  return true;
128  }
129  else
130  {
131  return false;
132  }
133  }
134  }
135 }