Quick implementation of free UNIX command
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Wed, 9 Jul 2014 05:24:38 +0000 (07:24 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Wed, 9 Jul 2014 05:24:38 +0000 (07:24 +0200)
Allgemeines/FreeCSharp/FreeCSharp.sln [new file with mode: 0644]
Allgemeines/FreeCSharp/FreeCSharp/FreeCSharp.csproj [new file with mode: 0644]
Allgemeines/FreeCSharp/FreeCSharp/Program.cs [new file with mode: 0644]
Allgemeines/FreeCSharp/FreeCSharp/Properties/AssemblyInfo.cs [new file with mode: 0644]

diff --git a/Allgemeines/FreeCSharp/FreeCSharp.sln b/Allgemeines/FreeCSharp/FreeCSharp.sln
new file mode 100644 (file)
index 0000000..eb99328
--- /dev/null
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2012
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FreeCSharp", "FreeCSharp\FreeCSharp.csproj", "{C6503E05-09CF-4186-8153-0F30B7642DA5}"
+EndProject
+Global
+       GlobalSection(SolutionConfigurationPlatforms) = preSolution
+               Debug|x86 = Debug|x86
+               Release|x86 = Release|x86
+       EndGlobalSection
+       GlobalSection(ProjectConfigurationPlatforms) = postSolution
+               {C6503E05-09CF-4186-8153-0F30B7642DA5}.Debug|x86.ActiveCfg = Debug|x86
+               {C6503E05-09CF-4186-8153-0F30B7642DA5}.Debug|x86.Build.0 = Debug|x86
+               {C6503E05-09CF-4186-8153-0F30B7642DA5}.Release|x86.ActiveCfg = Release|x86
+               {C6503E05-09CF-4186-8153-0F30B7642DA5}.Release|x86.Build.0 = Release|x86
+       EndGlobalSection
+       GlobalSection(MonoDevelopProperties) = preSolution
+               StartupItem = FreeCSharp\FreeCSharp.csproj
+       EndGlobalSection
+EndGlobal
diff --git a/Allgemeines/FreeCSharp/FreeCSharp/FreeCSharp.csproj b/Allgemeines/FreeCSharp/FreeCSharp/FreeCSharp.csproj
new file mode 100644 (file)
index 0000000..f577e1e
--- /dev/null
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+    <ProductVersion>12.0.0</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{C6503E05-09CF-4186-8153-0F30B7642DA5}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>FreeCSharp</RootNamespace>
+    <AssemblyName>FreeCSharp</AssemblyName>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug</OutputPath>
+    <DefineConstants>DEBUG;</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <Externalconsole>true</Externalconsole>
+    <PlatformTarget>x86</PlatformTarget>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+    <DebugType>full</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release</OutputPath>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <Externalconsole>true</Externalconsole>
+    <PlatformTarget>x86</PlatformTarget>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+</Project>
\ No newline at end of file
diff --git a/Allgemeines/FreeCSharp/FreeCSharp/Program.cs b/Allgemeines/FreeCSharp/FreeCSharp/Program.cs
new file mode 100644 (file)
index 0000000..7f2817c
--- /dev/null
@@ -0,0 +1,75 @@
+using System;
+using System.Text.RegularExpressions;
+using System.IO;
+
+namespace FreeCSharp
+{
+    class MainClass
+    {
+        public static void Main(string[] args)
+        {
+            FreeCSharp free = new FreeCSharp();
+            free.GetValues();
+
+            long buffersPlusCached = free.Buffers + free.Cached;
+            long mainUsed = free.MemTotal - free.MemFree;
+
+            // What you would get from free command:
+            Console.WriteLine("-/+ buffers/cache:    {0}     {1}", (mainUsed - buffersPlusCached), (free.MemFree + buffersPlusCached));
+
+            // What means:
+            Console.WriteLine("Used physical memory: {0} kB", mainUsed - buffersPlusCached);
+            Console.WriteLine("Available physical memory: {0} kB", free.MemFree + buffersPlusCached);
+        }
+    }
+
+    /// <summary>
+    /// FreeCSharp: quick implementation of free command (kind of) using C#
+    /// </summary>
+    public class FreeCSharp
+    {
+        public long MemTotal { get; private set; }
+        public long MemFree { get; private set; }
+        public long Buffers { get; private set; }
+        public long Cached { get; private set; }
+
+        public void GetValues()
+        {
+            string[] memInfoLines = File.ReadAllLines(@"/proc/meminfo");
+
+            MemInfoMatch[] memInfoMatches =
+            {
+                new MemInfoMatch(@"^Buffers:\s+(\d+)", value => Buffers = Convert.ToInt64(value)),
+                new MemInfoMatch(@"^Cached:\s+(\d+)", value => Cached = Convert.ToInt64(value)),
+                new MemInfoMatch(@"^MemFree:\s+(\d+)", value => MemFree = Convert.ToInt64(value)),
+                new MemInfoMatch(@"^MemTotal:\s+(\d+)", value => MemTotal = Convert.ToInt64(value))
+            };
+
+            foreach (string memInfoLine in memInfoLines)
+            {
+                foreach (MemInfoMatch memInfoMatch in memInfoMatches)
+                {
+                    Match match = memInfoMatch.regex.Match(memInfoLine);
+                    if (match.Groups[1].Success)
+                    {
+                        string value = match.Groups[1].Value;
+                        memInfoMatch.updateValue(value);
+                    }
+                }
+            }
+        }
+
+        public class MemInfoMatch
+        {
+            public Regex regex;
+            public Action<string> updateValue;
+
+            public MemInfoMatch(string pattern, Action<string> update)
+            {
+                this.regex = new Regex(pattern, RegexOptions.Compiled);
+                this.updateValue = update;
+            }
+
+        }
+    }
+}
diff --git a/Allgemeines/FreeCSharp/FreeCSharp/Properties/AssemblyInfo.cs b/Allgemeines/FreeCSharp/FreeCSharp/Properties/AssemblyInfo.cs
new file mode 100644 (file)
index 0000000..322c5f2
--- /dev/null
@@ -0,0 +1,22 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+// Information about this assembly is defined by the following attributes.
+// Change them to the values specific to your project.
+[assembly: AssemblyTitle("FreeCSharp")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("gumartinm.name")]
+[assembly: AssemblyProduct("")]
+[assembly: AssemblyCopyright("gumartinm.name")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
+// The form "{Major}.{Minor}.*" will automatically update the build and revision,
+// and "{Major}.{Minor}.{Build}.*" will update just the revision.
+[assembly: AssemblyVersion("1.0.*")]
+// The following attributes are used to specify the signing key for the assembly,
+// if desired. See the Mono documentation for more information about signing.
+//[assembly: AssemblyDelaySign(false)]
+//[assembly: AssemblyKeyFile("")]
+