Advertisement
Zeriab

StringBuffer

Apr 26th, 2015
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.89 KB | None | 0 0
  1. #------------------------------------------------------------------------------
  2. # * License :
  3. #
  4. # Copyright (C) 2008-2015   Zeriab
  5. #
  6. # This software is provided 'as-is', without any express or implied warranty.
  7. # In no event will the authors be held liable for any damages arising from
  8. # the use of this software.
  9. # Permission is granted to anyone to use this software for any purpose,
  10. # including commercial applications, and to alter it and redistribute it
  11. # freely, subject to the following restrictions:
  12. #
  13. # 1. The origin of this software must not be misrepresented; you must not
  14. #    claim that you wrote the original software. If you use this software
  15. #    in a product, an acknowledgment in the product documentation would
  16. #    be appreciated but is not required.
  17. # 2. Altered source versions must be plainly marked as such, and must not
  18. #    be misrepresented as being the original software.
  19. # 3. This notice may not be removed or altered from any source distribution.
  20. #
  21. class StringBuffer < IO
  22.    MODE_STRING = 1
  23.    MODE_FILE_MARSHAL = 2
  24.    MODE_FILE_BINMODE = 3
  25.    ##
  26.    # Initialization of the String Buffer
  27.    # The mode can be MODE_STRING, MODE_FILE_MARSHAL or MODE_FILE_BINMODE
  28.    #
  29.    def initialize(data = nil, mode = MODE_FILE_MARSHAL, compressed = true)
  30.      if data.nil?
  31.        @data = ''
  32.      else
  33.        # Check which type the data is
  34.        case mode
  35.        when MODE_STRING
  36.          @data = data
  37.        when MODE_FILE_MARSHAL
  38.          begin
  39.            @data = load_data(data)
  40.          rescue
  41.            @data = ''
  42.          end
  43.        when MODE_FILE_BINMODE
  44.          if FileTest.exists?(data)
  45.            f = File.open(data, 'rb')
  46.            f.binmode
  47.            @data = f.read
  48.            f.close
  49.          end
  50.        end
  51.        # Decompress the data if it is compressed
  52.        if compressed
  53.          @data = Zlib::Inflate.inflate(@data)
  54.        end
  55.      end
  56.    end
  57.  
  58.    ##
  59.    # Save to file
  60.    # The mode can be MODE_FILE_MARSHAL or MODE_FILE_BINMODE
  61.    #
  62.    def save(filename, mode = MODE_FILE_MARSHAL, compress = true)
  63.      # Check whether the data should be compressed
  64.      if compress
  65.        data = Zlib::Deflate.deflate(@data)
  66.      else
  67.        data = @data
  68.      end
  69.      # Check whether to save in binary or marshal mode
  70.      case mode
  71.      when MODE_FILE_MARSHAL
  72.        save_data(data, filename)
  73.      when MODE_FILE_BINMODE
  74.        f = File.open(filename, 'w+')
  75.        f.binmode
  76.        f.print data
  77.        f.close
  78.      end
  79.    end
  80.  
  81.    ##
  82.    # Get data
  83.    #
  84.    def data
  85.      return @data
  86.    end
  87.  
  88.    ##
  89.    # Binary mode
  90.    #
  91.    def binmode
  92.      # Do nothing
  93.    end
  94.  
  95.    ##
  96.    # Write data
  97.    #
  98.    def write(data)
  99.      @data += data
  100.    end
  101.  
  102.    ##
  103.    # Get char
  104.    #
  105.    def getc
  106.      return @data.slice!(0)
  107.    end
  108.  
  109.    ##
  110.    # Read an amount of bytes
  111.    #
  112.    def read(amount)
  113.      return @data.slice!(0...amount)
  114.    end
  115. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement