SirCumferance

CSV Grid

May 5th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///csv_to_grid( file, cell bounds, text bounds )
  2.  
  3. switch( argument_count ) {
  4.    
  5.     case 1:
  6.         var file = argument[0];
  7.         var cellDelimiter = "";
  8.         var textDelimiter = "";
  9.     break;
  10.    
  11.     case 2:
  12.         var file = argument[0];
  13.         var cellDelimiter = argument[1];
  14.         var textDelimiter = "";
  15.     break;
  16.    
  17.     case 3:
  18.         var file = argument[0];
  19.         var cellDelimiter = argument[1];
  20.         var textDelimiter = argument[2];
  21.     break;
  22.    
  23.     default:
  24.         return noone;
  25.     break;
  26.    
  27. }
  28.  
  29. if ( cellDelimiter == "" ) cellDelimiter = ",";
  30. if ( textDelimiter == "" ) textDelimiter = chr(34);
  31.  
  32.  
  33. var cellDelimiterOrd = ord( cellDelimiter );
  34. var textDelimiterOrd = ord( textDelimiter );
  35.  
  36. //
  37. var buffer = buffer_create( 1, buffer_grow, 1 );
  38. buffer_load_ext( buffer, file, 0 );
  39. buffer_seek( buffer, buffer_seek_start, 0 );
  40.  
  41. //width and height of the spreadsheet
  42. var sheetWidth = 0;
  43. var sheetHeight = 1;
  44.  
  45. var prevVal = 0;
  46. var nextVal = 0;
  47. var val = 0;
  48. var str = "";
  49. var inText = false;
  50. var grid = noone;
  51.  
  52. var size = buffer_get_size( buffer );
  53. for( var i = 0; i < size; i++ ) {
  54.    
  55.     prevVal = val;
  56.     var val = buffer_read( buffer, buffer_u8 );
  57.    
  58.     if ( val == 13 ) continue;
  59.    
  60.     if ( val == textDelimiterOrd ) {
  61.        
  62.         var nextVal = buffer_peek( buffer, buffer_tell( buffer ), buffer_u8 );
  63.        
  64.         if ( inText ) {
  65.             if ( nextVal == textDelimiterOrd ) continue;
  66.             if ( prevVal == textDelimiterOrd ) {
  67.                 str += textDelimiter;
  68.                 continue;
  69.             }
  70.         }
  71.        
  72.         inText = !inText;
  73.         continue;
  74.        
  75.     }
  76.    
  77.     if ( inText ) and ( ( prevVal == 13 ) and ( val == 10 ) ) {  
  78.         str += "#";
  79.         continue;
  80.     }
  81.    
  82.     if ( ( val == cellDelimiterOrd ) or ( ( prevVal == 13 ) and ( val == 10 ) ) ) and ( !inText ) {
  83.        
  84.         sheetWidth++;
  85.         if ( grid == noone ) {
  86.             grid = ds_grid_create( max( 1, sheetWidth ), max( 1, sheetHeight ) );
  87.             ds_grid_clear( grid, "" );
  88.         } else ds_grid_resize( grid, max( sheetWidth, ds_grid_width( grid ) ), sheetHeight );
  89.        
  90.         ds_grid_set( grid, sheetWidth - 1, sheetHeight - 1, str );
  91.         str = "";
  92.         inText = false;
  93.        
  94.         if ( val == 10 ) {
  95.             sheetWidth = 0;
  96.             sheetHeight++;
  97.         }
  98.        
  99.         continue;
  100.     }
  101.    
  102.     str += chr( val );
  103.    
  104. }
  105.  
  106. buffer_delete( buffer );
  107.  
  108. sheetWidth = ds_grid_width( grid );
  109. sheetHeight = ds_grid_height( grid );
  110. for( var yy = 0; yy < sheetHeight; yy++ ) {
  111.     for( var xx = 0; xx < sheetWidth; xx++ ) {
  112.         var val = ds_grid_get( grid, xx, yy );
  113.         if ( !is_string( val ) ) ds_grid_set( grid, xx, yy, "" );
  114.     }
  115. }
  116.  
  117. //return the grid
  118. return grid;
Add Comment
Please, Sign In to add comment