stringio.monkey2 2.8 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 @deprecated Use [[ULongToString]] instead.
  38. #end
  39. Function Hex:String( value:ULong )
  40. Local str:=""
  41. While value
  42. Local nyb:=value & $f
  43. If nyb<10 str=String.FromChar( nyb+48 )+str Else str=String.FromChar( nyb+55 )+str
  44. value=value Shr 4
  45. Wend
  46. Return str ? str Else "0"
  47. End
  48. #rem monkeydoc @deprecated Use [[StringToULong]] instead.
  49. #end
  50. Function FromHex:ULong( hex:String )
  51. Local value:ULong
  52. For Local i:=0 Until hex.Length
  53. Local ch:=hex[i]
  54. If ch>=48 And ch<58
  55. value=value Shl 4 | (ch-48)
  56. Else If ch>=65 And ch<71
  57. value=value Shl 4 | (ch-55)
  58. Else If ch>=97 And ch<103
  59. value=value Shl 4 | (ch-87)
  60. Else
  61. Exit
  62. Endif
  63. Next
  64. Return value
  65. End
  66. #rem monkeydoc Converts an unsigned long value to a string.
  67. @param value Value to convert.
  68. @param base Numeric base for conversion, eg: 2 for binary, 16 for hex etc.
  69. #end
  70. Function ULongToString:String( value:ULong,base:UInt )
  71. Local str:=""
  72. While value
  73. Local n:=value Mod base
  74. If n<10 str=String.FromChar( n+48 )+str Else str=String.FromChar( n+55 )+str
  75. value/=base
  76. Wend
  77. Return str
  78. End
  79. #rem monkeydoc Converts a string to an unsigned long value.
  80. @param str String to convert.
  81. @param base Numeric base for conversion, eg: 2 for binary, 16 for hex etc.
  82. #end
  83. Function StringToULong:ULong( str:String,base:UInt )
  84. Local value:ULong
  85. If base<=10
  86. For Local ch:=Eachin str
  87. If ch>=48 And ch<48+base value=value*base+(ch-48) Else Exit
  88. Next
  89. Return value
  90. Endif
  91. For Local ch:=Eachin str
  92. If ch>=48 And ch<58
  93. value=value*base+(ch-48)
  94. Else If ch>=65 And ch<65+(base-10)
  95. value=value*base+(ch-55)
  96. Else If ch>=97 And ch<97+(base-10)
  97. value=value*base+(ch-87)
  98. Else
  99. Exit
  100. Endif
  101. Next
  102. Return value
  103. End