stringio.monkey2 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. Namespace std.stringio
  2. Using libc
  3. #rem monkeydoc Loads a string from a file.
  4. An empty string will be returned if the file could not be opened.
  5. @param path The path of the file.
  6. @param fixeols If true, converts eols to UNIX "~n" eols after loading.
  7. @return A String containing the contents of the file.
  8. #end
  9. Function LoadString:String( path:String,fixeols:Bool=False )
  10. Local data:=DataBuffer.Load( path )
  11. If Not data Return ""
  12. Local str:=String.FromCString( data.Data,data.Length )
  13. data.Discard()
  14. If fixeols
  15. str=str.Replace( "~r~n","~n" )
  16. str=str.Replace( "~r","~n" )
  17. Endif
  18. Return str
  19. End
  20. #rem monkeydoc Saves a string to a file.
  21. @param str The string to save.
  22. @param path The path of the file.
  23. @param fixeols If true, converts eols to UNIX "~n" eols before saving.
  24. @return False if the file could not be opened.
  25. #end
  26. Function SaveString:Bool( str:String,path:String,fixeols:Bool=False )
  27. If fixeols
  28. str=str.Replace( "~r~n","~n" )
  29. str=str.Replace( "~r","~n" )
  30. Endif
  31. Local data:=New DataBuffer( str.CStringLength )
  32. str.ToCString( data.Data,data.Length )
  33. Local ok:=data.Save( path )
  34. data.Discard()
  35. Return ok
  36. End
  37. #rem monkeydoc Converts an unsigned long value to a string.
  38. @param value Value to convert.
  39. @param base Numeric base for conversion, eg: 2 for binary, 16 for hex etc.
  40. #end
  41. Function ULongToString:String( value:ULong,base:UInt )
  42. Local str:=""
  43. While value
  44. Local n:=value Mod base
  45. If n<10 str=String.FromChar( n+48 )+str Else str=String.FromChar( n+55 )+str
  46. value/=base
  47. Wend
  48. Return str ? str Else "0"
  49. End
  50. #rem monkeydoc Converts a string to an unsigned long value.
  51. @param str String to convert.
  52. @param base Numeric base for conversion, eg: 2 for binary, 16 for hex etc.
  53. #end
  54. Function StringToULong:ULong( str:String,base:UInt )
  55. Local value:ULong
  56. If base<=10
  57. For Local ch:=Eachin str
  58. If ch>=48 And ch<48+base value=value*base+(ch-48) Else Exit
  59. Next
  60. Return value
  61. Endif
  62. For Local ch:=Eachin str
  63. If ch>=48 And ch<58
  64. value=value*base+(ch-48)
  65. Else If ch>=65 And ch<65+(base-10)
  66. value=value*base+(ch-55)
  67. Else If ch>=97 And ch<97+(base-10)
  68. value=value*base+(ch-87)
  69. Else
  70. Exit
  71. Endif
  72. Next
  73. Return value
  74. End
  75. #rem monkeydoc Converts a ulong value to a binary string.
  76. #end
  77. Function Bin:String( value:ULong )
  78. Return ULongToString( value,2 )
  79. End
  80. #rem monkeydoc Converts a binary string to a ulong value.
  81. #end
  82. Function ParseBin:ULong( str:String )
  83. Return StringToULong( str,2 )
  84. End
  85. #rem monkeydoc Converts a ulong value to a hexadecimal string.
  86. #end
  87. Function Hex:String( value:ULong )
  88. Return ULongToString( value,16 )
  89. End
  90. #rem monkeydoc Converts a hexadecimal string to a ulong value.
  91. #end
  92. Function ParseHex:ULong( str:String )
  93. Return StringToULong( str,16 )
  94. End
  95. #rem monkeydoc Parse a boolean string.
  96. Returns true if `str` equals "True", ignoring case. Otherwise, returns false.
  97. #end
  98. Function ParseBool:Bool( str:String )
  99. Return str.ToLower()="true"
  100. End