Advertisement
Guest User

gamemaker csv to grid

a guest
Feb 8th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @desc csv_to_grid
  2. /// @param file
  3. /// @param [force_strings]
  4. /// @param [cell_delimiter]
  5. /// @param [string_delimiter]
  6. /// @param [mac_newline]
  7. //  
  8. //  CAUTION: Please ensure your files are in UTF-8 encoding.
  9. //  
  10. //  You may pass <undefined> to use the default value for optional arguments.
  11. //  arg0   string   Filename for the source UTF-8 CSV file
  12. //  arg1   bool     Whether to force all cells to be a string. Defaults to false
  13. //  arg2   string   The delimiter used to separate cells. Defaults to a comma
  14. //  arg3   string   The delimiter used to define strings in the CSV file. Defaults to a double-quote
  15. //  arg4   bool     Newline compatibility mode for Mac (0A). Defaults to Windows standard newline (0D,0A)
  16. //  
  17. //  (c) Juju Adams 26th May 2017
  18. //  @jujuadams
  19.  
  20. //boens example:
  21. //global.script_grid=scrCsv_to_grid_new("FIGHTKNIGHT_SCRIPT.csv",true,"|","~")
  22.  
  23.  
  24.  
  25. //Handle arguments
  26. if ( argument_count < 1 ) or ( argument_count > 5 ) {
  27.     show_error( "Incorrect number of arguments (" + string( argument_count ) + ")", false );
  28.     return undefined;
  29. }
  30.  
  31. var _filename         = argument[0];
  32. var _force_strings    = false;
  33. var _cell_delimiter   = chr(44); //comma
  34. var _string_delimiter = chr(34); //double-quote
  35. var _newline_alt      = false;
  36.  
  37. if ( argument_count >= 2 ) and ( !is_undefined( argument[1] ) ) _force_strings    = argument[1];
  38. if ( argument_count >= 3 ) and ( !is_undefined( argument[2] ) ) _cell_delimiter   = argument[2];
  39. if ( argument_count >= 4 ) and ( !is_undefined( argument[3] ) ) _string_delimiter = argument[3];
  40. if ( argument_count >= 5 ) and ( !is_undefined( argument[4] ) ) _newline_alt      = argument[4];
  41.  
  42. //Check for silliness...
  43. if ( string_length( _cell_delimiter ) != 1 ) or ( string_length( _string_delimiter ) != 1 ) {
  44.     show_error( "Delimiters must be one character", false );
  45.     return undefined;
  46. }
  47.  
  48. //More variables...
  49. var _cell_delimiter_ord  = ord( _cell_delimiter  );
  50. var _string_delimiter_ord = ord( _string_delimiter );
  51.  
  52. var _sheet_width  = 0;
  53. var _sheet_height = 1;
  54. var _max_width    = 0;
  55.  
  56. var _prev_val   = 0;
  57. var _val        = 0;
  58. var _str        = "";
  59. var _in_string  = false;
  60. var _is_decimal = !_force_strings;
  61. var _grid       = ds_grid_create( 1, 1 ); _grid[# 0, 0 ] = "";
  62.  
  63. //Load CSV file as a buffer
  64. var _buffer = buffer_load( _filename );
  65. var _size = buffer_get_size( _buffer );
  66. buffer_seek( _buffer, buffer_seek_start, 0 );
  67.  
  68. //Handle byte order marks from some UTF-8 encoders (EF BB BF at the start of the file)
  69. var _bom_a = buffer_read( _buffer, buffer_u8 );
  70. var _bom_b = buffer_read( _buffer, buffer_u8 );
  71. var _bom_c = buffer_read( _buffer, buffer_u8 );
  72. if !( ( _bom_a == 239 ) and ( _bom_b == 187 ) and ( _bom_c == 191 ) ) {
  73.     show_debug_message( "CAUTION: csv_to_grid: " + _filename + ": CSV file might not be UTF-8 encoded (no BOM)" );
  74.     buffer_seek( _buffer, buffer_seek_start, 0 );
  75. } else {
  76.     _size -= 3;
  77. }
  78.  
  79. //Iterate over the buffer
  80. for( var _i = 0; _i < _size; _i++ ) {
  81.  
  82.     _prev_val = _val;
  83.     var _val = buffer_read( _buffer, buffer_u8 );
  84.  
  85.     //Handle UTF-8 encoding
  86.     if ( ( _val & 224 ) == 192 ) { //two-byte
  87.  
  88.         _val  = (                              _val & 31 ) <<  6;
  89.         _val += ( buffer_read( _buffer, buffer_u8 ) & 63 );
  90.         _i++;
  91.  
  92.     } else if ( ( _val & 240 ) == 224 ) { //three-byte
  93.  
  94.         _val  = (                              _val & 15 ) << 12;
  95.         _val += ( buffer_read( _buffer, buffer_u8 ) & 63 ) <<  6;
  96.         _val +=   buffer_read( _buffer, buffer_u8 ) & 63;
  97.         _i += 2;
  98.  
  99.     } else if ( ( _val & 248 ) == 240 ) { //four-byte
  100.  
  101.         _val  = (                              _val &  7 ) << 18;
  102.         _val += ( buffer_read( _buffer, buffer_u8 ) & 63 ) << 12;
  103.         _val += ( buffer_read( _buffer, buffer_u8 ) & 63 ) <<  6;
  104.         _val +=   buffer_read( _buffer, buffer_u8 ) & 63;
  105.         _i += 3;
  106.  
  107.     }
  108.  
  109.     //If we've found a string delimiter
  110.     if ( _val == _string_delimiter_ord ) {
  111.  
  112.         //This definitely isn't a decimal number!
  113.         _is_decimal = false;
  114.  
  115.         //If we're in a string...
  116.         if ( _in_string ) {
  117.  
  118.             //If the next character is a string delimiter itself, skip this character
  119.             if ( buffer_peek( _buffer, buffer_tell( _buffer ), buffer_u8 ) == _string_delimiter_ord ) continue;
  120.  
  121.             //If the previous character is a string delimiter itself, add the string delimiter to the working string
  122.             if ( _prev_val == _string_delimiter_ord ) {
  123.                 _str += _string_delimiter;
  124.                 continue;
  125.             }
  126.  
  127.         }
  128.  
  129.         //Toggle "we're in a string" behaviour
  130.         _in_string = !_in_string;
  131.         continue;
  132.  
  133.     }
  134.    
  135.     if ( _newline_alt ) {
  136.         var _newline = ( _val == 10 );
  137.     } else {
  138.         var _newline = ( _prev_val == 13 ) and ( _val == 10 );
  139.        
  140.         //If we've found a newline and we're in a string, skip over the chr(10) character
  141.         if ( _in_string ) and ( _newline ) continue;
  142.     }
  143.  
  144.     //If we've found a new cell
  145.     if ( ( _val == _cell_delimiter_ord ) or ( _newline ) ) and ( !_in_string ) {
  146.  
  147.         _sheet_width++;
  148.  
  149.         //If this cell is now longer than the maximum width of the grid, expand the grid
  150.         if ( _sheet_width > _max_width ) {
  151.  
  152.             _max_width = _sheet_width;
  153.             ds_grid_resize( _grid, _max_width, _sheet_height );
  154.  
  155.             //Clear cells vertically above to overwrite the default 0-value
  156.             if ( _sheet_height >= 2 ) ds_grid_set_region( _grid, _max_width-1, 0, _max_width-1, _sheet_height-2, "" );
  157.  
  158.         }
  159.  
  160.         //Write the working string to a grid cell
  161.         if ( _is_decimal ) _str = real( _str );
  162.         _grid[# _sheet_width-1, _sheet_height-1 ] = _str;
  163.  
  164.         _str = "";
  165.         _in_string = false;
  166.         _is_decimal = !_force_strings;
  167.  
  168.         //A newline outside of a string triggers a new line... unsurprisingly
  169.         if ( _newline ) {
  170.  
  171.             //Clear cells horizontally to overwrite the default 0-value
  172.             if ( _sheet_width < _max_width ) ds_grid_set_region( _grid, _sheet_width, _sheet_height-1, _max_width-1, _sheet_height-1, "" );
  173.  
  174.             _sheet_width = 0;
  175.             _sheet_height++;
  176.             ds_grid_resize( _grid, _max_width, _sheet_height );
  177.         }
  178.  
  179.         continue;
  180.  
  181.     }
  182.  
  183.     //Check if we've read a "\n" dual-character
  184.     if ( _prev_val == 92 ) and ( _val == 110 ) {
  185.         _str = string_delete( _str, string_length( _str ), 1 ) + chr(13);
  186.         continue;
  187.     }
  188.    
  189.     //No newlines should appear outside of a string delimited cell
  190.     if ( ( _val == 10 ) or ( _val == 13 ) ) and ( !_in_string ) continue;
  191.    
  192.     //Check if this character is outside valid decimal character range
  193.     if ( _val != 45 ) and ( _val != 46 ) and ( ( _val < 48 ) or ( _val > 57 ) ) _is_decimal = false;
  194.  
  195.     //Finally add this character to the working string!
  196.     _str += chr( _val );
  197.  
  198. }
  199.  
  200. //Catch hanging work string on end-of-file
  201. if ( _str != "" ) {
  202.    
  203.     _sheet_width++;
  204.    
  205.     if ( _sheet_width > _max_width ) {
  206.         _max_width = _sheet_width;
  207.         ds_grid_resize( _grid, _max_width, _sheet_height );
  208.         if ( _sheet_height >= 2 ) ds_grid_set_region( _grid, _max_width-1, 0, _max_width-1, _sheet_height-2, "" );
  209.     }
  210.    
  211.     if ( _is_decimal ) _str = real( _str );
  212.     _grid[# _sheet_width-1, _sheet_height-1 ] = _str;
  213.    
  214. }
  215.  
  216. //If the last character was a newline then we'll have an erroneous extra row at the bottom
  217. if ( _newline ) ds_grid_resize( _grid, _max_width, _sheet_height-1 );
  218.  
  219. buffer_delete( _buffer );
  220. return _grid;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement