Migration

This commit is contained in:
Ray 2019-02-14 19:47:16 +00:00
commit 793d2347f4
4 changed files with 618 additions and 0 deletions

86
2016/04/ParseCSVLine.txt Normal file
View File

@ -0,0 +1,86 @@
/// <summary>
/// Parse CSV line into string array.
/// </summary>
/// <param name="line">CSV line.</param>
/// <returns>List of items in CSV.</returns>
protected string[] ParseCSVLine(string line)
{
string[] rv = new string[0];
int quotCount = 0;
bool ignoreNext = false;
bool hadQuot = false;
int startPos = 0;
for (int i = 0; i < line.Length; i++)
{
// ignore char
if (ignoreNext)
{
ignoreNext = false;
continue;
}
// ignore next char
if (line[i].Equals('\\'))
{
ignoreNext = true;
continue;
}
// push quot
if (line[i].Equals('"'))
{
hadQuot = true;
if (quotCount <= 0)
{
quotCount++;
}
else
{
quotCount--;
}
continue;
}
if (line[i].Equals(','))
{
if (quotCount <= 0)
{
string cell = line.Substring(startPos, (i - startPos));
if (hadQuot)
{
Array.Resize(ref rv, (rv.Length + 1));
rv[(rv.Length - 1)] = cell.Substring(1, (cell.Length - 2));
}
else
{
Array.Resize(ref rv, (rv.Length + 1));
rv[(rv.Length - 1)] = cell;
}
startPos = (i + 1);
hadQuot = false;
}
}
}
// last cell
if (startPos <= line.Length)
{
string cell = line.Substring(startPos, (line.Length - startPos));
if (hadQuot)
{
Array.Resize(ref rv, (rv.Length + 1));
rv[(rv.Length - 1)] = cell.Substring(1, (cell.Length - 2));
}
else
{
Array.Resize(ref rv, (rv.Length + 1));
rv[(rv.Length - 1)] = cell;
}
}
return rv;
}

View File

@ -0,0 +1,116 @@
/**
* BSHelper
* Bootstrap Helper
* @version v0.1.0.023 (2015/07/14 1157)
*/
/**
* BSDialog
*
*/
var BSDialog = {
Create:function(id, title, url, is_big){
id = "dlg" + id;
if ($("#" + id).length <= 0) this.Add(id, title, ((is_big == undefined)? false : is_big));
//--
$("#" + id).find(".modal-content").load(url);
$("#" + id).modal('show');
$("#" + id).on('hide.bs.modal', function(){
$("body > div[id='" + id + "']").remove();
$("body > div[class~='modal-backdrop']").remove();
$("body").removeClass("modal-open");
});
},
Add:function(id, title, is_big){
if ($("body > div#" + id).length > 0) return;
//--
var html = "";
html += "<div class=\"modal fade\" id=\"" + id + "\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"" + id + "Label\" aria-hidden=\"true\">";
html += " <div class=\"modal-dialog" + ((is_big)? " modal-lg" : "") + "\">";
html += " <div class=\"modal-content\">";
html += " <div class=\"modal-header\">";
html += " <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">&times;</button>";
html += " <strong class=\"modal-title\" id=\"" + id + "Label\">" + title + "</strong>";
html += " </div>";
html += " <div class=\"modal-body custom-loading\"></div>";
html += " <div class=\"modal-footer\">";
html += " <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>";
html += " </div>";
html += " </div>";
html += " </div>";
html += "</div>";
//--
$("body").append(html);
},
Close: function (id) {
if (typeof (id) == "object")
{
var sender = null;
do
{
if (typeof (id.target) != "undefined")
{
sender = id.target;
break;
}
if (typeof (id) != "undefined")
{
sender = id;
break;
}
} while (false);
if (sender == null) return;
var panel = $(sender).parentsUntil("div[id^='dlg']").parent();
if ($(panel).length > 0)
{
var dialogID = $(panel).attr("id").substr(3);
this.Close(dialogID);
}
} else {
if ($("body > div#dlg" + id).length <= 0) return;
$("body > div#dlg" + id).remove();
$("body > div[class~='modal-backdrop']").remove();
$("body").removeClass("modal-open");
}
},
Clear:function(){
$("body > div[class~='modal'][role='dialog']").remove();
$("body > div[class~='modal-backdrop']").remove();
$("body").removeClass("modal-open");
},
ShowToast:function(id, title, message, is_big){
if ($("body > div#" + id).length > 0) return;
//--
var html = "";
html += "<div class=\"modal fade\" id=\"" + id + "\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"" + id + "Label\" aria-hidden=\"true\">";
html += " <div class=\"modal-dialog" + ((is_big)? " modal-lg" : "") + "\">";
html += " <div class=\"modal-content\">";
html += " <div class=\"modal-header\">";
html += " <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">&times;</button>";
html += " <strong class=\"modal-title\" id=\"" + id + "Label\">" + title + "</strong>";
html += " </div>";
html += " <div class=\"modal-body\">" + message + "</div>";
html += " <div class=\"modal-footer\">";
html += " <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>";
html += " </div>";
html += " </div>";
html += " </div>";
html += "</div>";
//--
$("body").append(html);
//--
$("#" + id).modal('show');
$("#" + id).on('hide.bs.modal', function(){
$("body > div[id='" + id + "']").remove();
$("body > div[class~='modal-backdrop']").remove();
$("body").removeClass("modal-open");
});
}
};

168
2018/01/httpweb.cs Normal file
View File

@ -0,0 +1,168 @@
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
namespace RyzStudio.Net
{
public class HttpWeb
{
public string defaultUserAgent = "Momozilla/5.0 (" + Environment.OSVersion.Platform.ToString() + " ; " + Environment.OSVersion.VersionString + "; " + Application.CurrentCulture.TwoLetterISOLanguageName + ")";
public int defaultTimeout = 6000;
public int defaultMaxRedirect = 8;
public bool defaultAllowRedirect = true;
public CookieContainer defaultCookierContainer = null;
public HttpWeb()
{
}
public HttpWebRequest CreateRequest(string url)
{
return this.CreateRequest(url, url);
}
public HttpWebRequest CreateRequest(string url, string referrerURL)
{
if (defaultCookierContainer == null)
{
defaultCookierContainer = new CookieContainer();
}
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
webRequest.MaximumAutomaticRedirections = defaultMaxRedirect;
webRequest.CookieContainer = defaultCookierContainer;
webRequest.UserAgent = defaultUserAgent;
webRequest.AllowAutoRedirect = defaultAllowRedirect;
webRequest.Timeout = defaultTimeout;
return webRequest;
}
public int GetResponse(out string sourceCode, string url, string referrerURL = "")
{
HttpWebRequest webRequest = this.CreateRequest(url, referrerURL);
return GetResponse(out sourceCode, webRequest);
}
public int GetResponse(out string sourceCode, HttpWebRequest webRequest)
{
sourceCode = string.Empty;
int rv = 0;
try
{
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
rv = (int)webResponse.StatusCode;
StreamReader readContent = new StreamReader(webResponse.GetResponseStream());
sourceCode = readContent.ReadToEnd();
webResponse.Close();
webResponse = null;
}
catch (WebException xc)
{
if (xc.Response is HttpWebResponse)
{
HttpWebResponse rs = xc.Response as HttpWebResponse;
StreamReader readContent = new StreamReader(rs.GetResponseStream());
if (readContent != null)
{
sourceCode = readContent.ReadToEnd();
}
rv = (int)rs.StatusCode;
}
else
{
rv = (int)xc.Status;
sourceCode = xc.Message;
}
}
catch (Exception xc)
{
sourceCode = xc.Message;
}
return rv;
}
public static HttpWebRequest AddBasicAuthentication(HttpWebRequest webRequest, string username, string password)
{
webRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(string.Concat(username, ":", password)));
webRequest.PreAuthenticate = true;
return webRequest;
}
public int GetPOSTResponse(out string sourceCode, HttpWebRequest webRequest, string postData)
{
sourceCode = "";
int rv = 0;
byte[] buffer = Encoding.UTF8.GetBytes(postData);
webRequest.ContentLength = buffer.Length;
try
{
Stream dataStream = webRequest.GetRequestStream();
dataStream.Write(buffer, 0, buffer.Length);
dataStream.Close();
}
catch (Exception xc)
{
sourceCode = xc.Message;
return rv;
}
return this.GetResponse(out sourceCode, webRequest);
}
public int GetHeader(out WebHeaderCollection headerCollection, string url, string referrerURL = "")
{
headerCollection = null;
int rv = 0;
HttpWebRequest webRequest = this.CreateRequest(url, referrerURL);
webRequest.Method = "HEAD";
try
{
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
headerCollection = webResponse.Headers;
rv = (int)webResponse.StatusCode;
webResponse.Close();
webResponse = null;
}
catch (WebException xc)
{
if (xc.Response is HttpWebResponse)
{
HttpWebResponse rs = xc.Response as HttpWebResponse;
rv = (int)rs.StatusCode;
}
else
{
rv = (int)xc.Status;
}
}
catch (Exception xc)
{
// do nothing
}
return rv;
}
}
}

248
2019/02/Docxument.cs Normal file
View File

@ -0,0 +1,248 @@
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security;
using System.Text;
namespace HiImRay
{
public class Docxument
{
protected string Filename { get; set; }
public int CompressionLevel { get; set; } = 9;
public Dictionary<string, string> ReplacementWords = new Dictionary<string, string>();
public Dictionary<string, List<Dictionary<string, string>>> ReplacementParagraphs = new Dictionary<string, List<Dictionary<string, string>>>();
public Docxument()
{
}
public Docxument(string filename)
{
Load(filename);
}
public void Load(string filename)
{
this.Filename = filename;
}
public void Save(string saveFilename)
{
int size = 2048;
byte[] buffer = new byte[size];
int bufferSize = 0;
// read file
ZipEntry readEntry = null;
ZipInputStream readStream = new ZipInputStream(File.OpenRead(this.Filename));
// write file
ZipOutputStream writeStream = new ZipOutputStream(File.Create(saveFilename));
writeStream.SetLevel(this.CompressionLevel);
// loop
while (true)
{
readEntry = readStream.GetNextEntry();
if (readEntry == null)
{
break;
}
if (!readEntry.IsFile)
{
break;
}
if (string.IsNullOrEmpty(readEntry.Name))
{
break;
}
// change document
if (readEntry.Name.Equals("word/document.xml"))
{
MemoryStream ms = new MemoryStream();
buffer = new byte[size];
bufferSize = 0;
do
{
bufferSize = readStream.Read(buffer, 0, buffer.Length);
ms.Write(buffer, 0, bufferSize);
} while (bufferSize > 0);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
StringBuilder sb = new StringBuilder();
sb.Append(sr.ReadToEnd());
// make changes
sb = replaceTokens(sb, this.ReplacementWords);
sb = replaceParagraphs(sb, this.ReplacementParagraphs);
// make readable
MemoryStream ms2 = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(sb.ToString()));
ms2.Position = 0;
// write new document xml
writeStream.PutNextEntry(new ZipEntry(readEntry.Name));
buffer = new byte[size];
bufferSize = 0;
do
{
bufferSize = ms2.Read(buffer, 0, buffer.Length);
writeStream.Write(buffer, 0, bufferSize);
} while (bufferSize > 0);
ms2.Close();
ms2.Dispose();
}
else
{
writeStream.PutNextEntry(new ZipEntry(readEntry.Name));
buffer = new byte[size];
bufferSize = 0;
do
{
bufferSize = readStream.Read(buffer, 0, buffer.Length);
writeStream.Write(buffer, 0, bufferSize);
} while (bufferSize > 0);
}
}
writeStream.Finish();
writeStream.Flush();
writeStream.Close();
writeStream.Dispose();
readStream.Close();
readStream.Dispose();
}
protected StringBuilder replaceTokens(StringBuilder sb, Dictionary<string, string> options)
{
foreach (KeyValuePair<string, string> item in options)
{
sb.Replace("{{" + item.Key + "}}", SecurityElement.Escape(item.Value));
}
return sb;
}
protected StringBuilder replaceParagraphs(StringBuilder sb, Dictionary<string, List<Dictionary<string, string>>> options)
{
foreach (KeyValuePair<string, List<Dictionary<string, string>>> item in options)
{
sb = replaceParagraph(sb, item);
}
return sb;
}
protected StringBuilder replaceParagraph(StringBuilder sb, KeyValuePair<string, List<Dictionary<string, string>>> options)
{
string paragraph = sb.ToString();
Tuple<int, int> outerCoord = getOuterParagraph(paragraph, "{{" + options.Key + "}}");
if (outerCoord != null)
{
sb.Remove(outerCoord.Item1, outerCoord.Item2);
Tuple<int, int> innerCoord = getInnerParagraph(paragraph.Substring(outerCoord.Item1, outerCoord.Item2), "{{" + options.Key + "}}");
string innerParagraph = paragraph.Substring((innerCoord.Item1 + outerCoord.Item1), innerCoord.Item2);
StringBuilder innerText = new StringBuilder();
foreach (Dictionary<string, string> row in options.Value)
{
StringBuilder sb2 = new StringBuilder();
sb2.Append(innerParagraph);
sb2 = replaceTokens(sb2, row);
innerText.Append(sb2.ToString());
}
sb.Insert(outerCoord.Item1, innerText.ToString());
}
return sb;
}
protected Tuple<int, int> getOuterParagraph(string fullText, string findTerm)
{
string headTerm = "<w:p ";
string tailTerm = "</w:p>";
int headIndex = fullText.IndexOf(findTerm);
if (headIndex < 0)
{
return null;
}
int tailIndex = fullText.IndexOf(findTerm, (headIndex + findTerm.Length));
if (tailIndex < 0)
{
return null;
}
headIndex = fullText.LastIndexOf(headTerm, headIndex);
if (headIndex < 0)
{
return null;
}
tailIndex = fullText.IndexOf(tailTerm, (tailIndex + tailTerm.Length));
if (tailIndex < 0)
{
return null;
}
tailIndex += tailTerm.Length;
return new Tuple<int, int>(headIndex, (tailIndex - headIndex));
}
protected Tuple<int, int> getInnerParagraph(string fullText, string findTerm)
{
string headTerm = "<w:p ";
string tailTerm = "</w:p>";
int headIndex = fullText.IndexOf(findTerm);
if (headIndex < 0)
{
return null;
}
int tailIndex = fullText.IndexOf(findTerm, (headIndex + findTerm.Length));
if (tailIndex < 0)
{
return null;
}
headIndex = fullText.IndexOf(tailTerm, headIndex);
if (headIndex < 0)
{
return null;
}
headIndex += tailTerm.Length;
tailIndex = fullText.LastIndexOf(headTerm, tailIndex);
if (tailIndex < 0)
{
return null;
}
return new Tuple<int, int>(headIndex, (tailIndex - headIndex));
}
}
}