OpenKeyWord  Version: 426, Datum:
OKW_Helper.cs
1 #region Header
2 /*
3  ==============================================================================
4  Author: Zoltan Hrabovszki <zh@openkeyword.de>
5 
6  Copyright © 2012, 2013, 2014, 2015 IT-Beratung Hrabovszki
7  www.OpenKeyWord.de
8  ==============================================================================
9 
10  This file is part of OpenKeyWord.
11 
12  OpenKeyWord is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  OpenKeyWord is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with OpenKeyWord. If not, see <http://www.gnu.org/licenses/>.
24 
25  Diese Datei ist Teil von OpenKeyWord.
26 
27  OpenKeyWord ist Freie Software: Sie können es unter den Bedingungen
28  der GNU General Public License, wie von der Free Software Foundation,
29  Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren
30  veröffentlichten Version, weiterverbreiten und/oder modifizieren.
31 
32  OpenKeyWord wird in der Hoffnung, dass es nützlich sein wird, aber
33  OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
34  Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
35  Siehe die GNU General Public License für weitere Details.
36 
37  Sie sollten eine Kopie der GNU General Public License zusammen mit
38  OpenKeyWord erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
39 */
40 #endregion Header
41 
42 namespace OKW
43 {
44  using System;
45  using System.Collections.Generic;
46  using System.Text.RegularExpressions;
47  using System.Windows.Forms;
48  using OKW.Log;
49 
63  public static class OKW_Helper
64  {
65  #region Fields
66 
72  private static Logger Log = Logger.Instance;
73 
79  private static LogMessenger LM = new LogMessenger("OKWHelper");
80 
81  #endregion Fields
82 
83  #region Methods
84 
108  public static string Boolean2String(bool fpbTrueOrFalse)
109  {
110  string lvsReturn = string.Empty;
111 
112  Logger.Instance.LogFunctionStartDebug("OKW_Helper.Boolean2String", "fpbTrueOrFalse", fpbTrueOrFalse.ToString());
113 
114  try
115  {
116  if (fpbTrueOrFalse)
117  {
118  lvsReturn = "true";
119  }
120  else
121  {
122  lvsReturn = "false";
123  }
124  }
125  finally
126  {
127  Logger.Instance.LogFunctionEndDebug(lvsReturn);
128  }
129 
130  return lvsReturn;
131  }
132 
182  public static string GetLeftFromDelimiterNumber(string fpsSource, string fpsDelimiter, int fpiCount)
183  {
184  string lvsReturn = string.Empty;
185  List<string> lvLsSplitedSource = null;
186 
187  bool bOK = false;
188 
189  int RangeMin = 1;
190  int RangeMax = 1;
191 
192  try
193  {
194  Log.LogFunctionStartDebug("OKW_Helper.GetLeftFromDelimiterNumber", "fpsSource", fpsSource, "fpsDelimiter", fpsDelimiter, "fpiCount", fpiCount.ToString());
195 
196  if (fpsDelimiter != string.Empty)
197  {
198  lvLsSplitedSource = StrSplit(fpsSource, fpsDelimiter);
199 
200  // Gültigen Werte bereich berechnen:
201  RangeMax = lvLsSplitedSource.Count - 1;
202  RangeMin = 1;
203 
204  // 1. Prüfen ob Split Zahl <= Count, anderefalls Exception auslösen...
205  if ((RangeMin <= fpiCount) & (fpiCount <= RangeMax))
206  {
207  // 2. Dann bauen wir den string auf
208  lvsReturn = lvLsSplitedSource[0];
209 
210  for (int i = 1; i < fpiCount; i++)
211  {
212  lvsReturn = lvsReturn + fpsDelimiter + lvLsSplitedSource[i];
213  }
214  }
215  else
216  {
217  string lvsLM = LM.GetMessage("GetLeftFromDelimiterNumber", "OKWDelimiterCountOutOfRangeException", fpiCount.ToString(), RangeMax.ToString());
218  throw new System.IndexOutOfRangeException(lvsLM);
219  }
220  }
221  else
222  {
223  string lvsLM = LM.GetMessage("GetLeftFromDelimiterNumber", "OKWDelimiterIsEmptyException");
224  throw new System.ArgumentException(lvsLM);
225  }
226 
227  bOK = true;
228  }
229  finally
230  {
231  if (bOK)
232  {
233  Log.LogFunctionEndDebug(lvsReturn);
234  }
235  else
236  {
237  Log.LogFunctionEndDebug();
238  }
239  }
240 
241  return lvsReturn;
242  }
243 
284  public static string GetRigthFromDelimiterNumber(string fpsSource, string fpsDelimiter, int fpiCount)
285  {
286  string lvsReturn = string.Empty;
287  List<string> lvLsSplitedSource = null;
288  bool bOK = false;
289  int RangeMin = 1;
290  int RangeMax = 1;
291  try
292  {
293  Log.LogFunctionStartDebug("OKW_Helper.GetLeftFromDelimiterNumber", "fpsSource", fpsSource, "fpsDelimiter", fpsDelimiter, "fpiCount", fpiCount.ToString());
294  if (fpsDelimiter != string.Empty)
295  {
296  lvLsSplitedSource = StrSplit(fpsSource, fpsDelimiter);
297 
298  // Gültigen Werte bereich berechnen:
299  RangeMax = lvLsSplitedSource.Count - 1;
300  RangeMin = 1;
301 
302  // 1. Prüfen ob Split Zahl <= Count, anderefalls Exception auslösen...
303  if ((RangeMin <= fpiCount) & (fpiCount <= RangeMax))
304  {
305  // 2. Dann bauen wir den string auf
306  lvsReturn = lvLsSplitedSource[fpiCount];
307 
308  for (int i = fpiCount + 1; i < lvLsSplitedSource.Count; i++)
309  {
310  lvsReturn = lvsReturn + fpsDelimiter + lvLsSplitedSource[i];
311  }
312  }
313  else
314  {
315  string lvsLM = LM.GetMessage("GetLeftFromDelimiterNumber", "OKWDelimiterCountOutOfRangeException", fpiCount.ToString(), RangeMax.ToString());
316  throw new System.IndexOutOfRangeException(lvsLM);
317  }
318  }
319  else
320  {
321  string lvsLM = LM.GetMessage("GetLeftFromDelimiterNumber", "OKWDelimiterIsEmptyException");
322  throw new System.ArgumentException(lvsLM);
323  }
324 
325  bOK = true;
326  }
327  finally
328  {
329  if (bOK)
330  {
331  Log.LogFunctionEndDebug(lvsReturn);
332  }
333  else
334  {
335  Log.LogFunctionEndDebug();
336  }
337  }
338 
339  return lvsReturn;
340  }
341 
361  public static bool ListStringCompare(List<string> ListString1, List<string> ListString2)
362  {
363  bool lvbReturn = true;
364  bool bOK = false;
365 
366  Logger.Instance.LogFunctionStartDebug("OKW_Helper.ListStringCompare", "ListString1", ListString1.ToString(), "ListString2", ListString2.ToString());
367 
368  try
369  {
370  // HAben beide Listen
371  if (ListString1.Count == ListString2.Count)
372  {
373  for (int i = 0; i < ListString1.Count; i++)
374  {
375  if (ListString1[i] != ListString2[i])
376  {
377  // ungleich daher abbrechen und false zurückliefern.
378  Logger.Instance.LogPrintDebug(ListString1[i] + "<>" + ListString2[i]);
379  lvbReturn = false;
380  break;
381  }
382  else
383  {
384  lvbReturn = true;
385  }
386  }
387  }
388  else
389  {
390  lvbReturn = false;
391  }
392 
393  bOK = true;
394  }
395  finally
396  {
397  if (bOK)
398  {
399  Logger.Instance.LogFunctionEndDebug(lvbReturn);
400  }
401  else
402  {
403  Logger.Instance.LogFunctionEndDebug();
404  }
405  }
406 
407  return lvbReturn;
408  }
409 
449  public static string ListStringConcat(List<string> fps_ListString2Concat, string fps_Separator)
450  {
451  string lvsReturn = string.Empty;
452  bool bOK = false;
453 
454  Log.LogFunctionStartDebug("OKW_Helper.ListStringConcat", "fps_ListString2Concat", fps_ListString2Concat.ToString(), "fps_Separator", fps_Separator);
455 
456  try
457  {
458  lvsReturn = string.Join(fps_Separator, fps_ListString2Concat.ToArray());
459  bOK = true;
460  }
461  finally
462  {
463  if (bOK)
464  {
465  Log.LogFunctionEndDebug(lvsReturn);
466  }
467  else
468  {
469  Log.LogFunctionEndDebug();
470  }
471  }
472 
473  return lvsReturn;
474  }
475 
491  public static bool MatchStr(string fpsPattern, string fpsStringToMatch)
492  {
493  // Variables:
494  bool lvbReturn = false;
495  bool bOK = false;
496 
497  // Action:
498  try
499  {
500  Logger.Instance.LogFunctionStartDebug("OKW_Helper.MatchStr", "String fpsPattern", fpsPattern, "String fpsStringToMatch", fpsStringToMatch);
501 
502  // Replace the * with an .* and the ? with a dot. Put ^ at the
503  // beginning and a $ at the end
504  string pattern = "^" + Regex.Escape(fpsPattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$";
505 
506  // Now, run the Regex as you already know
507  Regex regex;
508  regex = new Regex(pattern);
509 
510  lvbReturn = regex.IsMatch(fpsStringToMatch);
511  bOK = true;
512  }
513  finally
514  {
515  if (bOK)
516  {
517  Log.LogFunctionEndDebug(lvbReturn);
518  }
519  else
520  {
521  Log.LogFunctionEndDebug();
522  }
523  }
524 
525  return lvbReturn;
526  }
527 
543  public static bool MatchStrIgnoreCase(string fpsPattern, string fpsStringToMatch)
544  {
545  // Variables:
546  bool lvb_Return = false;
547  bool bOK = true;
548 
549  // Action:
550  try
551  {
552  Logger.Instance.LogFunctionStartDebug("OKW_Helper.MatchStrIgnoreCase", "String fpsPattern", fpsPattern, "String fpsStringToMatch", fpsStringToMatch);
553 
554  // Replace the * with an .* and the ? with a dot. Put ^ at the
555  // beginning and a $ at the end
556  string pattern = "^" + Regex.Escape(fpsPattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$";
557 
558  // Now, run the Regex as you already know
559  Regex regex;
560  regex = new Regex(pattern, RegexOptions.IgnoreCase);
561 
562  lvb_Return = regex.IsMatch(fpsStringToMatch);
563  bOK = true;
564  }
565  finally
566  {
567  if (bOK)
568  {
569  Logger.Instance.LogFunctionEndDebug(lvb_Return);
570  }
571  else
572  {
573  Logger.Instance.LogFunctionEndDebug();
574  }
575  }
576 
577  return lvb_Return;
578  }
579 
600  public static string RemoveBeginEndQuotations(string fps_StringinQuotations)
601  {
602  string lvs_Return = Regex.Replace(fps_StringinQuotations, "^\"|\"$", string.Empty);
603  return lvs_Return;
604  }
605 
623  public static List<string> StrArray2ListStr(string[] fpsStringArray)
624  {
625  Logger.Instance.LogFunctionStartDebug("OKW_Helper.StrArray2ListStr", "fpsStringArray", fpsStringArray.ToString());
626 
627  List<string> lvls_Splited = new List<string>();
628  lvls_Splited.Clear();
629 
630  try
631  {
632  lvls_Splited = new List<string>(fpsStringArray);
633  }
634  finally
635  {
636  Logger.Instance.LogFunctionEndDebug(lvls_Splited);
637  }
638 
639  return lvls_Splited;
640  }
641 
671 
672  public static bool String2Boolean(string fpsTrueOrFalse)
673  {
674  bool lvbReturn = false;
675  bool bOK = false;
676 
677  string lvsTrueOrFalse = fpsTrueOrFalse.ToLower();
678 
679  Logger.Instance.LogFunctionStartDebug("OKW_Helper.String2Boolean", "fpsTrueOrFalse", fpsTrueOrFalse);
680 
681  try
682  {
683  if ( lvsTrueOrFalse == "true")
684  {
685  lvbReturn = true;
686  bOK = true;
687  }
688  else if ( lvsTrueOrFalse == "false" )
689  {
690  lvbReturn = false;
691  bOK = true;
692  }
693  else{
694  // alles andere löst ein OKWNotAllowedValueException aus
695  bOK = false;
696 
697  string lvsLM = LM.GetMessage("String2Boolean", "OKWNotAllowedValueException", fpsTrueOrFalse);
698  throw new OKWNotAllowedValueException(lvsLM);
699  }
700  }
701  finally
702  {
703  if (bOK)
704  {
705  Log.LogFunctionEndDebug(lvbReturn);
706  }
707  else
708  {
709  Log.LogFunctionEndDebug();
710  }
711  }
712 
713  return lvbReturn;
714  }
715 
737  public static List<string> StrSplit(string fpsString2Split, string fpsSeparator)
738  {
739  bool bOK = false;
740 
741  string[] lvsA_Splited;
742  List<string> lvls_Splited = new List<string>();
743 
744  string[] lvsSeparators = new string[] { fpsSeparator };
745 
746  Log.LogFunctionStartDebug("OKW_Helper.StrSplit", "fpsString2Split", fpsString2Split, "fpsSeparator", fpsSeparator);
747 
748  try
749  {
750  lvsA_Splited = fpsString2Split.Split(lvsSeparators, StringSplitOptions.None);
751  lvls_Splited = StrArray2ListStr(lvsA_Splited);
752  bOK = true;
753  }
754  finally
755  {
756  if (bOK)
757  {
758  Log.LogFunctionEndDebug(lvls_Splited);
759  }
760  else
761  {
762  Log.LogFunctionEndDebug();
763  }
764  }
765 
766  return lvls_Splited;
767  }
768 
769  #endregion Methods
770  }
771 }
static Logger Log
Hält eine Referenz auf die Klasse Logger vor.
Definition: OKW_Helper.cs:72
string GetMessage(string MethodName, string TextKey)
Holt die Log-Meldung für MethodeNmae/Textkey ohne weitere Parameter.
Klasse enthält OKW-Hilfsfunktionen.
Definition: OKW_Helper.cs:63
static string GetLeftFromDelimiterNumber(string fpsSource, string fpsDelimiter, int fpiCount)
Der String wird am fpiCount-ten Trennzeichen abgetrennt und die linke Hälfte des String wird zurückge...
Definition: OKW_Helper.cs:182
static string RemoveBeginEndQuotations(string fps_StringinQuotations)
Entfernt Anführungstriche " am Anfang und am Ende. Anführungstriche, die sich im String-Inneren befin...
Definition: OKW_Helper.cs:600
static bool String2Boolean(string fpsTrueOrFalse)
Konvertiert string "true"/"false" nach bool.
Definition: OKW_Helper.cs:672
static bool ListStringCompare(List< string > ListString1, List< string > ListString2)
Vergleicht zwei ListStrings inhaltlich miteinander.
Definition: OKW_Helper.cs:361
static string GetRigthFromDelimiterNumber(string fpsSource, string fpsDelimiter, int fpiCount)
Schneidet fpsSource am fpiCount-ten fpsDelimiter ab und liefert den rechten Teil des string zurück...
Definition: OKW_Helper.cs:284
static List< string > StrArray2ListStr(string[] fpsStringArray)
Konvertiert einen String-Array nach List-String.
Definition: OKW_Helper.cs:623
LogMessenger liest Log-Meldungen sprachspezifisch für die im Konstruktor gegeben Klasse aus der Zugeh...
Definition: LogMessenger.cs:90
void LogPrintDebug(string fpsMessage)
Loggt eine Nachricht.
Definition: Logger.cs:332
static string ListStringConcat(List< string > fps_ListString2Concat, string fps_Separator)
Wandelt List in einen string um und trennt die Werte durch das vorgegebene Trennzeichen vonei...
Definition: OKW_Helper.cs:449
static LogMessenger LM
Hält eine Referenz auf die Klasse LogMessenger für die sprachabhängige Log-Ausgabe vor...
Definition: OKW_Helper.cs:79
static string Boolean2String(bool fpbTrueOrFalse)
Konvertiert bool true/false nach string "true"/"false".
Definition: OKW_Helper.cs:108
static List< string > StrSplit(string fpsString2Split, string fpsSeparator)
Splittet einen string am angegebenen Separator auf. Der Separator kann aus mehr als einem Zeichen bes...
Definition: OKW_Helper.cs:737
static bool MatchStr(string fpsPattern, string fpsStringToMatch)
Quelle: http://stackoverflow.com/questions/6907720/need-to-perform-wildcard-etc-search-on-a-string-us...
Definition: OKW_Helper.cs:491
static bool MatchStrIgnoreCase(string fpsPattern, string fpsStringToMatch)
Quelle: http://stackoverflow.com/questions/6907720/need-to-perform-wildcard-etc-search-on-a-string-us...
Definition: OKW_Helper.cs:543
Definition: Core.cs:40