as_scriptcode.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2015 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_scriptcode.cpp
  25. //
  26. // A container class for the script code to be compiled
  27. //
  28. #include "as_config.h"
  29. #include "as_scriptcode.h"
  30. BEGIN_AS_NAMESPACE
  31. asCScriptCode::asCScriptCode()
  32. {
  33. lineOffset = 0;
  34. code = 0;
  35. codeLength = 0;
  36. sharedCode = false;
  37. }
  38. asCScriptCode::~asCScriptCode()
  39. {
  40. if( !sharedCode && code )
  41. {
  42. asDELETEARRAY(code);
  43. }
  44. }
  45. int asCScriptCode::SetCode(const char *in_name, const char *in_code, bool in_makeCopy)
  46. {
  47. return SetCode(in_name, in_code, 0, in_makeCopy);
  48. }
  49. int asCScriptCode::SetCode(const char *in_name, const char *in_code, size_t in_length, bool in_makeCopy)
  50. {
  51. if( !in_code) return asINVALID_ARG;
  52. this->name = in_name ? in_name : "";
  53. if( !sharedCode && code )
  54. asDELETEARRAY(code);
  55. if( in_length == 0 )
  56. in_length = strlen(in_code);
  57. if( in_makeCopy )
  58. {
  59. codeLength = in_length;
  60. sharedCode = false;
  61. code = asNEWARRAY(char, in_length);
  62. if( code == 0 )
  63. return asOUT_OF_MEMORY;
  64. memcpy(code, in_code, in_length);
  65. }
  66. else
  67. {
  68. codeLength = in_length;
  69. code = const_cast<char*>(in_code);
  70. sharedCode = true;
  71. }
  72. // Find the positions of each line
  73. linePositions.PushLast(0);
  74. for( size_t n = 0; n < in_length; n++ )
  75. if( in_code[n] == '\n' ) linePositions.PushLast(n+1);
  76. linePositions.PushLast(in_length);
  77. return asSUCCESS;
  78. }
  79. void asCScriptCode::ConvertPosToRowCol(size_t pos, int *row, int *col)
  80. {
  81. if( linePositions.GetLength() == 0 )
  82. {
  83. if( row ) *row = lineOffset;
  84. if( col ) *col = 1;
  85. return;
  86. }
  87. // Do a binary search in the buffer
  88. int max = (int)linePositions.GetLength() - 1;
  89. int min = 0;
  90. int i = max/2;
  91. for(;;)
  92. {
  93. if( linePositions[i] < pos )
  94. {
  95. // Have we found the largest number < programPosition?
  96. if( min == i ) break;
  97. min = i;
  98. i = (max + min)/2;
  99. }
  100. else if( linePositions[i] > pos )
  101. {
  102. // Have we found the smallest number > programPoisition?
  103. if( max == i ) break;
  104. max = i;
  105. i = (max + min)/2;
  106. }
  107. else
  108. {
  109. // We found the exact position
  110. break;
  111. }
  112. }
  113. if( row ) *row = i + 1 + lineOffset;
  114. if( col ) *col = (int)(pos - linePositions[i]) + 1;
  115. }
  116. bool asCScriptCode::TokenEquals(size_t pos, size_t len, const char *str)
  117. {
  118. if( pos + len > codeLength ) return false;
  119. if( strncmp(code + pos, str, len) == 0 && strlen(str) == len )
  120. return true;
  121. return false;
  122. }
  123. END_AS_NAMESPACE