yojimbos_law

kolmafia source code for file_to_map()

Aug 1st, 2018
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. public static Value file_to_map( Interpreter interpreter, final Value var1, final Value var2 )
  2. {
  3. return file_to_map( interpreter, var1, var2, DataTypes.TRUE_VALUE );
  4. }
  5.  
  6. public static Value file_to_map( Interpreter interpreter, final Value var1, final Value var2, final Value var3 )
  7. {
  8. String filename = var1.toString();
  9. CompositeValue result = (CompositeValue) var2;
  10. boolean compact = var3.intValue() == 1;
  11.  
  12. BufferedReader reader = DataFileCache.getReader( filename );
  13. if ( reader == null )
  14. {
  15. return DataTypes.FALSE_VALUE;
  16. }
  17.  
  18. String[] data = null;
  19. result.clear();
  20.  
  21. try
  22. {
  23. int line = 0;
  24. while ( ( data = FileUtilities.readData( reader ) ) != null )
  25. {
  26. line++;
  27. if ( data.length > 1 )
  28. {
  29. result.read( data, 0, compact, filename, line );
  30. }
  31. }
  32. }
  33. catch ( Exception e )
  34. {
  35. StringBuilder buffer = new StringBuilder( "Invalid line in data file" );
  36. if ( data != null )
  37. {
  38. buffer.append( ": \"" );
  39. for ( int i = 0; i < data.length; ++i )
  40. {
  41. if ( i > 0 )
  42. {
  43. buffer.append( '\t' );
  44. }
  45. buffer.append( data[ i ] );
  46. }
  47. buffer.append( "\"" );
  48. }
  49.  
  50. // Print the bad data that caused the error
  51. Exception ex = interpreter.runtimeException( buffer.toString() );
  52.  
  53. // If it's a ScriptException, we generated it ourself
  54. if ( e instanceof ScriptException )
  55. {
  56. // Print the bad data and the resulting error
  57. RequestLogger.printLine( ex.getMessage() );
  58. RequestLogger.printLine( e.getMessage() );
  59. }
  60. else
  61. {
  62. // Otherwise, print a stack trace
  63. StaticEntity.printStackTrace( e, ex.getMessage() );
  64. }
  65. return DataTypes.FALSE_VALUE;
  66. }
  67. finally
  68. {
  69. try
  70. {
  71. reader.close();
  72. }
  73. catch ( Exception e )
  74. {
  75. }
  76. }
  77.  
  78. return DataTypes.TRUE_VALUE;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment