KeyValuePairUtils.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Copyright (C) 2012-2015 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using Utilities;
  11. namespace ISCSI
  12. {
  13. public class KeyValuePairUtils
  14. {
  15. public static KeyValuePairList<string, string> GetKeyValuePairList(string nullDelimitedString)
  16. {
  17. KeyValuePairList<string, string> result = new KeyValuePairList<string, string>();
  18. string[] entries = nullDelimitedString.Split('\0');
  19. foreach (string entry in entries)
  20. {
  21. string[] pair = entry.Split('=');
  22. if (pair.Length >= 2)
  23. {
  24. string key = pair[0];
  25. string value = pair[1];
  26. result.Add(key, value);
  27. }
  28. }
  29. return result;
  30. }
  31. public static string GetNullDelimitedKeyValuePair(KeyValuePairList<string, string> list)
  32. {
  33. StringBuilder builder = new StringBuilder();
  34. foreach (KeyValuePair<string, string> pair in list)
  35. {
  36. builder.AppendFormat("{0}={1}\0", pair.Key, pair.Value);
  37. }
  38. return builder.ToString();
  39. }
  40. public static string ToString(KeyValuePairList<string, string> list)
  41. {
  42. StringBuilder builder = new StringBuilder();
  43. for (int index = 0; index < list.Count; index++)
  44. {
  45. if (index > 0)
  46. {
  47. builder.Append(", ");
  48. }
  49. builder.AppendFormat("{0}={1}", list[index].Key, list[index].Value);
  50. }
  51. return builder.ToString();
  52. }
  53. }
  54. }