From: <br...@us...> - 2008-10-14 20:29:16
|
Revision: 416 http://acmcontester.svn.sourceforge.net/acmcontester/?rev=416&view=rev Author: brus07 Date: 2008-10-14 20:29:04 +0000 (Tue, 14 Oct 2008) Log Message: ----------- Added class for work with IniFiles. (not tested) Modified Paths: -------------- ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj Added Paths: ----------- ACMServer/trunk/ACMServer/Library/LibraryExtention/IniFile.cs Added: ACMServer/trunk/ACMServer/Library/LibraryExtention/IniFile.cs =================================================================== --- ACMServer/trunk/ACMServer/Library/LibraryExtention/IniFile.cs (rev 0) +++ ACMServer/trunk/ACMServer/Library/LibraryExtention/IniFile.cs 2008-10-14 20:29:04 UTC (rev 416) @@ -0,0 +1,96 @@ +using System; +using System.IO; +using System.Text; +using System.Runtime.InteropServices; + +namespace AcmContester.Library.LibraryExtention +{ + /// <summary> + /// Class to read from INI files. Based loosely on the Delphi class of the same name. + /// </summary> + public class IniFile + { + private string fileName; + + /// <summary> + /// Creates a new <see cref="IniFile"/> instance. + /// </summary> + /// <param name="fileName">Name of the INI file.</param> + public IniFile(string fileName) + { + if (!File.Exists(fileName)) + throw new FileNotFoundException(fileName + " does not exist", fileName); + this.fileName = fileName; + } + + // native methods + [DllImport("kernel32")] + private static extern int GetPrivateProfileString(string section, + string key, string def, StringBuilder retVal, int size, string filePath); + + [DllImport("kernel32")] + private static extern int GetPrivateProfileSection(string section, IntPtr lpReturnedString, + int nSize, string lpFileName); + + [DllImport("kernel32")] + private static extern long WritePrivateProfileString(string section, + string key, string val, string filePath); + + + /// <summary> + /// Reads a string value from the INI file. + /// </summary> + /// <param name="section">Section to read.</param> + /// <param name="key">Key to read.</param> + public string ReadString(string section, string key) + { + const int bufferSize = 255; + //TODO: jaksho bilshe za bufferSize todi Exception + StringBuilder temp = new StringBuilder(bufferSize); + GetPrivateProfileString(section, key, "", temp, bufferSize, fileName); + return temp.ToString(); + } + + /// <summary> + /// Reads a whole section of the INI file. + /// </summary> + /// <param name="section">Section to read.</param> + public string[] ReadSection(string section) + { + const int bufferSize = 2048; + //TODO: jaksho bilshe za bufferSize todi Exception + + StringBuilder returnedString = new StringBuilder(); + + IntPtr pReturnedString = Marshal.AllocCoTaskMem(bufferSize); + try + { + int bytesReturned = GetPrivateProfileSection(section, pReturnedString, bufferSize, fileName); + + //bytesReturned -1 to remove trailing \0 + for (int i = 0; i < bytesReturned - 1; i++) + returnedString.Append((char)Marshal.ReadByte(new IntPtr((uint)pReturnedString + (uint)i))); + } + finally + { + Marshal.FreeCoTaskMem(pReturnedString); + } + + string sectionData = returnedString.ToString(); + return sectionData.Split('\0'); + } + + /// <summary> + /// Write Data to the INI File + /// </summary> + /// <PARAM name="Section"><Section name/PARAM> + /// <PARAM name="Key">Key Name</PARAM> + /// <PARAM name="Value">Value Name</PARAM> + public void WriteString(string section, string key, string value) + { + WritePrivateProfileString(section, key, value, fileName); + } + + } + +} Modified: ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj =================================================================== --- ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj 2008-10-13 18:19:46 UTC (rev 415) +++ ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj 2008-10-14 20:29:04 UTC (rev 416) @@ -33,6 +33,7 @@ <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> + <Compile Include="IniFile.cs" /> <Compile Include="Log.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Reader.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |