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. |
From: <br...@us...> - 2009-02-03 22:50:37
|
Revision: 483 http://acmcontester.svn.sourceforge.net/acmcontester/?rev=483&view=rev Author: brus07 Date: 2009-02-03 22:50:29 +0000 (Tue, 03 Feb 2009) Log Message: ----------- Added HtmlBuilder for Build html page to LibraryExtention. Modified Paths: -------------- ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj Added Paths: ----------- ACMServer/trunk/ACMServer/Library/LibraryExtention/HtmlBuilder.cs Added: ACMServer/trunk/ACMServer/Library/LibraryExtention/HtmlBuilder.cs =================================================================== --- ACMServer/trunk/ACMServer/Library/LibraryExtention/HtmlBuilder.cs (rev 0) +++ ACMServer/trunk/ACMServer/Library/LibraryExtention/HtmlBuilder.cs 2009-02-03 22:50:29 UTC (rev 483) @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace AcmContester.Library.LibraryExtention +{ + public class HtmlBuilder + { + private string title; + private string body; + private string bgcolor; + + public string Title + { + get + { + return title; + } + set + { + title = value; + } + } + + public string BackgroundColor + { + get + { + return bgcolor; + } + set + { + bgcolor = value; + } + } + + public string Result + { + get + { + string res = "<html>\n<head>\n\t<title>" + title + "</title>\n</head>"; + res = res + "<body bgcolor=\"" + bgcolor + ">\n" + body + "</body>\n</html>"; + return res; + } + } + + public void AddTable(string[,] data,string[] header,string caption) + { + body += "<table>\n"; + if (caption!="") + body+="<caption>" + caption + "</caption>\n"; + if (header.Length>0) + { + body+="<tr>"; + for (int i = 0; i < header.Length; i++) + body += "<th>" + header[i] + "</th>"; + body+="\n"; + } + for (int i=0;i<data.GetLength(0);i++) + { + body += "<tr>"; + for (int j = 0; j < data.GetLength(1); j++) + body += "<td>" + data[i,j] + "</td>"; + body += "</tr>\n"; + } + body += "</table>"; + } + + public void AddReference(string URL, string text) + { + body += "<a href=\"" + URL + "\">" + text + "</a> "; + } + + public void AddReference(string URL, string text, string align) + { + body += "<a align=\""+align+"\" href=\"" + URL + "\">" + text + "</a> "; + } + + public void AddParagraph(string data, string align, string color) + { + body += "<p align=\"" + align + "\" color=\"" + color + "\" >" + data + "</p>\n"; + } + + public void AddParagraph(string data, string align) + { + body += "<p align=\"" + align + "\" >" + data + "</p>\n"; + } + + public void AddParagraph(string data) + { + body += "<p>" + data + "</p>\n"; + } + + public void AddHorizLine() + { + body+="\n<hr>\n"; + } + + public void AddList(string[] data, bool ordered) + { + if (ordered) + body += "<ol>\n"; + else + body += "<ul>\n"; + for (int i = 0; i < data.Length; i++) + body += "<li>" + data[i] + "\n"; + if (ordered) + body += "</ol>\n"; + else + body += "</ul>\n"; + } + + public void AddText(string data) + { + body += data; + } + + public void AddCode(string data) + { + body += "<code>" + data + "</code>\n"; + } + + public void AddHeader(string data, int size, string align) //size from 1 to 6 + { + body += "<h" + size.ToString() + " align=\"" + align + "\">" + data + "</h" + size.ToString() + ">\n"; + } + } +} Modified: ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj =================================================================== --- ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj 2009-02-03 11:33:16 UTC (rev 482) +++ ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj 2009-02-03 22:50:29 UTC (rev 483) @@ -34,6 +34,7 @@ </ItemGroup> <ItemGroup> <Compile Include="Configuration.cs" /> + <Compile Include="HtmlBuilder.cs" /> <Compile Include="IniFile.cs" /> <Compile Include="Log.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> @@ -51,4 +52,4 @@ <Target Name="AfterBuild"> </Target> --> -</Project> +</Project> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |