PageRenderTime 68ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/src/BuildUtil/CoreUtil/Str.cs

https://github.com/NealSCarffery/SoftEtherVPN
C# | 4392 lines | 4048 code | 239 blank | 105 comment | 524 complexity | 4001b5b40ffd11e9a67a41f2a45fbbf4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
  1. // CoreUtil
  2. //
  3. // Copyright (C) 2012-2014 Daiyuu Nobori. All Rights Reserved.
  4. // Copyright (C) 2012-2014 SoftEther VPN Project at University of Tsukuba. All Rights Reserved.
  5. // Comments: Tetsuo Sugiyama, Ph.D.
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // version 2 as published by the Free Software Foundation.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License version 2
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  29. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  30. //
  31. //
  32. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  33. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  34. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  35. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  36. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  37. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  38. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  39. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  40. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  41. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  42. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  43. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  44. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  45. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  46. //
  47. // USE ONLY IN JAPAN. DO NOT USE IT IN OTHER COUNTRIES. IMPORTING THIS
  48. // SOFTWARE INTO OTHER COUNTRIES IS AT YOUR OWN RISK. SOME COUNTRIES
  49. // PROHIBIT ENCRYPTED COMMUNICATIONS. USING THIS SOFTWARE IN OTHER
  50. // COUNTRIES MIGHT BE RESTRICTED.
  51. //
  52. //
  53. // SOURCE CODE CONTRIBUTION
  54. // ------------------------
  55. //
  56. // Your contribution to SoftEther VPN Project is much appreciated.
  57. // Please send patches to us through GitHub.
  58. // Read the SoftEther VPN Patch Acceptance Policy in advance:
  59. // http://www.softether.org/5-download/src/9.patch
  60. //
  61. //
  62. // DEAR SECURITY EXPERTS
  63. // ---------------------
  64. //
  65. // If you find a bug or a security vulnerability please kindly inform us
  66. // about the problem immediately so that we can fix the security problem
  67. // to protect a lot of users around the world as soon as possible.
  68. //
  69. // Our e-mail address for security reports is:
  70. // softether-vpn-security [at] softether.org
  71. //
  72. // Please note that the above e-mail address is not a technical support
  73. // inquiry address. If you need technical assistance, please visit
  74. // http://www.softether.org/ and ask your question on the users forum.
  75. //
  76. // Thank you for your cooperation.
  77. //
  78. //
  79. // NO MEMORY OR RESOURCE LEAKS
  80. // ---------------------------
  81. //
  82. // The memory-leaks and resource-leaks verification under the stress
  83. // test has been passed before release this source code.
  84. using System;
  85. using System.Threading;
  86. using System.Data;
  87. using System.Data.Sql;
  88. using System.Data.SqlClient;
  89. using System.Data.SqlTypes;
  90. using System.Text;
  91. using System.Configuration;
  92. using System.Collections;
  93. using System.Collections.Generic;
  94. using System.Security.Cryptography;
  95. using System.Web;
  96. using System.Web.Security;
  97. using System.Web.UI;
  98. using System.Web.UI.WebControls;
  99. using System.Web.UI.WebControls.WebParts;
  100. using System.Web.UI.HtmlControls;
  101. using System.IO;
  102. using System.Drawing;
  103. using System.Drawing.Imaging;
  104. using System.Drawing.Drawing2D;
  105. using System.Runtime.Serialization.Formatters.Soap;
  106. using System.Runtime.Serialization.Formatters.Binary;
  107. using System.Xml;
  108. using System.Xml.Serialization;
  109. using System.Web.Mail;
  110. using System.Runtime.InteropServices;
  111. namespace CoreUtil
  112. {
  113. [FlagsAttribute]
  114. public enum PrintFFLags
  115. {
  116. Minus = 1,
  117. Plus = 2,
  118. Zero = 4,
  119. Blank = 8,
  120. Sharp = 16,
  121. }
  122. public class PrintFParsedParam
  123. {
  124. public bool Ok = false;
  125. public readonly PrintFFLags Flags = 0;
  126. public readonly int Width = 0;
  127. public readonly int Precision = 0;
  128. public readonly bool NoPrecision = true;
  129. public readonly string Type = "";
  130. static PrintFFLags charToFlag(char c)
  131. {
  132. switch (c)
  133. {
  134. case '-':
  135. return PrintFFLags.Minus;
  136. case '+':
  137. return PrintFFLags.Plus;
  138. case '0':
  139. return PrintFFLags.Zero;
  140. case ' ':
  141. return PrintFFLags.Blank;
  142. case '#':
  143. return PrintFFLags.Sharp;
  144. }
  145. return 0;
  146. }
  147. public string GetString(object param)
  148. {
  149. int i;
  150. StringBuilder sb;
  151. string tmp = "(error)";
  152. double f;
  153. bool signed = false;
  154. switch (this.Type)
  155. {
  156. case "c":
  157. case "C":
  158. if (param is char)
  159. {
  160. tmp += (char)param;
  161. }
  162. else if (param is string)
  163. {
  164. string s = (string)param;
  165. if (s.Length >= 1)
  166. {
  167. tmp += s[0];
  168. }
  169. }
  170. break;
  171. case "d":
  172. case "i":
  173. sb = new StringBuilder();
  174. int count = this.Width;
  175. if (this.Precision != 0)
  176. {
  177. count = this.Precision;
  178. }
  179. for (i = 1; i < this.Precision; i++)
  180. {
  181. sb.Append("#");
  182. }
  183. sb.Append("0");
  184. if (param is int)
  185. {
  186. tmp = ((int)param).ToString(sb.ToString());
  187. }
  188. else if (param is long)
  189. {
  190. tmp = ((long)param).ToString(sb.ToString());
  191. }
  192. else if (param is uint)
  193. {
  194. tmp = ((int)((uint)param)).ToString(sb.ToString());
  195. }
  196. else if (param is ulong)
  197. {
  198. tmp = ((long)((ulong)param)).ToString(sb.ToString());
  199. }
  200. else if (param is decimal)
  201. {
  202. tmp = ((decimal)param).ToString(sb.ToString());
  203. }
  204. signed = true;
  205. break;
  206. case "u":
  207. sb = new StringBuilder();
  208. for (i = 1; i < this.Precision; i++)
  209. {
  210. sb.Append("#");
  211. }
  212. sb.Append("0");
  213. if (param is int)
  214. {
  215. tmp = ((uint)((int)param)).ToString(sb.ToString());
  216. }
  217. else if (param is long)
  218. {
  219. tmp = ((ulong)((long)param)).ToString(sb.ToString());
  220. }
  221. else if (param is uint)
  222. {
  223. tmp = ((uint)param).ToString(sb.ToString());
  224. }
  225. else if (param is ulong)
  226. {
  227. tmp = ((ulong)param).ToString(sb.ToString());
  228. }
  229. else if (param is decimal)
  230. {
  231. tmp = ((decimal)param).ToString(sb.ToString());
  232. }
  233. break;
  234. case "x":
  235. case "X":
  236. sb = new StringBuilder();
  237. sb.Append(this.Type);
  238. sb.Append(this.Precision.ToString());
  239. if (param is int)
  240. {
  241. tmp = ((uint)((int)param)).ToString(sb.ToString());
  242. }
  243. else if (param is long)
  244. {
  245. tmp = ((ulong)((long)param)).ToString(sb.ToString());
  246. }
  247. else if (param is uint)
  248. {
  249. tmp = ((uint)param).ToString(sb.ToString());
  250. }
  251. else if (param is ulong)
  252. {
  253. tmp = ((ulong)param).ToString(sb.ToString());
  254. }
  255. break;
  256. case "e":
  257. case "E":
  258. case "f":
  259. f = 0;
  260. if (param is int)
  261. {
  262. f = (double)((int)param);
  263. }
  264. else if (param is long)
  265. {
  266. f = (double)((long)param);
  267. }
  268. else if (param is uint)
  269. {
  270. f = (double)((uint)param);
  271. }
  272. else if (param is ulong)
  273. {
  274. f = (double)((ulong)param);
  275. }
  276. else if (param is decimal)
  277. {
  278. f = (double)((long)param);
  279. }
  280. else if (param is float)
  281. {
  282. f = (double)((float)param);
  283. }
  284. else if (param is double)
  285. {
  286. f = (double)param;
  287. }
  288. else
  289. {
  290. break;
  291. }
  292. int prectmp = Precision;
  293. if (prectmp == 0 && NoPrecision)
  294. {
  295. prectmp = 6;
  296. }
  297. tmp = f.ToString(string.Format("{0}{1}", Type, prectmp));
  298. break;
  299. case "s":
  300. case "S":
  301. if (param == null)
  302. {
  303. tmp = "(null)";
  304. }
  305. else
  306. {
  307. tmp = param.ToString();
  308. }
  309. break;
  310. }
  311. int normalWidth = Str.GetStrWidth(tmp);
  312. int targetWidth = Math.Max(this.Width, normalWidth);
  313. if ((this.Flags & PrintFFLags.Plus) != 0)
  314. {
  315. if (signed)
  316. {
  317. if (tmp.StartsWith("-") == false)
  318. {
  319. tmp = "+" + tmp;
  320. }
  321. }
  322. }
  323. else
  324. {
  325. if ((this.Flags & PrintFFLags.Blank) != 0)
  326. {
  327. if (signed)
  328. {
  329. if (tmp.StartsWith("-") == false)
  330. {
  331. tmp = " " + tmp;
  332. }
  333. }
  334. }
  335. }
  336. if ((this.Flags & PrintFFLags.Minus) != 0)
  337. {
  338. int w = targetWidth - Str.GetStrWidth(tmp);
  339. if (w < 0)
  340. {
  341. w = 0;
  342. }
  343. tmp += Str.MakeCharArray(' ', w);
  344. }
  345. else if ((this.Flags & PrintFFLags.Zero) != 0)
  346. {
  347. int w = targetWidth - Str.GetStrWidth(tmp);
  348. if (w < 0)
  349. {
  350. w = 0;
  351. }
  352. tmp = Str.MakeCharArray('0', w) + tmp;
  353. }
  354. else
  355. {
  356. int w = targetWidth - Str.GetStrWidth(tmp);
  357. if (w < 0)
  358. {
  359. w = 0;
  360. }
  361. tmp = Str.MakeCharArray(' ', w) + tmp;
  362. }
  363. if ((this.Flags & PrintFFLags.Sharp) != 0)
  364. {
  365. if (Type == "x" || Type == "X")
  366. {
  367. tmp = "0x" + tmp;
  368. }
  369. }
  370. return tmp;
  371. }
  372. public PrintFParsedParam(string str)
  373. {
  374. Str.NormalizeString(ref str);
  375. if (str.StartsWith("%") == false)
  376. {
  377. return;
  378. }
  379. str = str.Substring(1);
  380. Queue<char> q = new Queue<char>();
  381. foreach (char c in str)
  382. {
  383. q.Enqueue(c);
  384. }
  385. while (q.Count >= 1)
  386. {
  387. char c = q.Peek();
  388. PrintFFLags f = charToFlag(c);
  389. if (f == 0)
  390. {
  391. break;
  392. }
  393. this.Flags |= f;
  394. q.Dequeue();
  395. }
  396. Queue<char> q2 = new Queue<char>();
  397. while (q.Count >= 1)
  398. {
  399. bool bf = false;
  400. char c = q.Peek();
  401. switch (c)
  402. {
  403. case 'h':
  404. case 'l':
  405. case 'I':
  406. case 'c':
  407. case 'C':
  408. case 'd':
  409. case 'i':
  410. case 'o':
  411. case 'u':
  412. case 'x':
  413. case 'X':
  414. case 'e':
  415. case 'E':
  416. case 'f':
  417. case 'g':
  418. case 'G':
  419. case 'n':
  420. case 'p':
  421. case 's':
  422. case 'S':
  423. bf = true;
  424. break;
  425. default:
  426. q2.Enqueue(c);
  427. break;
  428. }
  429. if (bf)
  430. {
  431. break;
  432. }
  433. q.Dequeue();
  434. }
  435. string[] widthAndPrec = (new string(q2.ToArray())).Split('.');
  436. if (widthAndPrec.Length == 1)
  437. {
  438. this.Width = Str.StrToInt(widthAndPrec[0]);
  439. }
  440. else if (widthAndPrec.Length == 2)
  441. {
  442. this.Width = Str.StrToInt(widthAndPrec[0]);
  443. this.Precision = Str.StrToInt(widthAndPrec[1]);
  444. this.NoPrecision = false;
  445. }
  446. this.Width = Math.Max(this.Width, 0);
  447. this.Precision = Math.Max(this.Precision, 0);
  448. while (q.Count >= 1)
  449. {
  450. char c = q.Peek();
  451. bool bf = false;
  452. switch (c)
  453. {
  454. case 'c':
  455. case 'C':
  456. case 'd':
  457. case 'i':
  458. case 'o':
  459. case 'u':
  460. case 'x':
  461. case 'X':
  462. case 'e':
  463. case 'E':
  464. case 'f':
  465. case 'g':
  466. case 'G':
  467. case 'a':
  468. case 'A':
  469. case 'n':
  470. case 'p':
  471. case 's':
  472. case 'S':
  473. bf = true;
  474. break;
  475. default:
  476. break;
  477. }
  478. if (bf)
  479. {
  480. break;
  481. }
  482. q.Dequeue();
  483. }
  484. this.Type = new string(q.ToArray());
  485. if (this.Type.Length >= 1)
  486. {
  487. this.Type = this.Type.Substring(0, 1);
  488. }
  489. this.Ok = (Str.IsEmptyStr(this.Type) == false);
  490. }
  491. }
  492. public class StrEqualityComparer : IEqualityComparer<string>
  493. {
  494. bool caseSensitive;
  495. public StrEqualityComparer()
  496. {
  497. this.caseSensitive = false;
  498. }
  499. public StrEqualityComparer(bool caseSensitive)
  500. {
  501. this.caseSensitive = caseSensitive;
  502. }
  503. public bool Equals(string x, string y)
  504. {
  505. return x.Equals(y, caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase);
  506. }
  507. public int GetHashCode(string obj)
  508. {
  509. return obj.GetHashCode();
  510. }
  511. }
  512. public class StrComparer : IComparer<string>
  513. {
  514. bool caseSensitive;
  515. public StrComparer()
  516. {
  517. this.caseSensitive = false;
  518. }
  519. public StrComparer(bool caseSensitive)
  520. {
  521. this.caseSensitive = caseSensitive;
  522. }
  523. public int Compare(string x, string y)
  524. {
  525. return string.Compare(x, y, !caseSensitive);
  526. }
  527. }
  528. public delegate bool RemoveStringFunction(string str);
  529. public static class Str
  530. {
  531. public static string NormalizeStrSoftEther(string str)
  532. {
  533. return NormalizeStrSoftEther(str, false);
  534. }
  535. public static string NormalizeStrSoftEther(string str, bool trim)
  536. {
  537. bool b = false;
  538. StringReader sr = new StringReader(str);
  539. StringWriter sw = new StringWriter();
  540. while (true)
  541. {
  542. string line = sr.ReadLine();
  543. if (line == null)
  544. {
  545. break;
  546. }
  547. if (b)
  548. {
  549. sw.WriteLine();
  550. }
  551. b = true;
  552. line = normalizeStrSoftEtherInternal(line);
  553. sw.Write(line);
  554. }
  555. int len = str.Length;
  556. try
  557. {
  558. if (str[len - 1] == '\n' || str[len - 1] == '\r')
  559. {
  560. sw.WriteLine();
  561. }
  562. }
  563. catch
  564. {
  565. }
  566. str = sw.ToString();
  567. if (trim)
  568. {
  569. str = str.Trim();
  570. }
  571. return str;
  572. }
  573. static string normalizeStrSoftEtherInternal(string str)
  574. {
  575. if (str.Trim().Length == 0)
  576. {
  577. return "";
  578. }
  579. int i;
  580. StringBuilder sb1 = new StringBuilder();
  581. for (i = 0; i < str.Length; i++)
  582. {
  583. char c = str[i];
  584. if (c == ' ' || c == ' ' || c == '\t')
  585. {
  586. sb1.Append(c);
  587. }
  588. else
  589. {
  590. break;
  591. }
  592. }
  593. string str2 = str.Substring(i).Trim();
  594. string str1 = sb1.ToString();
  595. str1 = ReplaceStr(str1, " ", " ");
  596. str1 = ReplaceStr(str1, "\t", " ");
  597. return str1 + normalizeStrSoftEtherInternal2(str2);
  598. }
  599. static string normalizeStrSoftEtherInternal2(string str)
  600. {
  601. NormalizeString(ref str, true, true, false, true);
  602. char[] chars = str.ToCharArray();
  603. StringBuilder sb = new StringBuilder();
  604. int i;
  605. for (i = 0; i < chars.Length; i++)
  606. {
  607. char c = chars[i];
  608. bool insert_space = false;
  609. bool insert_space2 = false;
  610. char c1 = (char)0;
  611. if (i >= 1)
  612. {
  613. c1 = chars[i - 1];
  614. }
  615. char c2 = (char)0;
  616. if (i < (chars.Length - 1))
  617. {
  618. c2 = chars[i + 1];
  619. }
  620. if (c == '\'' || c1 == '\'' || c2 == '\'' || c == '\"' || c1 == '\"' || c2 == '\"' || c == '>' || c1 == '>' || c2 == '>' || c == '<' || c1 == '<' || c2 == '<')
  621. {
  622. }
  623. else if (c == '(' || c == '[' || c == '{' || c == '<')
  624. {
  625. if (c1 != '「' && c1 != '『' && c1 != '。' && c1 != '、' && c1 != '・')
  626. {
  627. insert_space = true;
  628. }
  629. }
  630. else if (c == ')' || c == ']' || c == '}' || c == '>')
  631. {
  632. if (c2 != '.' && c2 != ',' && c2 != '。' && c2 != '、')
  633. {
  634. insert_space2 = true;
  635. }
  636. }
  637. else if (c == '~')
  638. {
  639. if (c1 != '~')
  640. {
  641. insert_space = true;
  642. }
  643. if (c2 != '~')
  644. {
  645. insert_space2 = true;
  646. }
  647. }
  648. else if (IsZenkaku(c) == false)
  649. {
  650. if (IsZenkaku(c1))
  651. {
  652. if (c != '.' && c != ',' && c != ';' && c != ':' && c1 != '※' && c1 != '〒' && c1 != '℡' && c1 != '「' && c1 != '『' && c1 != '。' && c1 != '、' && c1 != '・')
  653. {
  654. insert_space = true;
  655. }
  656. }
  657. }
  658. else
  659. {
  660. if (IsZenkaku(c1) == false)
  661. {
  662. if (c != '。' && c != '、' && c != '」' && c != '』' && c != '・' && c1 != '(' && c1 != '[' && c1 != '{' && c1 != '<' && c1 != ';' && c1 != ':')
  663. {
  664. insert_space = true;
  665. }
  666. }
  667. }
  668. if (insert_space)
  669. {
  670. sb.Append(' ');
  671. }
  672. sb.Append(c);
  673. if (insert_space2)
  674. {
  675. sb.Append(' ');
  676. }
  677. }
  678. str = sb.ToString();
  679. NormalizeString(ref str, true, true, false, true);
  680. return str;
  681. }
  682. public static bool IsZenkaku(char c)
  683. {
  684. return !((c >= (char)0) && (c <= (char)256));
  685. }
  686. public static string[] DivideStringMulti(string str, bool caseSensitive, params string[] keywords)
  687. {
  688. List<string> ret = new List<string>();
  689. int next = 0;
  690. while (true)
  691. {
  692. int foundIndex;
  693. string foundKeyword;
  694. int r = Str.SearchStrMulti(str, next, caseSensitive, out foundIndex, out foundKeyword, keywords);
  695. if (r == -1)
  696. {
  697. ret.Add(str.Substring(next));
  698. break;
  699. }
  700. else
  701. {
  702. ret.Add(str.Substring(next, r - next));
  703. ret.Add(foundKeyword);
  704. next = r + foundKeyword.Length;
  705. }
  706. }
  707. return ret.ToArray();
  708. }
  709. public static bool IsSuitableEncodingForString(string str, Encoding enc)
  710. {
  711. try
  712. {
  713. str = Str.NormalizeCrlf(str);
  714. byte[] utf1 = Str.Utf8Encoding.GetBytes(str);
  715. byte[] b = enc.GetBytes(str);
  716. string str2 = enc.GetString(b);
  717. byte[] utf2 = Str.Utf8Encoding.GetBytes(str2);
  718. return Util.CompareByte(utf1, utf2);
  719. }
  720. catch
  721. {
  722. return false;
  723. }
  724. }
  725. public static bool IsCharNumOrAlpha(char c)
  726. {
  727. if (c >= 'a' && c <= 'z')
  728. {
  729. return true;
  730. }
  731. if (c >= 'A' && c <= 'Z')
  732. {
  733. return true;
  734. }
  735. if (c >= '0' && c <= '9')
  736. {
  737. return true;
  738. }
  739. return false;
  740. }
  741. public static bool IsStringNumOrAlpha(string s)
  742. {
  743. foreach (char c in s)
  744. {
  745. if (IsCharNumOrAlpha(c) == false)
  746. {
  747. return false;
  748. }
  749. }
  750. return true;
  751. }
  752. public static string[] StrToStrLineBySplitting(string str)
  753. {
  754. StringReader r = new StringReader(str);
  755. List<string> ret = new List<string>();
  756. while (true)
  757. {
  758. string line = r.ReadLine();
  759. if (line == null)
  760. {
  761. break;
  762. }
  763. if (IsEmptyStr(line) == false)
  764. {
  765. ret.Add(line.Trim());
  766. }
  767. }
  768. return ret.ToArray();
  769. }
  770. public static string GetLeft(string str, int len)
  771. {
  772. if (str == null)
  773. {
  774. return null;
  775. }
  776. if (str.Length > len)
  777. {
  778. return str.Substring(0, len);
  779. }
  780. else
  781. {
  782. return str;
  783. }
  784. }
  785. public static string[] SplitStringForSearch(string str)
  786. {
  787. bool b = false;
  788. int i, len;
  789. len = str.Length;
  790. List<string> ret = new List<string>();
  791. string currentStr = "";
  792. for (i = 0; i < len; i++)
  793. {
  794. char c = str[i];
  795. if (c == '\"')
  796. {
  797. b = !b;
  798. if (b == false)
  799. {
  800. currentStr = currentStr.Trim();
  801. if (Str.IsEmptyStr(currentStr) == false)
  802. {
  803. ret.Add(currentStr);
  804. currentStr = "";
  805. }
  806. }
  807. }
  808. else
  809. {
  810. if (b == false && (c == ' ' || c == ' ' || c == '\t'))
  811. {
  812. currentStr = currentStr.Trim();
  813. if (Str.IsEmptyStr(currentStr) == false)
  814. {
  815. ret.Add(currentStr);
  816. currentStr = "";
  817. }
  818. }
  819. else
  820. {
  821. currentStr += c;
  822. }
  823. }
  824. }
  825. currentStr = currentStr.Trim();
  826. if (Str.IsEmptyStr(currentStr) == false)
  827. {
  828. ret.Add(currentStr);
  829. }
  830. return ret.ToArray();
  831. }
  832. public static string AppendZeroToNumString(string str, int numKeta)
  833. {
  834. int n = numKeta - str.Length;
  835. if (n >= 1)
  836. {
  837. return MakeCharArray('0', n) + str;
  838. }
  839. else
  840. {
  841. return str;
  842. }
  843. }
  844. public static Encoding CheckBOM(byte[] data)
  845. {
  846. int i;
  847. return CheckBOM(data, out i);
  848. }
  849. public static Encoding CheckBOM(byte[] data, out int bomNumBytes)
  850. {
  851. bomNumBytes = 0;
  852. try
  853. {
  854. if (data[0] == 0x00 && data[1] == 0x00 && data[2] == 0xfe && data[3] == 0xff)
  855. {
  856. bomNumBytes = 3;
  857. return Encoding.GetEncoding("utf-32BE");
  858. }
  859. else if (data[0] == 0xff && data[1] == 0xfe && data[2] == 0x00 && data[3] == 0x00)
  860. {
  861. bomNumBytes = 4;
  862. return Encoding.GetEncoding("utf-32");
  863. }
  864. else if (data[0] == 0xff && data[1] == 0xfe)
  865. {
  866. bomNumBytes = 2;
  867. return Encoding.GetEncoding("utf-16");
  868. }
  869. else if (data[0] == 0xfe && data[1] == 0xff)
  870. {
  871. bomNumBytes = 2;
  872. return Encoding.GetEncoding("unicodeFFFE");
  873. }
  874. else if (data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf)
  875. {
  876. bomNumBytes = 3;
  877. return Encoding.GetEncoding("utf-8");
  878. }
  879. else
  880. {
  881. return null;
  882. }
  883. }
  884. catch
  885. {
  886. return null;
  887. }
  888. }
  889. public static byte[] GetBOM(Encoding encoding)
  890. {
  891. if (Str.StrCmpi(encoding.BodyName, "utf-32BE"))
  892. {
  893. return new byte[] { 0x00, 0x00, 0xfe, 0xff };
  894. }
  895. else if (Str.StrCmpi(encoding.BodyName, "utf-32"))
  896. {
  897. return new byte[] {0xff, 0xfe, 0x00, 0x00 };
  898. }
  899. else if (Str.StrCmpi(encoding.BodyName, "utf-16"))
  900. {
  901. return new byte[] { 0xff, 0xfe };
  902. }
  903. else if (Str.StrCmpi(encoding.BodyName, "unicodeFFFE"))
  904. {
  905. return new byte[] { 0xfe, 0xff };
  906. }
  907. else if (Str.StrCmpi(encoding.BodyName, "utf-8"))
  908. {
  909. return new byte[] { 0xef, 0xbb, 0xbf };
  910. }
  911. else
  912. {
  913. return null;
  914. }
  915. }
  916. public static byte[] ConvertEncoding(byte[] srcData, Encoding destEncoding)
  917. {
  918. return ConvertEncoding(srcData, destEncoding, false);
  919. }
  920. public static byte[] ConvertEncoding(byte[] srcData, Encoding destEncoding, bool appendBom)
  921. {
  922. Encoding srcEncoding = GetEncoding(srcData);
  923. if (srcEncoding == null)
  924. {
  925. srcEncoding = Str.ShiftJisEncoding;
  926. }
  927. int nb;
  928. if (CheckBOM(srcData, out nb) != null)
  929. {
  930. srcData = Util.RemoveStartByteArray(srcData, nb);
  931. }
  932. string str = srcEncoding.GetString(srcData);
  933. byte[] b1 = null;
  934. if (appendBom)
  935. {
  936. b1 = GetBOM(destEncoding);
  937. }
  938. byte[] b2 = destEncoding.GetBytes(str);
  939. return Util.CombineByteArray(b1, b2);
  940. }
  941. public static string ReadTextFile(string filename)
  942. {
  943. byte[] data = IO.ReadFileData(filename);
  944. int bomSize = 0;
  945. Encoding enc = GetEncoding(data, out bomSize);
  946. if (enc == null)
  947. {
  948. enc = Str.Utf8Encoding;
  949. }
  950. if (bomSize >= 1)
  951. {
  952. data = Util.CopyByte(data, bomSize);
  953. }
  954. return enc.GetString(data);
  955. }
  956. public static void WriteTextFile(string filename, Encoding enc, bool writeBom)
  957. {
  958. Buf buf = new Buf();
  959. byte[] bom = GetBOM(enc);
  960. if (writeBom && bom != null && bom.Length >= 1)
  961. {
  962. buf.Write(bom);
  963. }
  964. buf.Write(enc.GetBytes(filename));
  965. buf.SeekToBegin();
  966. File.WriteAllBytes(filename, buf.Read());
  967. }
  968. public static Encoding GetEncoding(byte[] data)
  969. {
  970. int i;
  971. return GetEncoding(data, out i);
  972. }
  973. public static Encoding GetEncoding(byte[] data, out int bomSize)
  974. {
  975. const byte bESC = 0x1B;
  976. const byte bAT = 0x40;
  977. const byte bDollar = 0x24;
  978. const byte bAnd = 0x26;
  979. const byte bOP = 0x28;
  980. const byte bB = 0x42;
  981. const byte bD = 0x44;
  982. const byte bJ = 0x4A;
  983. const byte bI = 0x49;
  984. bomSize = 0;
  985. int len = data.Length;
  986. int binary = 0;
  987. int ucs2 = 0;
  988. int sjis = 0;
  989. int euc = 0;
  990. int utf8 = 0;
  991. byte b1, b2;
  992. Encoding bomEncoding = CheckBOM(data, out bomSize);
  993. if (bomEncoding != null)
  994. {
  995. return bomEncoding;
  996. }
  997. for (int i = 0; i < len; i++)
  998. {
  999. if (data[i] <= 0x06 || data[i] == 0x7F || data[i] == 0xFF)
  1000. {
  1001. //'binary'
  1002. binary++;
  1003. if (len - 1 > i && data[i] == 0x00
  1004. && i > 0 && data[i - 1] <= 0x7F)
  1005. {
  1006. //smells like raw unicode
  1007. ucs2++;
  1008. }
  1009. }
  1010. }
  1011. if (binary > 0)
  1012. {
  1013. if (ucs2 > 0)
  1014. {
  1015. //JIS
  1016. //ucs2(Unicode)
  1017. int n1 = 0, n2 = 0;
  1018. for (int i = 0; i < (len / 2); i++)
  1019. {
  1020. byte e1 = data[i * 2];
  1021. byte e2 = data[i * 2 + 1];
  1022. if (e1 == 0 && e2 != 0)
  1023. {
  1024. n1++;
  1025. }
  1026. else if (e1 != 0 && e2 == 0)
  1027. {
  1028. n2++;
  1029. }
  1030. }
  1031. if (n1 > n2)
  1032. {
  1033. return Encoding.GetEncoding("unicodeFFFE");
  1034. }
  1035. else
  1036. {
  1037. return System.Text.Encoding.Unicode;
  1038. }
  1039. }
  1040. else
  1041. {
  1042. //binary
  1043. return null;
  1044. }
  1045. }
  1046. for (int i = 0; i < len - 1; i++)
  1047. {
  1048. b1 = data[i];
  1049. b2 = data[i + 1];
  1050. if (b1 == bESC)
  1051. {
  1052. if (b2 >= 0x80)
  1053. //not Japanese
  1054. //ASCII
  1055. return System.Text.Encoding.ASCII;
  1056. else if (len - 2 > i &&
  1057. b2 == bDollar && data[i + 2] == bAT)
  1058. //JIS_0208 1978
  1059. //JIS
  1060. return System.Text.Encoding.GetEncoding(50220);
  1061. else if (len - 2 > i &&
  1062. b2 == bDollar && data[i + 2] == bB)
  1063. //JIS_0208 1983
  1064. //JIS
  1065. return System.Text.Encoding.GetEncoding(50220);
  1066. else if (len - 5 > i &&
  1067. b2 == bAnd && data[i + 2] == bAT && data[i + 3] == bESC &&
  1068. data[i + 4] == bDollar && data[i + 5] == bB)
  1069. //JIS_0208 1990
  1070. //JIS
  1071. return System.Text.Encoding.GetEncoding(50220);
  1072. else if (len - 3 > i &&
  1073. b2 == bDollar && data[i + 2] == bOP && data[i + 3] == bD)
  1074. //JIS_0212
  1075. //JIS
  1076. return System.Text.Encoding.GetEncoding(50220);
  1077. else if (len - 2 > i &&
  1078. b2 == bOP && (data[i + 2] == bB || data[i + 2] == bJ))
  1079. //JIS_ASC
  1080. //JIS
  1081. return System.Text.Encoding.GetEncoding(50220);
  1082. else if (len - 2 > i &&
  1083. b2 == bOP && data[i + 2] == bI)
  1084. //JIS_KANA
  1085. //JIS
  1086. return System.Text.Encoding.GetEncoding(50220);
  1087. }
  1088. }
  1089. for (int i = 0; i < len - 1; i++)
  1090. {
  1091. b1 = data[i];
  1092. b2 = data[i + 1];
  1093. if (((b1 >= 0x81 && b1 <= 0x9F) || (b1 >= 0xE0 && b1 <= 0xFC)) &&
  1094. ((b2 >= 0x40 && b2 <= 0x7E) || (b2 >= 0x80 && b2 <= 0xFC)))
  1095. {
  1096. sjis += 2;
  1097. i++;
  1098. }
  1099. }
  1100. for (int i = 0; i < len - 1; i++)
  1101. {
  1102. b1 = data[i];
  1103. b2 = data[i + 1];
  1104. if (((b1 >= 0xA1 && b1 <= 0xFE) && (b2 >= 0xA1 && b2 <= 0xFE)) ||
  1105. (b1 == 0x8E && (b2 >= 0xA1 && b2 <= 0xDF)))
  1106. {
  1107. euc += 2;
  1108. i++;
  1109. }
  1110. else if (len - 2 > i &&
  1111. b1 == 0x8F && (b2 >= 0xA1 && b2 <= 0xFE) &&
  1112. (data[i + 2] >= 0xA1 && data[i + 2] <= 0xFE))
  1113. {
  1114. euc += 3;
  1115. i += 2;
  1116. }
  1117. }
  1118. for (int i = 0; i < len - 1; i++)
  1119. {
  1120. b1 = data[i];
  1121. b2 = data[i + 1];
  1122. if ((b1 >= 0xC0 && b1 <= 0xDF) && (b2 >= 0x80 && b2 <= 0xBF))
  1123. {
  1124. utf8 += 2;
  1125. i++;
  1126. }
  1127. else if (len - 2 > i &&
  1128. (b1 >= 0xE0 && b1 <= 0xEF) && (b2 >= 0x80 && b2 <= 0xBF) &&
  1129. (data[i + 2] >= 0x80 && data[i + 2] <= 0xBF))
  1130. {
  1131. utf8 += 3;
  1132. i += 2;
  1133. }
  1134. }
  1135. if (euc > sjis && euc > utf8)
  1136. //EUC
  1137. return System.Text.Encoding.GetEncoding(51932);
  1138. else if (sjis > euc && sjis > utf8)
  1139. //SJIS
  1140. return System.Text.Encoding.GetEncoding(932);
  1141. else if (utf8 > euc && utf8 > sjis)
  1142. //UTF8
  1143. return System.Text.Encoding.UTF8;
  1144. return null;
  1145. }
  1146. public static bool StartsWithMulti(string str, StringComparison comp, params string[] keys)
  1147. {
  1148. NormalizeString(ref str);
  1149. foreach (string key in keys)
  1150. {
  1151. if (str.StartsWith(key, comp))
  1152. {
  1153. return true;
  1154. }
  1155. }
  1156. return false;
  1157. }
  1158. public static bool IsCharForMail(char c)
  1159. {
  1160. switch (c)
  1161. {
  1162. case '<':
  1163. case '>':
  1164. case ' ':
  1165. case ';':
  1166. case ':':
  1167. case '/':
  1168. case '(':
  1169. case ')':
  1170. case '&':
  1171. case ',':
  1172. case '%':
  1173. case '$':
  1174. case '#':
  1175. case '\"':
  1176. case '\'':
  1177. case '!':
  1178. case '=':
  1179. case '\\':
  1180. return false;
  1181. }
  1182. if (c >= 0x80)
  1183. {
  1184. return false;
  1185. }
  1186. if (IsAscii(c) == false)
  1187. {
  1188. return false;
  1189. }
  1190. return true;
  1191. }
  1192. public static string LinkMailtoOnText(string text)
  1193. {
  1194. NormalizeString(ref text);
  1195. StringBuilder sb = new StringBuilder();
  1196. string tmp = "";
  1197. int i;
  1198. for (i = 0; i < text.Length; i++)
  1199. {
  1200. char c = text[i];
  1201. if (IsCharForMail(c) == false)
  1202. {
  1203. if (Str.CheckMailAddress(tmp) == false)
  1204. {
  1205. tmp += c;
  1206. sb.Append(tmp);
  1207. tmp = "";
  1208. }
  1209. else
  1210. {
  1211. sb.AppendFormat("<a href=\"mailto:{0}\">{0}</a>", tmp);
  1212. sb.Append(c);
  1213. tmp = "";
  1214. }
  1215. }
  1216. else
  1217. {
  1218. tmp += c;
  1219. }
  1220. }
  1221. if (Str.CheckMailAddress(tmp) == false)
  1222. {
  1223. sb.Append(tmp);
  1224. tmp = "";
  1225. }
  1226. else
  1227. {
  1228. sb.AppendFormat("<a href=\"mailto:{0}\">{0}</a>", tmp);
  1229. tmp = "";
  1230. }
  1231. return sb.ToString();
  1232. }
  1233. public static string LinkUrlOnText(string text, string target)
  1234. {
  1235. int findStart = 0;
  1236. NormalizeString(ref text);
  1237. NormalizeString(ref target);
  1238. StringBuilder sb = new StringBuilder();
  1239. while (true)
  1240. {
  1241. int foundStrIndex;
  1242. int foundIndex = FindStrings(text, findStart, StringComparison.InvariantCultureIgnoreCase, out foundStrIndex,
  1243. "http://", "https://", "ftp://", "telnet://", "mailto://", "news://");
  1244. if (foundIndex != -1)
  1245. {
  1246. int i;
  1247. int endOfUrl = -1;
  1248. for (i = foundIndex; i < text.Length; i++)
  1249. {
  1250. char c = text[i];
  1251. if (IsValidForUrl(c) == false)
  1252. {
  1253. endOfUrl = i;
  1254. break;
  1255. }
  1256. if (c == '<' || c == '&')
  1257. {
  1258. if (StartsWithMulti(text.Substring(i), StringComparison.InvariantCultureIgnoreCase,
  1259. HtmlSpacing, HtmlCrlf, HtmlBr, HtmlLt, HtmlGt))
  1260. {
  1261. endOfUrl = i;
  1262. break;
  1263. }
  1264. }
  1265. }
  1266. if (endOfUrl == -1)
  1267. {
  1268. endOfUrl = text.Length;
  1269. }
  1270. string url = text.Substring(foundIndex, endOfUrl - foundIndex);
  1271. string beforeUrl = text.Substring(findStart, foundIndex - findStart);
  1272. sb.Append(beforeUrl);
  1273. if (Str.IsEmptyStr(target) == false)
  1274. {
  1275. sb.AppendFormat("<a href=\"{0}\" target=\"{2}\">{1}</a>", url, url, target);
  1276. }
  1277. else
  1278. {
  1279. sb.AppendFormat("<a href=\"{0}\">{1}</a>", url, url);
  1280. }
  1281. findStart = endOfUrl;
  1282. }
  1283. else
  1284. {
  1285. sb.Append(text.Substring(findStart));
  1286. break;
  1287. }
  1288. }
  1289. return LinkMailtoOnText(sb.ToString());
  1290. }
  1291. public static int FindStrings(string str, int findStartIndex, StringComparison comp, out int foundKeyIndex, params string[] keys)
  1292. {
  1293. int ret = -1;
  1294. foundKeyIndex = -1;
  1295. int n = 0;
  1296. foreach (string key in keys)
  1297. {
  1298. int i = str.IndexOf(key, findStartIndex, comp);
  1299. if (i != -1)
  1300. {
  1301. if (ret == -1)
  1302. {
  1303. ret = i;
  1304. foundKeyIndex = n;
  1305. }
  1306. else
  1307. {
  1308. if (ret > i)
  1309. {
  1310. ret = i;
  1311. foundKeyIndex = n;
  1312. }
  1313. }
  1314. }
  1315. n++;
  1316. }
  1317. return ret;
  1318. }
  1319. public static bool IsValidForUrl(char c)
  1320. {
  1321. if (c >= '0' && c <= '9')
  1322. {
  1323. return true;
  1324. }
  1325. if (c >= 'a' && c <= 'z')
  1326. {
  1327. return true;
  1328. }
  1329. if (c >= 'A' && c <= 'Z')
  1330. {
  1331. return true;
  1332. }
  1333. switch (c)
  1334. {
  1335. case '_':
  1336. case '-':
  1337. case '?':
  1338. case '!':
  1339. case '\"':
  1340. case ',':
  1341. case '\'':
  1342. case '/':
  1343. case '\\':
  1344. case '&':
  1345. case ';':
  1346. case '%':
  1347. case '#':
  1348. case '@':
  1349. case '~':
  1350. case ':':
  1351. case '=':
  1352. case '+':
  1353. case '*':
  1354. case '$':
  1355. case '.':
  1356. return true;
  1357. }
  1358. return false;
  1359. }
  1360. public static List<string> RemoteStringFromList(List<string> str, RemoveStringFunction func)
  1361. {
  1362. List<string> ret = new List<string>();
  1363. foreach (string s in str)
  1364. {
  1365. if (func(s) == false)
  1366. {
  1367. ret.Add(s);
  1368. }
  1369. }
  1370. return ret;
  1371. }
  1372. public const string ConstZenkaku = "`{}0123456789/*-+!”#$%&’()= ̄|¥[]@;:<>?_^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ‘";
  1373. public const string ConstHankaku = "`{}0123456789/*-+!\"#$%&'()=~|\\[]@;:<>?_^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'";
  1374. public const string ConstKanaZenkaku = "ー「」アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンァゥェォャュョッィ゛゜";
  1375. public const string ConstKanaHankaku = "ー「」アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンァゥェォャュョッィ゙゚";
  1376. public static void RemoveSpace(ref string str)
  1377. {
  1378. NormalizeString(ref str);
  1379. str = str.Replace(" ", "").Replace(" ", "").Replace("\t", "");
  1380. }
  1381. public static void TrimStartWith(ref string str, string key, StringComparison sc)
  1382. {
  1383. if (str.StartsWith(key, sc))
  1384. {
  1385. str = str.Substring(key.Length);
  1386. }
  1387. }
  1388. public static void TrimEndsWith(ref string str, string key, StringComparison sc)
  1389. {
  1390. if (str.EndsWith(key, sc))
  1391. {
  1392. str = str.Substring(0, str.Length - key.Length);
  1393. }
  1394. }
  1395. public static void RemoveSpaceChar(ref string str)
  1396. {
  1397. if (Str.IsEmptyStr(str))
  1398. {
  1399. return;
  1400. }
  1401. StringBuilder sb = new StringBuilder();
  1402. foreach (char c in str)
  1403. {
  1404. if (c == ' ' || c == '\t' || c == ' ')
  1405. {
  1406. }
  1407. else
  1408. {
  1409. sb.Append(c);
  1410. }
  1411. }
  1412. str = sb.ToString();
  1413. }
  1414. public static void NormalizeStringStandard(ref string str)
  1415. {
  1416. NormalizeString(ref str, true, true, false, true);
  1417. }
  1418. public static void NormalizeString(ref string str, bool space, bool toHankaku, bool toZenkaku, bool toZenkakuKana)
  1419. {
  1420. NormalizeString(ref str);
  1421. if (space)
  1422. {
  1423. str = NormalizeSpace(str);
  1424. }
  1425. if (toHankaku)
  1426. {
  1427. str = ZenkakuToHankaku(str);
  1428. }
  1429. if (toZenkaku)
  1430. {
  1431. str = HankakuToZenkaku(str);
  1432. }
  1433. if (toZenkakuKana)
  1434. {
  1435. str = KanaHankakuToZenkaku(str);
  1436. }
  1437. }
  1438. public static string NormalizeSpace(string str)
  1439. {
  1440. NormalizeString(ref str);
  1441. char[] sps =
  1442. {
  1443. ' ', ' ', '\t',
  1444. };
  1445. string[] tokens = str.Split(sps, StringSplitOptions.RemoveEmptyEntries);
  1446. return Str.CombineStringArray(tokens, " ");
  1447. }
  1448. public static string KanaHankakuToZenkaku(string str)
  1449. {
  1450. NormalizeString(ref str);
  1451. str = str.Replace("ガ", "ガ");
  1452. str = str.Replace("ギ", "ギ");
  1453. str = str.Replace("グ", "グ");
  1454. str = str.Replace("ゲ", "ゲ");
  1455. str = str.Replace("ゴ", "ゴ");
  1456. str = str.Replace("ザ", "ザ");
  1457. str = str.Replace("ジ", "ジ");
  1458. str = str.Replace("ズ", "ズ");
  1459. str = str.Replace("ゼ", "ゼ");
  1460. str = str.Replace("ゾ", "ゾ");
  1461. str = str.Replace("ダ", "ダ");
  1462. str = str.Replace("ヂ", "ヂ");
  1463. str = str.Replace("ヅ", "ヅ");
  1464. str = str.Replace("デ", "デ");
  1465. str = str.Replace("ド", "ド");
  1466. str = str.Replace("バ", "バ");
  1467. str = str.Replace("ビ", "ビ");
  1468. str = str.Replace("ブ", "ブ");
  1469. str = str.Replace("ベ", "ベ");
  1470. str = str.Replace("ボ", "ボ");
  1471. char[] a = str.ToCharArray();
  1472. int i;
  1473. for (i = 0; i < a.Length; i++)
  1474. {
  1475. int j = ConstKanaHankaku.IndexOf(a[i]);
  1476. if (j != -1)
  1477. {
  1478. a[i] = ConstKanaZenkaku[j];
  1479. }
  1480. }
  1481. return new string(a);
  1482. }
  1483. public static string ZenkakuToHankaku(string str)
  1484. {
  1485. NormalizeString(ref str);
  1486. str = ReplaceStr(str, "“", " \"");
  1487. str = ReplaceStr(str, "”", "\" ");
  1488. str = ReplaceStr(str, "‘", "'");
  1489. str = ReplaceStr(str, "’", "'");
  1490. char[] a = str.ToCharArray();
  1491. int i;
  1492. for (i = 0; i < a.Length; i++)
  1493. {
  1494. int j = ConstZenkaku.IndexOf(a[i]);
  1495. if (j != -1)
  1496. {
  1497. a[i] = ConstHankaku[j];
  1498. }
  1499. }
  1500. return new string(a);
  1501. }
  1502. public static string HankakuToZenkaku(string str)
  1503. {
  1504. NormalizeString(ref str);
  1505. str = KanaHankakuToZenkaku(str);
  1506. char[] a = str.ToCharArray();
  1507. int i;
  1508. for (i = 0; i < a.Length; i++)
  1509. {
  1510. int j = ConstHankaku.IndexOf(a[i]);
  1511. if (j != -1)
  1512. {
  1513. a[i] = ConstZenkaku[j];
  1514. }
  1515. }
  1516. return new string(a);
  1517. }
  1518. public const string HtmlSpacing = "&nbsp;";
  1519. public const string HtmlCrlf = "<BR>";
  1520. public const string HtmlBr = "<BR>";
  1521. public const string HtmlLt = "&lt;";
  1522. public const string HtmlGt = "&gt;";
  1523. public const string HtmlAmp = "&amp;";
  1524. public const int HtmlNumTabChar = 8;
  1525. public static string HtmlTab
  1526. {
  1527. get
  1528. {
  1529. int i;
  1530. StringBuilder sb = new StringBuilder();
  1531. for (i = 0; i < HtmlNumTabChar; i++)
  1532. {
  1533. sb.Append(HtmlSpacing);
  1534. }
  1535. return sb.ToString();
  1536. }
  1537. }
  1538. public static string ToUrl(string str, Encoding e)
  1539. {
  1540. Str.NormalizeString(ref str);
  1541. return HttpUtility.UrlEncode(str, e);
  1542. }
  1543. public static string FromUrl(string str, Encoding e)
  1544. {
  1545. Str.NormalizeString(ref str);
  1546. return HttpUtility.UrlDecode(str, e);
  1547. }
  1548. public static string FromHtml(string str)
  1549. {
  1550. str = Str.ReplaceStr(str, HtmlCrlf, "\r\n", false);
  1551. str = str.Replace(HtmlSpacing, " ");
  1552. str = str.Replace(HtmlLt, "<").Replace(HtmlGt, ">").Replace(HtmlAmp, "&");
  1553. str = NormalizeCrlf(str);
  1554. return str;
  1555. }
  1556. public static string ToHtml(string str)
  1557. {
  1558. return ToHtml(str, false);
  1559. }
  1560. public static string ToHtml(string str, bool forceAllSpaceToTag)
  1561. {
  1562. str = NormalizeCrlf(str);
  1563. str = str.Replace("&", HtmlAmp);
  1564. str = str.Replace("<", HtmlLt).Replace(">", HtmlGt);
  1565. if (str.IndexOf(' ') != -1)
  1566. {
  1567. if (forceAllSpaceToTag)
  1568. {
  1569. str = str.Replace(" ", HtmlSpacing);
  1570. }
  1571. else
  1572. {
  1573. int i;
  1574. StringBuilder sb = new StringBuilder();
  1575. bool flag = false;
  1576. for (i = 0; i < str.Length; i++)
  1577. {
  1578. char c = str[i];
  1579. if (c == ' ')
  1580. {
  1581. if (flag == false)
  1582. {
  1583. flag = true;
  1584. sb.Append(' ');
  1585. }
  1586. else
  1587. {
  1588. sb.Append(HtmlSpacing);
  1589. }
  1590. }
  1591. else
  1592. {
  1593. flag = false;
  1594. sb.Append(c);
  1595. }
  1596. }
  1597. str = sb.ToString();
  1598. }
  1599. }
  1600. str = str.Replace("\t", HtmlTab);
  1601. str = str.Replace("\r\n", HtmlCrlf);
  1602. return str;
  1603. }
  1604. public static bool IsPrintable(char c)
  1605. {
  1606. if (c >= 256)
  1607. {
  1608. return true;
  1609. }
  1610. if (c >= 32 && c <= 126)
  1611. {
  1612. return true;
  1613. }
  1614. return false;
  1615. }
  1616. public static bool IsPrintable(string str)
  1617. {
  1618. foreach (char c in str)
  1619. {
  1620. if (IsPrintable(c) == false)
  1621. {
  1622. return false;
  1623. }
  1624. }
  1625. return true;
  1626. }
  1627. public static string Unescape(string str)
  1628. {
  1629. StringBuilder sb = new StringBuilder();
  1630. int i;
  1631. for (i = 0; i < str.Length; i++)
  1632. {
  1633. char c = str[i];
  1634. if (IsPrintable(c) && c != '\\')
  1635. {
  1636. sb.Append(c);
  1637. }
  1638. else
  1639. {
  1640. string s = "" + c;
  1641. switch (c)
  1642. {
  1643. case '\r':
  1644. s = "\\r";
  1645. break;
  1646. case '\n':
  1647. s = "\\n";
  1648. break;
  1649. case '\0':
  1650. s = "\\0";
  1651. break;
  1652. case '\t':
  1653. s = "\\t";
  1654. break;
  1655. case '\\':
  1656. s = "\\\\";
  1657. break;
  1658. default:
  1659. s = "0x" + Convert.ToString((int)c, 16);
  1660. break;
  1661. }
  1662. sb.Append(s);
  1663. }
  1664. }
  1665. return sb.ToString();
  1666. }
  1667. public static string Escape(string str)
  1668. {
  1669. StringBuilder sb = new StringBuilder();
  1670. int i, j, hex;
  1671. string padding = "00000000";
  1672. str = str + padding;
  1673. StringBuilder sb2;
  1674. for (i = 0; i < str.Length - padding.Length; i++)
  1675. {
  1676. char c = str[i];
  1677. char d = c;
  1678. if (c == '\\')
  1679. {
  1680. char c1 = str[i + 1];
  1681. switch (c1)
  1682. {
  1683. case '\'':
  1684. d = '\'';
  1685. i++;
  1686. break;
  1687. case '?':
  1688. d = '?';
  1689. i++;
  1690. break;
  1691. case '\\':
  1692. d = '\\';
  1693. i++;
  1694. break;
  1695. case 't':
  1696. d = '\t';
  1697. i++;
  1698. break;
  1699. case 'r':
  1700. d = '\r';
  1701. i++;
  1702. break;
  1703. case 'n':
  1704. d = '\n';
  1705. i++;
  1706. break;
  1707. case ' ':
  1708. d = ' ';
  1709. i++;
  1710. break;
  1711. case ' ':
  1712. d = ' ';
  1713. i++;
  1714. break;
  1715. case '\t':
  1716. d = '\t';
  1717. i++;
  1718. break;
  1719. case '0':
  1720. d = '\0';
  1721. i++;
  1722. break;
  1723. case 'x':
  1724. i++;
  1725. sb2 = new StringBuilder();
  1726. for (j = 0; j < 4; j++)
  1727. {
  1728. char c2 = str[++i];
  1729. if ((c2 >= '0' && c2 <= '9') || (c2 >= 'a' && c2 <= 'f') || (c2 >= 'A' && c2 <= 'F'))
  1730. {
  1731. sb2.Append(c2);
  1732. }
  1733. else
  1734. {
  1735. i--;
  1736. break;
  1737. }
  1738. }
  1739. hex = Convert.ToInt32(sb2.ToString(), 16);
  1740. d = (char)hex;
  1741. break;
  1742. default:
  1743. if (c1 >= '0' && c1 <= '7')
  1744. {
  1745. sb2 = new StringBuilder();
  1746. for (j = 0; j < 3; j++)
  1747. {
  1748. char c2 = str[++i];
  1749. if (c2 >= '0' && c2 <= '7')
  1750. {
  1751. sb2.Append(c2);
  1752. }
  1753. else
  1754. {
  1755. i--;
  1756. break;
  1757. }
  1758. }
  1759. hex = Convert.ToInt32(sb2.ToString(), 8);
  1760. d = (char)hex;
  1761. }
  1762. else
  1763. {
  1764. d = '\\';
  1765. i++;
  1766. }
  1767. break;
  1768. }
  1769. }
  1770. if (d != '\0')
  1771. {
  1772. sb.Append(d);
  1773. }
  1774. else
  1775. {
  1776. break;
  1777. }
  1778. }
  1779. return sb.ToString();
  1780. }
  1781. public static int GetStrWidth(string str)
  1782. {
  1783. int ret = 0;
  1784. foreach (char c in str)
  1785. {
  1786. if (c <= 255)
  1787. {
  1788. ret++;
  1789. }
  1790. else
  1791. {
  1792. ret += 2;
  1793. }
  1794. }
  1795. return ret;
  1796. }
  1797. public static string TrimCrlf(string str)
  1798. {
  1799. int len;
  1800. if (str == null)
  1801. {
  1802. return "";
  1803. }
  1804. len = str.Length;
  1805. if (len == 0)
  1806. {
  1807. return "";
  1808. }
  1809. if (str[len - 1] == '\n')
  1810. {
  1811. if (len >= 2 && str[len - 2] == '\r')
  1812. {
  1813. str = str.Substring(0, len - 2);
  1814. }
  1815. str = str.Substring(0, len - 1);
  1816. }
  1817. else if (str[len - 1] == '\r')
  1818. {
  1819. str = str.Substring(0, len - 1);
  1820. }
  1821. return str;
  1822. }
  1823. public static bool IsAllUpperStr(string str)
  1824. {
  1825. int i, len;
  1826. if (str == null)
  1827. {
  1828. return false;
  1829. }
  1830. len = str.Length;
  1831. for (i = 0; i < len; i++)
  1832. {
  1833. char c = str[i];
  1834. if ((c >= '0' && c <= '9') ||
  1835. (c >= 'A' && c <= 'Z'))
  1836. {
  1837. }
  1838. else
  1839. {
  1840. return false;
  1841. }
  1842. }
  1843. return true;
  1844. }
  1845. public static List<string> StrArrayToList(string[] strArray)
  1846. {
  1847. List<string> ret = new List<string>();
  1848. foreach (string s in strArray)
  1849. {
  1850. ret.Add(s);
  1851. }
  1852. return ret;
  1853. }
  1854. private static string[] __new_ParseCmdLine(string str)
  1855. {
  1856. List<string> o;
  1857. int i, len, mode;
  1858. char c;
  1859. StringBuilder tmp;
  1860. bool ignore_space = false;
  1861. if (str == null)
  1862. {
  1863. return new string[0];
  1864. }
  1865. o = new List<string>();
  1866. tmp = new StringBuilder();
  1867. mode = 0;
  1868. len = str.Length;
  1869. for (i = 0; i < len; i++)
  1870. {
  1871. c = str[i];
  1872. switch (mode)
  1873. {
  1874. case 0:
  1875. if (c == ' ' || c == '\t')
  1876. {
  1877. }
  1878. else
  1879. {
  1880. if (c == '\"')
  1881. {
  1882. if (str[i + 1] == '\"')
  1883. {
  1884. tmp.Append("\"");
  1885. i++;
  1886. }
  1887. else
  1888. {
  1889. ignore_space = true;
  1890. }
  1891. }
  1892. else
  1893. {
  1894. tmp.Append(c);
  1895. }
  1896. }
  1897. mode = 1;
  1898. break;
  1899. case 1:
  1900. if (ignore_space == false && (c == ' ' || c == '\t'))
  1901. {
  1902. o.Add(tmp.ToString());
  1903. tmp = new StringBuilder();
  1904. mode = 0;
  1905. }
  1906. else
  1907. {
  1908. if (c == '\"')
  1909. {
  1910. if (str[i + 1] == '\"')
  1911. {
  1912. tmp.Append("\"");
  1913. i++;
  1914. }
  1915. else
  1916. {
  1917. if (ignore_space == false)
  1918. {
  1919. ignore_space = true;
  1920. }
  1921. else
  1922. {
  1923. ignore_space = false;
  1924. }
  1925. }
  1926. }
  1927. else
  1928. {
  1929. tmp.Append(c);
  1930. }
  1931. }
  1932. break;
  1933. }
  1934. }
  1935. if (tmp.Length >= 1)
  1936. {
  1937. o.Add(tmp.ToString());
  1938. }
  1939. List<string> ret = new List<string>();
  1940. foreach (string s in o)
  1941. {
  1942. ret.Add(s);
  1943. }
  1944. return ret.ToArray();
  1945. }
  1946. public static int CompareString(string s1, string s2)
  1947. {
  1948. try
  1949. {
  1950. return string.Compare(s1, s2, true);
  1951. }
  1952. catch
  1953. {
  1954. return 0;
  1955. }
  1956. }
  1957. public static int CompareStringCaseSensitive(string s1, string s2)
  1958. {
  1959. try
  1960. {
  1961. return string.Compare(s1, s2, false);
  1962. }
  1963. catch
  1964. {
  1965. return 0;
  1966. }
  1967. }
  1968. public static string ReplaceStr(string str, string oldKeyword, string newKeyword)
  1969. {
  1970. return ReplaceStr(str, oldKeyword, newKeyword, false);
  1971. }
  1972. public static string ReplaceStr(string str, string oldKeyword, string newKeyword, bool caseSensitive)
  1973. {
  1974. int len_string, len_old, len_new;
  1975. if (str == null || oldKeyword == null || newKeyword == null)
  1976. {
  1977. return null;
  1978. }
  1979. if (caseSensitive == false)
  1980. {
  1981. return str.Replace(oldKeyword, newKeyword);
  1982. }
  1983. int i, j, num;
  1984. StringBuilder sb = new StringBuilder();
  1985. len_string = str.Length;
  1986. len_old = oldKeyword.Length;
  1987. len_new = newKeyword.Length;
  1988. i = j = num = 0;
  1989. while (true)
  1990. {
  1991. i = SearchStr(str, oldKeyword, i, caseSensitive);
  1992. if (i == -1)
  1993. {
  1994. sb.Append(str.Substring(j, len_string - j));
  1995. break;
  1996. }
  1997. num++;
  1998. sb.Append(str.Substring(j, i - j));
  1999. sb.Append(newKeyword);
  2000. i += len_old;
  2001. j = i;
  2002. }
  2003. return sb.ToString();
  2004. }
  2005. public static int SearchStrMulti(string str, int start, bool caseSensitive, out int foundIndex, out string foundKeyword, params string[] keywords)
  2006. {
  2007. int i;
  2008. foundIndex = -1;
  2009. foundKeyword = "";
  2010. int ret = -1;
  2011. int min = int.MaxValue;
  2012. for (i = 0; i < keywords.Length; i++)
  2013. {
  2014. string keyword = keywords[i];
  2015. int r = Str.SearchStr(str, keyword, start, caseSensitive);
  2016. if (r != -1)
  2017. {
  2018. if (min > r)
  2019. {
  2020. min = r;
  2021. foundKeyword = str.Substring(r, keyword.Length);
  2022. foundIndex = i;
  2023. }
  2024. }
  2025. }
  2026. if (foundIndex != -1)
  2027. {
  2028. ret = min;
  2029. }
  2030. return ret;
  2031. }
  2032. public static int SearchStr(string str, string keyword, int start)
  2033. {
  2034. return SearchStr(str, keyword, start, false);
  2035. }
  2036. public static int SearchStr(string str, string keyword, int start, bool caseSensitive)
  2037. {
  2038. if (str == null || keyword == null)
  2039. {
  2040. return -1;
  2041. }
  2042. try
  2043. {
  2044. return str.IndexOf(keyword, start, (caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase));
  2045. }
  2046. catch
  2047. {
  2048. return -1;
  2049. }
  2050. }
  2051. public static void Printf(string fmt, params object[] args)
  2052. {
  2053. if (args.Length == 0)
  2054. {
  2055. Console.Write(fmt);
  2056. }
  2057. else
  2058. {
  2059. Console.Write(FormatC(fmt, args));
  2060. }
  2061. }
  2062. public static string FormatC(string fmt)
  2063. {
  2064. return FormatC(fmt, new object[0]);
  2065. }
  2066. public static string FormatC(string fmt, params object[] args)
  2067. {
  2068. int i, len;
  2069. StringBuilder tmp;
  2070. List<string> o;
  2071. int mode = 0;
  2072. int pos = 0;
  2073. if (fmt == null)
  2074. {
  2075. return null;
  2076. }
  2077. len = fmt.Length;
  2078. tmp = new StringBuilder();
  2079. o = new List<string>();
  2080. mode = 0;
  2081. for (i = 0; i < len; i++)
  2082. {
  2083. char c = fmt[i];
  2084. if (mode == 0)
  2085. {
  2086. switch (c)
  2087. {
  2088. case '%':
  2089. if (fmt[i + 1] == '%')
  2090. {
  2091. i++;
  2092. tmp.Append("%");
  2093. }
  2094. else
  2095. {
  2096. mode = 1;
  2097. o.Add(tmp.ToString());
  2098. tmp = new StringBuilder();
  2099. tmp.Append(c);
  2100. }
  2101. break;
  2102. default:
  2103. tmp.Append(c);
  2104. break;
  2105. }
  2106. }
  2107. else
  2108. {
  2109. switch (c)
  2110. {
  2111. case 'c':
  2112. case 'C':
  2113. case 'd':
  2114. case 'i':
  2115. case 'o':
  2116. case 'u':
  2117. case 'x':
  2118. case 'X':
  2119. case 'e':
  2120. case 'E':
  2121. case 'f':
  2122. case 'g':
  2123. case 'G':
  2124. case 'a':
  2125. case 'A':
  2126. case 'n':
  2127. case 'p':
  2128. case 's':
  2129. case 'S':
  2130. tmp.Append(c);
  2131. PrintFParsedParam pp = new PrintFParsedParam(tmp.ToString());
  2132. string s;
  2133. if (pp.Ok)
  2134. {
  2135. s = pp.GetString(args[pos++]);
  2136. }
  2137. else
  2138. {
  2139. s = "(parse_error)";
  2140. }
  2141. o.Add(s);
  2142. tmp = new StringBuilder();
  2143. mode = 0;
  2144. break;
  2145. default:
  2146. tmp.Append(c);
  2147. break;
  2148. }
  2149. }
  2150. }
  2151. if (tmp.Length >= 1)
  2152. {
  2153. o.Add(tmp.ToString());
  2154. }
  2155. StringBuilder retstr = new StringBuilder();
  2156. foreach (string stmp in o)
  2157. {
  2158. retstr.Append(stmp);
  2159. }
  2160. return retstr.ToString();
  2161. }
  2162. static Encoding asciiEncoding = Encoding.ASCII;
  2163. public static Encoding AsciiEncoding
  2164. {
  2165. get { return asciiEncoding; }
  2166. }
  2167. static Encoding shiftJisEncoding = Encoding.GetEncoding("shift_jis");
  2168. public static Encoding ShiftJisEncoding
  2169. {
  2170. get { return shiftJisEncoding; }
  2171. }
  2172. static Encoding iso2022JpEncoding = Encoding.GetEncoding("ISO-2022-JP");
  2173. public static Encoding ISO2022JPEncoding
  2174. {
  2175. get { return iso2022JpEncoding; }
  2176. }
  2177. static Encoding eucJpEncoding = Encoding.GetEncoding("euc-jp");
  2178. public static Encoding EucJpEncoding
  2179. {
  2180. get { return eucJpEncoding; }
  2181. }
  2182. static Encoding iso88591Encoding = Encoding.GetEncoding("iso-8859-1");
  2183. public static Encoding ISO88591Encoding
  2184. {
  2185. get { return iso88591Encoding; }
  2186. }
  2187. static Encoding gb2312Encoding = Encoding.GetEncoding("gb2312");
  2188. public static Encoding GB2312Encoding
  2189. {
  2190. get { return gb2312Encoding; }
  2191. }
  2192. static Encoding utf8Encoding = Encoding.UTF8;
  2193. public static Encoding Utf8Encoding
  2194. {
  2195. get { return utf8Encoding; }
  2196. }
  2197. static Encoding uniEncoding = Encoding.Unicode;
  2198. public static Encoding UniEncoding
  2199. {
  2200. get { return uniEncoding; }
  2201. }
  2202. public static void NormalizeString(ref string str)
  2203. {
  2204. if (str == null)
  2205. {
  2206. str = "";
  2207. }
  2208. str = str.Trim();
  2209. }
  2210. public static string PasswordPrompt()
  2211. {
  2212. Queue<char> ret = new Queue<char>();
  2213. bool escape = false;
  2214. while (true)
  2215. {
  2216. ConsoleKeyInfo ki = Console.ReadKey(true);
  2217. char c = ki.KeyChar;
  2218. if (c >= 0x20 && c <= 0x7e)
  2219. {
  2220. ret.Enqueue(c);
  2221. Console.Write("*");
  2222. }
  2223. else if (c == 0x04 || c == 0x1a || c == 0x0d || c == 0x0a)
  2224. {
  2225. if (c == 0x04 || c == 0x1a)
  2226. {
  2227. escape = true;
  2228. }
  2229. break;
  2230. }
  2231. else if (c == 0x08)
  2232. {
  2233. Console.Write(c);
  2234. Console.Write(" ");
  2235. Console.Write(c);
  2236. if (ret.Count >= 1)
  2237. {
  2238. ret.Dequeue();
  2239. }
  2240. }
  2241. }
  2242. Console.WriteLine();
  2243. if (escape)
  2244. {
  2245. return null;
  2246. }
  2247. return new string(ret.ToArray());
  2248. }
  2249. public static bool CheckStrLen(string str, int maxLen)
  2250. {
  2251. if (str == null)
  2252. {
  2253. return false;
  2254. }
  2255. if (str.Length > maxLen)
  2256. {
  2257. return false;
  2258. }
  2259. return true;
  2260. }
  2261. public static bool IsSafe(string s)
  2262. {
  2263. foreach (char c in s)
  2264. {
  2265. if (IsSafe(c) == false)
  2266. {
  2267. return false;
  2268. }
  2269. }
  2270. return true;
  2271. }
  2272. public static bool IsSafe(char c)
  2273. {
  2274. char[] b = Path.GetInvalidFileNameChars();
  2275. foreach (char bb in b)
  2276. {
  2277. if (bb == c)
  2278. {
  2279. return false;
  2280. }
  2281. }
  2282. if (c == '\\' || c == '/')
  2283. {
  2284. return false;
  2285. }
  2286. return true;
  2287. }
  2288. public static string MakeSafePathName(string name)
  2289. {
  2290. char[] a = name.ToCharArray();
  2291. char[] b = Path.GetInvalidFileNameChars();
  2292. StringBuilder sb = new StringBuilder();
  2293. int i;
  2294. for (i = 0; i < a.Length; i++)
  2295. {
  2296. int j;
  2297. bool ok = true;
  2298. for (j = 0; j < b.Length; j++)
  2299. {
  2300. if (b[j] == a[i])
  2301. {
  2302. ok = false;
  2303. break;
  2304. }
  2305. }
  2306. if (a[i] == '\\' || a[i] == '/')
  2307. {
  2308. ok = true;
  2309. a[i] = '\\';
  2310. }
  2311. string s;
  2312. if (ok == false)
  2313. {
  2314. s = "_" + ((int)a[i]).ToString() + "_";
  2315. }
  2316. else
  2317. {
  2318. s = "" + a[i];
  2319. }
  2320. sb.Append(s);
  2321. }
  2322. return sb.ToString();
  2323. }
  2324. public static string MakeSafeFileName(string name)
  2325. {
  2326. char[] a = name.ToCharArray();
  2327. char[] b = Path.GetInvalidFileNameChars();
  2328. StringBuilder sb = new StringBuilder();
  2329. int i;
  2330. for (i = 0; i < a.Length; i++)
  2331. {
  2332. int j;
  2333. bool ok = true;
  2334. for (j = 0; j < b.Length; j++)
  2335. {
  2336. if (b[j] == a[i])
  2337. {
  2338. ok = false;
  2339. break;
  2340. }
  2341. }
  2342. string s;
  2343. if (ok == false)
  2344. {
  2345. s = "_" + ((int)a[i]).ToString() + "_";
  2346. }
  2347. else
  2348. {
  2349. s = "" + a[i];
  2350. }
  2351. sb.Append(s);
  2352. }
  2353. return sb.ToString();
  2354. }
  2355. public static object CloneObject(object o)
  2356. {
  2357. return BinaryToObject(ObjectToBinary(o));
  2358. }
  2359. public static object AnyToObject(byte[] data)
  2360. {
  2361. if (data.Length >= 5)
  2362. {
  2363. if (Str.StrCmpi(Encoding.ASCII.GetString(data, 0, 5), "<SOAP"))
  2364. {
  2365. return XMLDataToObject(data);
  2366. }
  2367. }
  2368. return BinaryToObject(data);
  2369. }
  2370. public static byte[] ObjectToBinary(object o)
  2371. {
  2372. BinaryFormatter f = new BinaryFormatter();
  2373. MemoryStream ms = new MemoryStream();
  2374. f.Serialize(ms, o);
  2375. return ms.ToArray();
  2376. }
  2377. public static object BinaryToObject(byte[] data)
  2378. {
  2379. BinaryFormatter f = new BinaryFormatter();
  2380. MemoryStream ms = new MemoryStream();
  2381. ms.Write(data, 0, data.Length);
  2382. ms.Position = 0;
  2383. return f.Deserialize(ms);
  2384. }
  2385. public static string ObjectToXMLString(object o)
  2386. {
  2387. SoapFormatter f = new SoapFormatter();
  2388. MemoryStream ms = new MemoryStream();
  2389. f.Serialize(ms, o);
  2390. ms.Position = 0;
  2391. StreamReader r = new StreamReader(ms);
  2392. return r.ReadToEnd();
  2393. }
  2394. public static byte[] ObjectToXMLData(object o)
  2395. {
  2396. SoapFormatter f = new SoapFormatter();
  2397. MemoryStream ms = new MemoryStream();
  2398. f.Serialize(ms, o);
  2399. ms.Position = 0;
  2400. return ms.ToArray();
  2401. }
  2402. public static object XMLStringToObject(string data)
  2403. {
  2404. SoapFormatter f = new SoapFormatter();
  2405. MemoryStream ms = new MemoryStream();
  2406. StreamWriter w = new StreamWriter(ms);
  2407. w.Write(data);
  2408. w.Flush();
  2409. ms.Position = 0;
  2410. return f.Deserialize(ms);
  2411. }
  2412. public static object XMLDataToObject(byte[] data)
  2413. {
  2414. SoapFormatter f = new SoapFormatter();
  2415. MemoryStream ms = new MemoryStream();
  2416. ms.Write(data, 0, data.Length);
  2417. ms.Position = 0;
  2418. return f.Deserialize(ms);
  2419. }
  2420. public static string CombineStringArray(string[] str)
  2421. {
  2422. return CombineStringArray(str, "");
  2423. }
  2424. public static string CombineStringArray(string[] str, string sepstr)
  2425. {
  2426. int i;
  2427. StringBuilder b = new StringBuilder();
  2428. for (i = 0; i < str.Length; i++)
  2429. {
  2430. string s = str[i];
  2431. b.Append(s);
  2432. if ((str.Length - 1) != i)
  2433. {
  2434. b.Append(sepstr);
  2435. }
  2436. }
  2437. return b.ToString();
  2438. }
  2439. public static string TruncStr(string str, int len)
  2440. {
  2441. if (str == null)
  2442. {
  2443. return "";
  2444. }
  2445. if (str.Length <= len)
  2446. {
  2447. return str;
  2448. }
  2449. else
  2450. {
  2451. return str.Substring(0, len);
  2452. }
  2453. }
  2454. public static string GenRandStr()
  2455. {
  2456. return ByteToStr(Secure.HashSHA1(Guid.NewGuid().ToByteArray()));
  2457. }
  2458. public static byte[] HashStr(string str)
  2459. {
  2460. return Secure.HashSHA1(Encoding.UTF8.GetBytes(str));
  2461. }
  2462. public static ulong HashStrToLong(string str)
  2463. {
  2464. Buf b = new Buf();
  2465. b.Write(HashStr(str));
  2466. b.SeekToBegin();
  2467. return b.ReadInt64();
  2468. }
  2469. public static string ByteToStr(byte[] data)
  2470. {
  2471. return ByteToStr(data, "");
  2472. }
  2473. public static string ByteToStr(byte[] data, string paddingStr)
  2474. {
  2475. StringBuilder sb = new StringBuilder();
  2476. int i;
  2477. for (i = 0; i < data.Length; i++)
  2478. {
  2479. byte b = data[i];
  2480. sb.Append(b.ToString("X2"));
  2481. if (i != (data.Length - 1))
  2482. {
  2483. sb.Append(paddingStr);
  2484. }
  2485. }
  2486. return sb.ToString();
  2487. }
  2488. public static string RandToStr6(string rand)
  2489. {
  2490. byte[] hash = HashStr(rand + "coreutil");
  2491. return ByteToStr(hash).Substring(0, 6);
  2492. }
  2493. public static bool IsAscii(char c)
  2494. {
  2495. if (c >= '0' && c <= '9')
  2496. {
  2497. return true;
  2498. }
  2499. if (c >= 'A' && c <= 'Z')
  2500. {
  2501. return true;
  2502. }
  2503. if (c >= 'a' && c <= 'z')
  2504. {
  2505. return true;
  2506. }
  2507. if (c == '!' || c == '\"' || c == '#' || c == '$' || c == '%' || c == '&' || c == '\'' ||
  2508. c == '(' || c == ')' || c == '-' || c == ' ' || c == '=' || c == '~' || c == '^' || c == '_' ||
  2509. c == '\\' || c == '|' || c == '{' || c == '}' || c == '[' || c == ']' || c == '@' ||
  2510. c == '*' || c == '+' || c == '.' || c == '<' || c == '>' ||
  2511. c == ',' || c == '?' || c == '/' || c == ' ' || c == '^' || c == '\'')
  2512. {
  2513. return true;
  2514. }
  2515. return false;
  2516. }
  2517. public static bool IsAscii(string str)
  2518. {
  2519. foreach (char c in str)
  2520. {
  2521. if (IsAscii(c) == false)
  2522. {
  2523. return false;
  2524. }
  2525. }
  2526. return true;
  2527. }
  2528. public static string GetBpsStr(int size)
  2529. {
  2530. return GetBpsStr(size);
  2531. }
  2532. public static string GetBpsStr(long size)
  2533. {
  2534. if (size >= 1000000000000L)
  2535. {
  2536. return ((double)(size) / 1000.0f / 1000.0f / 1000.0f / 1000.0f).ToString(".00") + " Tbps";
  2537. }
  2538. if (size >= 1000 * 1000 * 1000)
  2539. {
  2540. return ((double)(size) / 1000.0f / 1000.0f / 1000.0f).ToString(".00") + " Gbps";
  2541. }
  2542. if (size >= 1000 * 1000)
  2543. {
  2544. return ((double)(size) / 1000.0f / 1000.0f).ToString(".00") + " Mbps";
  2545. }
  2546. if (size >= 1000)
  2547. {
  2548. return ((double)(size) / 1000.0f).ToString(".00") + " Kbps";
  2549. }
  2550. return ((double)(size)).ToString() + " bps";
  2551. }
  2552. public static string GetFileSizeStr(int size)
  2553. {
  2554. return GetFileSizeStr(size);
  2555. }
  2556. public static string GetFileSizeStr(long size)
  2557. {
  2558. if (size >= 1099511627776L)
  2559. {
  2560. return ((double)(size) / 1024.0f / 1024.0f / 1024.0f / 1024.0f).ToString(".00") + " TB";
  2561. }
  2562. if (size >= 1024 * 1024 * 1024)
  2563. {
  2564. return ((double)(size) / 1024.0f / 1024.0f / 1024.0f).ToString(".00") + " GB";
  2565. }
  2566. if (size >= 1024 * 1024)
  2567. {
  2568. return ((double)(size) / 1024.0f / 1024.0f).ToString(".00") + " MB";
  2569. }
  2570. if (size >= 1024)
  2571. {
  2572. return ((double)(size) / 1024.0f).ToString(".00") + " KB";
  2573. }
  2574. return ((double)(size)).ToString() + " Bytes";
  2575. }
  2576. public static string IntToStr(int i)
  2577. {
  2578. return i.ToString();
  2579. }
  2580. public static string IntToStr(uint i)
  2581. {
  2582. return i.ToString();
  2583. }
  2584. public static string LongToStr(long i)
  2585. {
  2586. return i.ToString();
  2587. }
  2588. public static string LongToStr(ulong i)
  2589. {
  2590. return i.ToString();
  2591. }
  2592. public static bool StrToBool(string s)
  2593. {
  2594. if (s == null)
  2595. {
  2596. return false;
  2597. }
  2598. Str.NormalizeString(ref s, true, true, false, false);
  2599. if (s.StartsWith("y", StringComparison.InvariantCultureIgnoreCase))
  2600. {
  2601. return true;
  2602. }
  2603. if (s.StartsWith("t", StringComparison.InvariantCultureIgnoreCase))
  2604. {
  2605. return true;
  2606. }
  2607. if (Str.StrToInt(s) != 0)
  2608. {
  2609. return true;
  2610. }
  2611. return false;
  2612. }
  2613. public static int StrToInt(string str)
  2614. {
  2615. try
  2616. {
  2617. Str.RemoveSpaceChar(ref str);
  2618. Str.NormalizeString(ref str, true, true, false, false);
  2619. str = str.Replace(",", "");
  2620. return int.Parse(str);
  2621. }
  2622. catch
  2623. {
  2624. return 0;
  2625. }
  2626. }
  2627. public static uint StrToUInt(string str)
  2628. {
  2629. try
  2630. {
  2631. Str.RemoveSpaceChar(ref str);
  2632. Str.NormalizeString(ref str, true, true, false, false);
  2633. str = str.Replace(",", "");
  2634. return uint.Parse(str);
  2635. }
  2636. catch
  2637. {
  2638. return 0;
  2639. }
  2640. }
  2641. public static long StrToLong(string str)
  2642. {
  2643. try
  2644. {
  2645. Str.RemoveSpaceChar(ref str);
  2646. Str.NormalizeString(ref str, true, true, false, false);
  2647. str = str.Replace(",", "");
  2648. return long.Parse(str);
  2649. }
  2650. catch
  2651. {
  2652. return 0;
  2653. }
  2654. }
  2655. public static ulong StrToULong(string str)
  2656. {
  2657. try
  2658. {
  2659. Str.RemoveSpaceChar(ref str);
  2660. Str.NormalizeString(ref str, true, true, false, false);
  2661. str = str.Replace(",", "");
  2662. return ulong.Parse(str);
  2663. }
  2664. catch
  2665. {
  2666. return 0;
  2667. }
  2668. }
  2669. public static bool IsStrDateTime(string str)
  2670. {
  2671. try
  2672. {
  2673. Str.NormalizeString(ref str, true, true, false, false);
  2674. StrToDateTime(str);
  2675. return true;
  2676. }
  2677. catch
  2678. {
  2679. return false;
  2680. }
  2681. }
  2682. public static DateTime StrToDateTime(string str, bool toUtc)
  2683. {
  2684. return StrToDateTime(str).ToUniversalTime();
  2685. }
  2686. public static DateTime StrToDateTime(string str)
  2687. {
  2688. Str.NormalizeString(ref str, true, true, false, false);
  2689. str = str.Trim();
  2690. string[] sps =
  2691. {
  2692. " ",
  2693. "_",
  2694. " ",
  2695. "\t",
  2696. };
  2697. string[] tokens = str.Split(sps, StringSplitOptions.RemoveEmptyEntries);
  2698. if (tokens.Length != 2)
  2699. {
  2700. int r1 = str.IndexOf("年", StringComparison.InvariantCultureIgnoreCase);
  2701. int r2 = str.IndexOf("月", StringComparison.InvariantCultureIgnoreCase);
  2702. int r3 = str.IndexOf("日", StringComparison.InvariantCultureIgnoreCase);
  2703. if (r1 != -1 && r2 != -1 && r3 != -1)
  2704. {
  2705. tokens = new string[2];
  2706. tokens[0] = str.Substring(0, r3 + 1);
  2707. tokens[1] = str.Substring(r3 + 1);
  2708. }
  2709. }
  2710. if (tokens.Length == 2)
  2711. {
  2712. DateTime dt1 = StrToDate(tokens[0]);
  2713. DateTime dt2 = StrToTime(tokens[1]);
  2714. return dt1.Date + dt2.TimeOfDay;
  2715. }
  2716. else if (tokens.Length == 1)
  2717. {
  2718. if (tokens[0].Length == 14)
  2719. {
  2720. // yyyymmddhhmmss
  2721. DateTime dt1 = StrToDate(tokens[0].Substring(0, 8));
  2722. DateTime dt2 = StrToTime(tokens[0].Substring(8));
  2723. return dt1.Date + dt2.TimeOfDay;
  2724. }
  2725. else if (tokens[0].Length == 12)
  2726. {
  2727. // yymmddhhmmss
  2728. DateTime dt1 = StrToDate(tokens[0].Substring(0, 6));
  2729. DateTime dt2 = StrToTime(tokens[0].Substring(6));
  2730. return dt1.Date + dt2.TimeOfDay;
  2731. }
  2732. else
  2733. {
  2734. DateTime dt1 = StrToDate(tokens[0]);
  2735. return dt1.Date;
  2736. }
  2737. }
  2738. throw new ArgumentException();
  2739. }
  2740. public static bool IsStrTime(string str)
  2741. {
  2742. try
  2743. {
  2744. Str.NormalizeString(ref str, true, true, false, false);
  2745. StrToTime(str);
  2746. return true;
  2747. }
  2748. catch
  2749. {
  2750. return false;
  2751. }
  2752. }
  2753. public static DateTime StrToTime(string str, bool toUtc)
  2754. {
  2755. return StrToTime(str).ToUniversalTime();
  2756. }
  2757. public static DateTime StrToTime(string str)
  2758. {
  2759. string[] sps =
  2760. {
  2761. "/",
  2762. "-",
  2763. ":",
  2764. "時",
  2765. "分",
  2766. "秒",
  2767. };
  2768. Str.NormalizeString(ref str, true, true, false, false);
  2769. str = str.Trim();
  2770. string[] tokens;
  2771. tokens = str.Split(sps, StringSplitOptions.RemoveEmptyEntries);
  2772. if (tokens.Length == 3)
  2773. {
  2774. // hh:mm:ss
  2775. string hourStr = tokens[0];
  2776. string minuteStr = tokens[1];
  2777. string secondStr = tokens[2];
  2778. int hour = -1;
  2779. int minute = -1;
  2780. int second = -1;
  2781. if ((hourStr.Length == 1 || hourStr.Length == 2) && IsNumber(hourStr))
  2782. {
  2783. hour = StrToInt(hourStr);
  2784. }
  2785. if ((minuteStr.Length == 1 || minuteStr.Length == 2) && IsNumber(minuteStr))
  2786. {
  2787. minute = StrToInt(minuteStr);
  2788. }
  2789. if ((secondStr.Length == 1 || secondStr.Length == 2) && IsNumber(secondStr))
  2790. {
  2791. second = StrToInt(secondStr);
  2792. }
  2793. if (hour < 0 || hour >= 25 || minute < 0 || minute >= 60 || second < 0 || second >= 60)
  2794. {
  2795. throw new ArgumentException();
  2796. }
  2797. return new DateTime(2000, 1, 1, hour, minute, second);
  2798. }
  2799. else if (tokens.Length == 2)
  2800. {
  2801. // hh:mm
  2802. string hourStr = tokens[0];
  2803. string minuteStr = tokens[1];
  2804. int hour = -1;
  2805. int minute = -1;
  2806. int second = 0;
  2807. if ((hourStr.Length == 1 || hourStr.Length == 2) && IsNumber(hourStr))
  2808. {
  2809. hour = StrToInt(hourStr);
  2810. }
  2811. if ((minuteStr.Length == 1 || minuteStr.Length == 2) && IsNumber(minuteStr))
  2812. {
  2813. minute = StrToInt(minuteStr);
  2814. }
  2815. if (hour < 0 || hour >= 25 || minute < 0 || minute >= 60 || second < 0 || second >= 60)
  2816. {
  2817. throw new ArgumentException();
  2818. }
  2819. return new DateTime(2000, 1, 1, hour, minute, second);
  2820. }
  2821. else if (tokens.Length == 1)
  2822. {
  2823. string hourStr = tokens[0];
  2824. int hour = -1;
  2825. int minute = 0;
  2826. int second = 0;
  2827. if ((hourStr.Length == 1 || hourStr.Length == 2) && IsNumber(hourStr))
  2828. {
  2829. // hh
  2830. hour = StrToInt(hourStr);
  2831. }
  2832. else
  2833. {
  2834. if ((hourStr.Length == 4) && IsNumber(hourStr))
  2835. {
  2836. // hhmm
  2837. int i = StrToInt(hourStr);
  2838. hour = i / 100;
  2839. minute = i % 100;
  2840. }
  2841. else if ((hourStr.Length == 6) && IsNumber(hourStr))
  2842. {
  2843. // hhmmss
  2844. int i = StrToInt(hourStr);
  2845. hour = i / 10000;
  2846. minute = ((i % 10000) / 100);
  2847. second = i % 100;
  2848. }
  2849. }
  2850. if (hour < 0 || hour >= 25 || minute < 0 || minute >= 60 || second < 0 || second >= 60)
  2851. {
  2852. throw new ArgumentException();
  2853. }
  2854. return new DateTime(2000, 1, 1, hour, minute, second);
  2855. }
  2856. throw new ArgumentException();
  2857. }
  2858. public static bool IsStrDate(string str)
  2859. {
  2860. try
  2861. {
  2862. Str.NormalizeString(ref str, true, true, false, false);
  2863. StrToDate(str);
  2864. return true;
  2865. }
  2866. catch
  2867. {
  2868. return false;
  2869. }
  2870. }
  2871. public static DateTime StrToDate(string str, bool toUtc)
  2872. {
  2873. return StrToDate(str).ToUniversalTime();
  2874. }
  2875. public static DateTime StrToDate(string str)
  2876. {
  2877. string[] sps =
  2878. {
  2879. "/",
  2880. "/",
  2881. "-",
  2882. ":",
  2883. "年",
  2884. "月",
  2885. "日",
  2886. };
  2887. str = str.Trim();
  2888. Str.NormalizeString(ref str, true, true, false, false);
  2889. string[] youbi =
  2890. {
  2891. "月", "火", "水", "木", "金", "土", "日",
  2892. };
  2893. foreach (string ys in youbi)
  2894. {
  2895. string ys2 = string.Format("({0})", ys);
  2896. str = str.Replace(ys2, "");
  2897. }
  2898. string[] tokens;
  2899. tokens = str.Split(sps, StringSplitOptions.RemoveEmptyEntries);
  2900. if (tokens.Length == 3)
  2901. {
  2902. // yyyy/mm/dd
  2903. string yearStr = tokens[0];
  2904. string monthStr = tokens[1];
  2905. string dayStr = tokens[2];
  2906. int year = 0;
  2907. int month = 0;
  2908. int day = 0;
  2909. if ((yearStr.Length == 1 || yearStr.Length == 2) && IsNumber(yearStr))
  2910. {
  2911. year = 2000 + StrToInt(yearStr);
  2912. }
  2913. else if (yearStr.Length == 4 && IsNumber(yearStr))
  2914. {
  2915. year = StrToInt(yearStr);
  2916. }
  2917. if ((monthStr.Length == 1 || monthStr.Length == 2) && IsNumber(monthStr))
  2918. {
  2919. month = StrToInt(monthStr);
  2920. }
  2921. if ((dayStr.Length == 1 || dayStr.Length == 2) && IsNumber(dayStr))
  2922. {
  2923. day = StrToInt(dayStr);
  2924. }
  2925. if (year < 1800 || year >= 2100 || month <= 0 || month >= 13 || day <= 0 || day >= 32)
  2926. {
  2927. throw new ArgumentException();
  2928. }
  2929. return new DateTime(year, month, day);
  2930. }
  2931. else if (tokens.Length == 1)
  2932. {
  2933. if (str.Length == 8)
  2934. {
  2935. // yyyymmdd
  2936. string yearStr = str.Substring(0, 4);
  2937. string monthStr = str.Substring(4, 2);
  2938. string dayStr = str.Substring(6, 2);
  2939. int year = int.Parse(yearStr);
  2940. int month = int.Parse(monthStr);
  2941. int day = int.Parse(dayStr);
  2942. if (year < 1800 || year >= 2100 || month <= 0 || month >= 13 || day <= 0 || day >= 32)
  2943. {
  2944. throw new ArgumentException();
  2945. }
  2946. return new DateTime(year, month, day);
  2947. }
  2948. else if (str.Length == 6)
  2949. {
  2950. // yymmdd
  2951. string yearStr = str.Substring(0, 2);
  2952. string monthStr = str.Substring(2, 2);
  2953. string dayStr = str.Substring(4, 2);
  2954. int year = int.Parse(yearStr) + 2000;
  2955. int month = int.Parse(monthStr);
  2956. int day = int.Parse(dayStr);
  2957. if (year < 1800 || year >= 2100 || month <= 0 || month >= 13 || day <= 0 || day >= 32)
  2958. {
  2959. throw new ArgumentException();
  2960. }
  2961. return new DateTime(year, month, day);
  2962. }
  2963. }
  2964. throw new ArgumentException();
  2965. }
  2966. public static string TimeToStr(DateTime dt)
  2967. {
  2968. return TimeToStr(dt, false);
  2969. }
  2970. public static string TimeToStr(DateTime dt, CoreLanguage lang)
  2971. {
  2972. return TimeToStr(dt, false, lang);
  2973. }
  2974. public static string TimeToStr(DateTime dt, bool toLocalTime)
  2975. {
  2976. return TimeToStr(dt, toLocalTime, CoreLanguageClass.CurrentThreadLanguage);
  2977. }
  2978. public static string TimeToStr(DateTime dt, bool toLocalTime, CoreLanguage lang)
  2979. {
  2980. string s = DateTimeToStr(dt, toLocalTime, lang);
  2981. string[] tokens = s.Split(' ');
  2982. return tokens[1];
  2983. }
  2984. public static string TimeToStrShort(DateTime dt)
  2985. {
  2986. return TimeToStrShort(dt, false);
  2987. }
  2988. public static string TimeToStrShort(DateTime dt, bool toLocalTime)
  2989. {
  2990. string s = DateTimeToStrShort(dt, toLocalTime);
  2991. string[] tokens = s.Split('_');
  2992. return tokens[1];
  2993. }
  2994. public static string DateToStr(DateTime dt)
  2995. {
  2996. return DateToStr(dt, false);
  2997. }
  2998. public static string DateToStr(DateTime dt, CoreLanguage lang)
  2999. {
  3000. return DateToStr(dt, false, lang);
  3001. }
  3002. public static string DateToStr(DateTime dt, bool toLocalTime)
  3003. {
  3004. return DateToStr(dt, toLocalTime, false);
  3005. }
  3006. public static string DateToStr(DateTime dt, bool toLocalTime, CoreLanguage lang)
  3007. {
  3008. return DateToStr(dt, toLocalTime, false, lang);
  3009. }
  3010. public static string DateToStr(DateTime dt, bool toLocalTime, bool noDayOfWeek)
  3011. {
  3012. return DateToStr(dt, toLocalTime, noDayOfWeek, CoreLanguageClass.CurrentThreadLanguage);
  3013. }
  3014. public static string DateToStr(DateTime dt, bool toLocalTime, bool noDayOfWeek, CoreLanguage lang)
  3015. {
  3016. string s = DateTimeToStr(dt, toLocalTime, lang);
  3017. string[] tokens = s.Split(' ');
  3018. string ret = tokens[0];
  3019. if (noDayOfWeek)
  3020. {
  3021. string[] tokens2 = s.Split('(');
  3022. ret = tokens2[0];
  3023. }
  3024. return ret;
  3025. }
  3026. public static string DateToStrShort(DateTime dt)
  3027. {
  3028. return DateToStrShort(dt, false);
  3029. }
  3030. public static string DateToStrShort(DateTime dt, bool toLocalTime)
  3031. {
  3032. string s = DateTimeToStrShort(dt, toLocalTime);
  3033. string[] tokens = s.Split('_');
  3034. return tokens[0];
  3035. }
  3036. public static string DayOfWeekToStr(CoreLanguage lang, int d)
  3037. {
  3038. if (lang == CoreLanguage.Japanese)
  3039. {
  3040. string[] youbi =
  3041. {
  3042. "日", "月", "火", "水", "木", "金", "土",
  3043. };
  3044. return youbi[d];
  3045. }
  3046. else
  3047. {
  3048. string[] youbi =
  3049. {
  3050. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
  3051. };
  3052. return youbi[d];
  3053. }
  3054. }
  3055. public static string DateTimeToStr(DateTime dt)
  3056. {
  3057. return DateTimeToStr(dt, false);
  3058. }
  3059. public static string DateTimeToStr(DateTime dt, CoreLanguage lang)
  3060. {
  3061. return DateTimeToStr(dt, false, lang);
  3062. }
  3063. public static string DateTimeToStr(DateTime dt, bool toLocalTime)
  3064. {
  3065. return DateTimeToStr(dt, toLocalTime, CoreLanguageClass.CurrentThreadLanguage);
  3066. }
  3067. public static string DateTimeToStr(DateTime dt, bool toLocalTime, CoreLanguage lang)
  3068. {
  3069. if (toLocalTime)
  3070. {
  3071. dt = dt.ToLocalTime();
  3072. }
  3073. if (lang == CoreLanguage.Japanese)
  3074. {
  3075. return dt.ToString("yyyy年M月d日") + "(" + DayOfWeekToStr(lang, (int)dt.DayOfWeek) + ")" + dt.ToString(" H時m分s秒");
  3076. }
  3077. else
  3078. {
  3079. return dt.ToString("yyyy-MM-dd(") + DayOfWeekToStr(lang, (int)dt.DayOfWeek) + dt.ToString(") H:mm:ss");
  3080. }
  3081. }
  3082. public static string DateTimeToStrShort(DateTime dt)
  3083. {
  3084. return DateTimeToStrShort(dt, false);
  3085. }
  3086. public static string DateTimeToStrShort(DateTime dt, bool toLocalTime)
  3087. {
  3088. if (toLocalTime)
  3089. {
  3090. dt = dt.ToLocalTime();
  3091. }
  3092. return dt.ToString("yyyyMMdd_HHmmss");
  3093. }
  3094. public static string DateTimeToStrShortWithMilliSecs(DateTime dt)
  3095. {
  3096. return DateTimeToStrShortWithMilliSecs(dt, false);
  3097. }
  3098. public static string DateTimeToStrShortWithMilliSecs(DateTime dt, bool toLocalTime)
  3099. {
  3100. if (toLocalTime)
  3101. {
  3102. dt = dt.ToLocalTime();
  3103. }
  3104. long ticks = dt.Ticks % 10000000;
  3105. if (ticks >= 9990000)
  3106. {
  3107. ticks = 9990000;
  3108. }
  3109. string msecStr = ((decimal)ticks / (decimal)10000000).ToString(".000");
  3110. return dt.ToString("yyyyMMdd_HHmmss") + "." + msecStr.Split('.')[1];
  3111. }
  3112. public static string Base64ToSafe64(string str)
  3113. {
  3114. return str.Replace('=', '(').Replace('+', ')').Replace('/', '_');
  3115. }
  3116. public static string Safe64ToBase64(string str)
  3117. {
  3118. return str.Replace('(', '=').Replace(')', '+').Replace('_', '/');
  3119. }
  3120. public static string Base64Encode(byte[] data)
  3121. {
  3122. try
  3123. {
  3124. return Convert.ToBase64String(data);
  3125. }
  3126. catch
  3127. {
  3128. return "";
  3129. }
  3130. }
  3131. public static byte[] Base64Decode(string str)
  3132. {
  3133. try
  3134. {
  3135. return Convert.FromBase64String(str);
  3136. }
  3137. catch
  3138. {
  3139. return new byte[0];
  3140. }
  3141. }
  3142. public static byte[] StrToByte(string str)
  3143. {
  3144. Str.NormalizeString(ref str, true, true, false, false);
  3145. return Base64Decode(Safe64ToBase64(str));
  3146. }
  3147. public static string Encode64(string str)
  3148. {
  3149. return Convert.ToBase64String(Encoding.UTF8.GetBytes(str)).Replace("/", "(").Replace("+", ")");
  3150. }
  3151. public static string Decode64(string str)
  3152. {
  3153. return Encoding.UTF8.GetString(Convert.FromBase64String(str.Replace(")", "+").Replace("(", "/")));
  3154. }
  3155. public static bool CheckMailAddress(string str)
  3156. {
  3157. str = str.Trim();
  3158. if (str.Length == 0)
  3159. {
  3160. return false;
  3161. }
  3162. string[] tokens = str.Split('@');
  3163. if (tokens.Length != 2)
  3164. {
  3165. return false;
  3166. }
  3167. string a = tokens[0];
  3168. string b = tokens[1];
  3169. if (a.Length == 0 || b.Length == 0)
  3170. {
  3171. return false;
  3172. }
  3173. if (b.IndexOf(".") == -1)
  3174. {
  3175. return false;
  3176. }
  3177. return IsAscii(str);
  3178. }
  3179. public static bool StrCmpi(string s1, string s2)
  3180. {
  3181. try
  3182. {
  3183. if (s1.Equals(s2, StringComparison.InvariantCultureIgnoreCase))
  3184. {
  3185. return true;
  3186. }
  3187. return false;
  3188. }
  3189. catch
  3190. {
  3191. return false;
  3192. }
  3193. }
  3194. public static bool StrCmp(string s1, string s2)
  3195. {
  3196. try
  3197. {
  3198. if (s1 == s2)
  3199. {
  3200. return true;
  3201. }
  3202. return false;
  3203. }
  3204. catch
  3205. {
  3206. return false;
  3207. }
  3208. }
  3209. public static string ByteToHex(byte[] data)
  3210. {
  3211. return ByteToHex(data, "");
  3212. }
  3213. public static string ByteToHex(byte[] data, string paddingStr)
  3214. {
  3215. StringBuilder ret = new StringBuilder();
  3216. foreach (byte b in data)
  3217. {
  3218. string s = b.ToString("X");
  3219. if (s.Length == 1)
  3220. {
  3221. s = "0" + s;
  3222. }
  3223. ret.Append(s);
  3224. if (paddingStr != null)
  3225. {
  3226. ret.Append(paddingStr);
  3227. }
  3228. }
  3229. return ret.ToString().Trim();
  3230. }
  3231. public static byte[] HexToByte(string str)
  3232. {
  3233. try
  3234. {
  3235. List<byte> o = new List<byte>();
  3236. string tmp = "";
  3237. int i, len;
  3238. str = str.ToUpper().Trim();
  3239. len = str.Length;
  3240. for (i = 0; i < len; i++)
  3241. {
  3242. char c = str[i];
  3243. if (('0' <= c && c <= '9') || ('A' <= c && c <= 'F'))
  3244. {
  3245. tmp += c;
  3246. if (tmp.Length == 2)
  3247. {
  3248. byte b = Convert.ToByte(tmp, 16);
  3249. o.Add(b);
  3250. tmp = "";
  3251. }
  3252. }
  3253. else if (c == ' ' || c == ',' || c == '-' || c == ';')
  3254. {
  3255. }
  3256. else
  3257. {
  3258. break;
  3259. }
  3260. }
  3261. return o.ToArray();
  3262. }
  3263. catch
  3264. {
  3265. return new byte[0];
  3266. }
  3267. }
  3268. public static string[] GetLines(string str)
  3269. {
  3270. List<string> a = new List<string>();
  3271. StringReader sr = new StringReader(str);
  3272. while (true)
  3273. {
  3274. string s = sr.ReadLine();
  3275. if (s == null)
  3276. {
  3277. break;
  3278. }
  3279. a.Add(s);
  3280. }
  3281. return a.ToArray();
  3282. }
  3283. public static string LinesToStr(string[] lines)
  3284. {
  3285. StringWriter sw = new StringWriter();
  3286. foreach (string s in lines)
  3287. {
  3288. sw.WriteLine(s);
  3289. }
  3290. return sw.ToString();
  3291. }
  3292. public static bool IsEmptyStr(string str)
  3293. {
  3294. if (str == null || str.Trim().Length == 0)
  3295. {
  3296. return true;
  3297. }
  3298. else
  3299. {
  3300. return false;
  3301. }
  3302. }
  3303. public static bool IsSolidStr(string str)
  3304. {
  3305. return !IsEmptyStr(str);
  3306. }
  3307. public static bool IsSplitChar(char c, string splitStr)
  3308. {
  3309. if (splitStr == null)
  3310. {
  3311. splitStr = StrToken.DefaultSplitStr;
  3312. }
  3313. foreach (char t in splitStr)
  3314. {
  3315. string a = "" + t;
  3316. string b = "" + c;
  3317. if (Str.StrCmpi(a, b))
  3318. {
  3319. return true;
  3320. }
  3321. }
  3322. return false;
  3323. }
  3324. public static bool GetKeyAndValue(string str, out string key, out string value)
  3325. {
  3326. return GetKeyAndValue(str, out key, out value, null);
  3327. }
  3328. public static bool GetKeyAndValue(string str, out string key, out string value, string splitStr)
  3329. {
  3330. uint mode = 0;
  3331. string keystr = "", valuestr = "";
  3332. if (splitStr == null)
  3333. {
  3334. splitStr = StrToken.DefaultSplitStr;
  3335. }
  3336. foreach (char c in str)
  3337. {
  3338. switch (mode)
  3339. {
  3340. case 0:
  3341. if (IsSplitChar(c, splitStr) == false)
  3342. {
  3343. mode = 1;
  3344. keystr += c;
  3345. }
  3346. break;
  3347. case 1:
  3348. if (IsSplitChar(c, splitStr) == false)
  3349. {
  3350. keystr += c;
  3351. }
  3352. else
  3353. {
  3354. mode = 2;
  3355. }
  3356. break;
  3357. case 2:
  3358. if (IsSplitChar(c, splitStr) == false)
  3359. {
  3360. mode = 3;
  3361. valuestr += c;
  3362. }
  3363. break;
  3364. case 3:
  3365. valuestr += c;
  3366. break;
  3367. }
  3368. }
  3369. if (mode == 0)
  3370. {
  3371. value = "";
  3372. key = "";
  3373. return false;
  3374. }
  3375. else
  3376. {
  3377. value = valuestr;
  3378. key = keystr;
  3379. return true;
  3380. }
  3381. }
  3382. public static int StrCmpRetInt(string s1, string s2)
  3383. {
  3384. return string.Compare(s1, s2, false);
  3385. }
  3386. public static int StrCmpiRetInt(string s1, string s2)
  3387. {
  3388. return string.Compare(s1, s2, true);
  3389. }
  3390. public static bool IsStrInList(string str, params string[] args)
  3391. {
  3392. return IsStrInList(str, true, args);
  3393. }
  3394. public static bool IsStrInList(string str, bool ignoreCase, params string[] args)
  3395. {
  3396. foreach (string s in args)
  3397. {
  3398. if (ignoreCase)
  3399. {
  3400. if (StrCmpi(str, s))
  3401. {
  3402. return true;
  3403. }
  3404. }
  3405. else
  3406. {
  3407. if (StrCmp(str, s))
  3408. {
  3409. return true;
  3410. }
  3411. }
  3412. }
  3413. return false;
  3414. }
  3415. public static bool IsDouble(string str)
  3416. {
  3417. double v;
  3418. Str.NormalizeString(ref str, true, true, false, false);
  3419. str = str.Replace(",", "");
  3420. return double.TryParse(str, out v);
  3421. }
  3422. public static bool IsLong(string str)
  3423. {
  3424. long v;
  3425. Str.RemoveSpaceChar(ref str);
  3426. Str.NormalizeString(ref str, true, true, false, false);
  3427. str = str.Replace(",", "");
  3428. return long.TryParse(str, out v);
  3429. }
  3430. public static bool IsInt(string str)
  3431. {
  3432. int v;
  3433. Str.RemoveSpaceChar(ref str);
  3434. Str.NormalizeString(ref str, true, true, false, false);
  3435. str = str.Replace(",", "");
  3436. return int.TryParse(str, out v);
  3437. }
  3438. public static bool IsNumber(string str)
  3439. {
  3440. str = str.Trim();
  3441. Str.RemoveSpaceChar(ref str);
  3442. Str.NormalizeString(ref str, true, true, false, false);
  3443. str = str.Replace(",", "");
  3444. foreach (char c in str)
  3445. {
  3446. if (IsNumber(c) == false)
  3447. {
  3448. return false;
  3449. }
  3450. }
  3451. return true;
  3452. }
  3453. public static bool IsNumber(char c)
  3454. {
  3455. if (c >= '0' && c <= '9')
  3456. {
  3457. }
  3458. else if (c == '-')
  3459. {
  3460. }
  3461. else
  3462. {
  3463. return false;
  3464. }
  3465. return true;
  3466. }
  3467. public static bool InStr(string str, string keyword)
  3468. {
  3469. return InStr(str, keyword, false);
  3470. }
  3471. public static bool InStr(string str, string keyword, bool caseSensitive)
  3472. {
  3473. if (str.IndexOf(keyword, (caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase)) == -1)
  3474. {
  3475. return false;
  3476. }
  3477. return true;
  3478. }
  3479. public static string MakeCharArray(char c, int len)
  3480. {
  3481. return new string(c, len);
  3482. }
  3483. public static string NormalizeCrlf(string str)
  3484. {
  3485. return NormalizeCrlf(str, new byte[] { 13, 10 });
  3486. }
  3487. public static string NormalizeCrlf(string str, byte[] crlfData)
  3488. {
  3489. byte[] srcData = Str.Utf8Encoding.GetBytes(str);
  3490. byte[] destData = NormalizeCrlf(srcData, crlfData);
  3491. return Str.Utf8Encoding.GetString(destData);
  3492. }
  3493. public static byte[] NormalizeCrlf(byte[] srcData)
  3494. {
  3495. return NormalizeCrlf(srcData, new byte[] { 13, 10 });
  3496. }
  3497. public static byte[] NormalizeCrlf(byte[] srcData, byte[] crlfData)
  3498. {
  3499. Buf ret = new Buf();
  3500. int i;
  3501. Buf b = new Buf();
  3502. for (i = 0; i < srcData.Length; i++)
  3503. {
  3504. bool isNewLine = false;
  3505. if (srcData[i] == 13)
  3506. {
  3507. if (i < (srcData.Length - 1) && srcData[i + 1] == 10)
  3508. {
  3509. i++;
  3510. }
  3511. isNewLine = true;
  3512. }
  3513. else if (srcData[i] == 10)
  3514. {
  3515. isNewLine = true;
  3516. }
  3517. if (isNewLine)
  3518. {
  3519. ret.Write(b.ByteData);
  3520. ret.Write(crlfData);
  3521. b.Clear();
  3522. }
  3523. else
  3524. {
  3525. b.WriteByte(srcData[i]);
  3526. }
  3527. }
  3528. ret.Write(b.ByteData);
  3529. return ret.ByteData;
  3530. }
  3531. public static string[] UniqueToken(string[] t)
  3532. {
  3533. Dictionary<string, object> o = new Dictionary<string, object>();
  3534. List<string> ret = new List<string>();
  3535. foreach (string s in t)
  3536. {
  3537. string key = s.ToUpper();
  3538. if (o.ContainsKey(key) == false)
  3539. {
  3540. o.Add(key, new Object());
  3541. ret.Add(s);
  3542. }
  3543. }
  3544. return ret.ToArray();
  3545. }
  3546. public static string ToStr3(long v)
  3547. {
  3548. bool neg = false;
  3549. if (v < 0)
  3550. {
  3551. neg = true;
  3552. v = v * (long)-1;
  3553. }
  3554. string tmp, tmp2;
  3555. int i;
  3556. tmp = Str.LongToStr(v);
  3557. tmp2 = "";
  3558. for (i = tmp.Length - 1; i >= 0; i--)
  3559. {
  3560. tmp2 += tmp[i];
  3561. }
  3562. int len = tmp.Length;
  3563. tmp = "";
  3564. for (i = 0; i < len; i++)
  3565. {
  3566. if (i != 0 && (i % 3) == 0)
  3567. {
  3568. tmp += ",";
  3569. }
  3570. tmp += tmp2[i];
  3571. }
  3572. char[] array = tmp.ToCharArray();
  3573. Array.Reverse(array);
  3574. string str = new string(array);
  3575. if (neg)
  3576. {
  3577. str = "-" + str;
  3578. }
  3579. return str;
  3580. }
  3581. public static string[] ParseCmdLine(string str)
  3582. {
  3583. List<string> o;
  3584. int i, len, mode;
  3585. string tmp;
  3586. bool ignoreSpace = false;
  3587. o = new List<string>();
  3588. mode = 0;
  3589. len = str.Length;
  3590. tmp = "";
  3591. for (i = 0; i < len; i++)
  3592. {
  3593. char c = str[i];
  3594. switch (mode)
  3595. {
  3596. case 0:
  3597. if (c == ' ' || c == '\t')
  3598. {
  3599. }
  3600. else
  3601. {
  3602. if (c == '\"')
  3603. {
  3604. if ((i != (len - 1)) && str[i + 1] == '\"')
  3605. {
  3606. tmp += '\"';
  3607. i++;
  3608. }
  3609. else
  3610. {
  3611. ignoreSpace = true;
  3612. }
  3613. }
  3614. else
  3615. {
  3616. tmp += c;
  3617. }
  3618. mode = 1;
  3619. }
  3620. break;
  3621. case 1:
  3622. if (ignoreSpace == false && (c == ' ' || c == '\t'))
  3623. {
  3624. o.Add(tmp);
  3625. tmp = "";
  3626. mode = 0;
  3627. }
  3628. else
  3629. {
  3630. if (c == '\"')
  3631. {
  3632. if ((i != (len - 1)) && str[i + 1] == '\"')
  3633. {
  3634. tmp += '\"';
  3635. i++;
  3636. }
  3637. else
  3638. {
  3639. if (ignoreSpace == false)
  3640. {
  3641. ignoreSpace = true;
  3642. }
  3643. else
  3644. {
  3645. ignoreSpace = false;
  3646. }
  3647. }
  3648. }
  3649. else
  3650. {
  3651. tmp += c;
  3652. }
  3653. }
  3654. break;
  3655. }
  3656. }
  3657. if (tmp.Length != 0)
  3658. {
  3659. o.Add(tmp);
  3660. tmp = "";
  3661. }
  3662. return o.ToArray();
  3663. }
  3664. public static string ObjectToXMLSimple(object o)
  3665. {
  3666. return ObjectToXMLSimple(o, o.GetType());
  3667. }
  3668. public static string ObjectToXMLSimple(object o, Type t)
  3669. {
  3670. XmlSerializer xs = new XmlSerializer(t);
  3671. MemoryStream ms = new MemoryStream();
  3672. xs.Serialize(ms, o);
  3673. return Str.Utf8Encoding.GetString(ms.ToArray());
  3674. }
  3675. public static object XMLToObjectSimple(string str, Type t)
  3676. {
  3677. XmlSerializer xs = new XmlSerializer(t);
  3678. MemoryStream ms = new MemoryStream();
  3679. byte[] data = Str.Utf8Encoding.GetBytes(str);
  3680. ms.Write(data, 0, data.Length);
  3681. ms.Position = 0;
  3682. return xs.Deserialize(ms);
  3683. }
  3684. public static bool IsStrOkForXML(string str)
  3685. {
  3686. try
  3687. {
  3688. XmlCheckObjectInternal o = new XmlCheckObjectInternal();
  3689. o.Str = str;
  3690. string xmlstr = ObjectToXMLSimple(o);
  3691. XMLToObjectSimple(xmlstr, typeof(XmlCheckObjectInternal));
  3692. return true;
  3693. }
  3694. catch
  3695. {
  3696. return false;
  3697. }
  3698. }
  3699. }
  3700. public class XmlCheckObjectInternal
  3701. {
  3702. public string Str;
  3703. }
  3704. public class StrToken
  3705. {
  3706. string[] tokens;
  3707. public string[] Tokens
  3708. {
  3709. get { return tokens; }
  3710. }
  3711. public string this[uint index]
  3712. {
  3713. get { return tokens[index]; }
  3714. }
  3715. public uint NumTokens
  3716. {
  3717. get
  3718. {
  3719. return (uint)Tokens.Length;
  3720. }
  3721. }
  3722. const string defaultSplitStr = " ,\t\r\n";
  3723. public static string DefaultSplitStr
  3724. {
  3725. get { return defaultSplitStr; }
  3726. }
  3727. public StrToken(string[] tokens)
  3728. {
  3729. List<string> a = new List<string>();
  3730. foreach (string s in tokens)
  3731. {
  3732. a.Add(s);
  3733. }
  3734. this.tokens = a.ToArray();
  3735. }
  3736. public StrToken(string str)
  3737. : this(str, null)
  3738. {
  3739. }
  3740. public StrToken(string str, string splitStr)
  3741. {
  3742. if (splitStr == null)
  3743. {
  3744. splitStr = defaultSplitStr;
  3745. }
  3746. int i, len;
  3747. len = splitStr.Length;
  3748. char[] chars = new char[len];
  3749. for (i = 0; i < len; i++)
  3750. {
  3751. chars[i] = splitStr[i];
  3752. }
  3753. tokens = str.Split(chars, StringSplitOptions.RemoveEmptyEntries);
  3754. }
  3755. }
  3756. public class StrData
  3757. {
  3758. string strValue;
  3759. public string StrValue
  3760. {
  3761. get { return strValue; }
  3762. }
  3763. public uint IntValue
  3764. {
  3765. get
  3766. {
  3767. return Str.StrToUInt(strValue);
  3768. }
  3769. }
  3770. public ulong Int64Value
  3771. {
  3772. get
  3773. {
  3774. return Str.StrToULong(strValue);
  3775. }
  3776. }
  3777. public bool BoolValue
  3778. {
  3779. get
  3780. {
  3781. string s = strValue.Trim();
  3782. if (Str.IsEmptyStr(s))
  3783. {
  3784. return false;
  3785. }
  3786. if (s.StartsWith("true", StringComparison.CurrentCultureIgnoreCase))
  3787. {
  3788. return true;
  3789. }
  3790. if ("true".StartsWith(s, StringComparison.CurrentCultureIgnoreCase))
  3791. {
  3792. return true;
  3793. }
  3794. if (s.StartsWith("yes", StringComparison.CurrentCultureIgnoreCase))
  3795. {
  3796. return true;
  3797. }
  3798. if ("yes".StartsWith(s, StringComparison.CurrentCultureIgnoreCase))
  3799. {
  3800. return true;
  3801. }
  3802. if (Str.StrToUInt(s) != 0)
  3803. {
  3804. return true;
  3805. }
  3806. return false;
  3807. }
  3808. }
  3809. public StrData(string str)
  3810. {
  3811. if (str == null)
  3812. {
  3813. str = "";
  3814. }
  3815. strValue = str;
  3816. }
  3817. }
  3818. }
  3819. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  3820. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  3821. // Join us: http://www.tsukuba.ac.jp/english/admission/