My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

binarystring.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. function BinaryString(source) {
  2. this._source = null;
  3. this._bytes = [];
  4. this._pos = 0;
  5. this._length = 0;
  6. this._init = function(source) {
  7. this._source = source;
  8. this._bytes = this._stringToBytes(this._source);
  9. this._length = this._bytes.length;
  10. }
  11. this.current = function() {return this._pos;}
  12. this.rewind = function() {return this.jump(0);}
  13. this.end = function() {return this.jump(this.length() - 1);}
  14. this.next = function() {return this.jump(this.current() + 1);}
  15. this.prev = function() {return this.jump(this.current() - 1);}
  16. this.jump = function(pos) {
  17. if (pos < 0 || pos >= this.length()) return false;
  18. this._pos = pos;
  19. return true;
  20. }
  21. this.readByte = function(pos) {
  22. pos = (typeof pos == 'number') ? pos : this.current();
  23. return this.readBytes(1, pos)[0];
  24. }
  25. this.readBytes = function(length, pos) {
  26. length = length || 1;
  27. pos = (typeof pos == 'number') ? pos : this.current();
  28. if (pos > this.length() ||
  29. pos < 0 ||
  30. length <= 0 ||
  31. pos + length > this.length() ||
  32. pos + length < 0
  33. ) {
  34. return false;
  35. }
  36. var bytes = [];
  37. for (var i = pos; i < pos + length; i++) {
  38. bytes.push(this._bytes[i]);
  39. }
  40. return bytes;
  41. }
  42. this.length = function() {return this._length;}
  43. this.toString = function() {
  44. var string = '',
  45. length = this.length();
  46. for (var i = 0; i < length; i++) {
  47. string += String.fromCharCode(this.readByte(i));
  48. }
  49. return string;
  50. }
  51. this.toUtf8 = function() {
  52. var inc = 0,
  53. string = '',
  54. length = this.length();
  55. // determine if first 3 characters are the BOM
  56. // then skip them in output if so
  57. if (length >= 3 &&
  58. this.readByte(0) === 0xEF &&
  59. this.readByte(1) === 0xBB &&
  60. this.readByte(2) === 0xBF
  61. ) {
  62. inc = 3;
  63. }
  64. for (; inc < length; inc++) {
  65. var byte1 = this.readByte(inc),
  66. byte2 = 0,
  67. byte3 = 0,
  68. byte4 = 0,
  69. code1 = 0,
  70. code2 = 0,
  71. point = 0;
  72. switch (true) {
  73. // single byte character; same as ascii
  74. case (byte1 < 0x80):
  75. code1 = byte1;
  76. break;
  77. // 2 byte character
  78. case (byte1 >= 0xC2 && byte1 < 0xE0):
  79. byte2 = this.readByte(++inc);
  80. code1 = ((byte1 & 0x1F) << 6) +
  81. (byte2 & 0x3F);
  82. break;
  83. // 3 byte character
  84. case (byte1 >= 0xE0 && byte1 < 0xF0):
  85. byte2 = this.readByte(++inc);
  86. byte3 = this.readByte(++inc);
  87. code1 = ((byte1 & 0xFF) << 12) +
  88. ((byte2 & 0x3F) << 6) +
  89. (byte3 & 0x3F);
  90. break;
  91. // 4 byte character
  92. case (byte1 >= 0xF0 && byte1 < 0xF5):
  93. byte2 = this.readByte(++inc);
  94. byte3 = this.readByte(++inc);
  95. byte4 = this.readByte(++inc);
  96. point = ((byte1 & 0x07) << 18) +
  97. ((byte2 & 0x3F) << 12) +
  98. ((byte3 & 0x3F) << 6) +
  99. (byte4 & 0x3F)
  100. point -= 0x10000;
  101. code1 = (point >> 10) + 0xD800;
  102. code2 = (point & 0x3FF) + 0xDC00;
  103. break;
  104. default:
  105. throw 'Invalid byte ' + this._byteToString(byte1) + ' whilst converting to UTF-8';
  106. break;
  107. }
  108. string += (code2) ? String.fromCharCode(code1, code2)
  109. : String.fromCharCode(code1);
  110. }
  111. return string;
  112. }
  113. this.toArray = function() {return this.readBytes(this.length() - 1, 0);}
  114. this._stringToBytes = function(str) {
  115. var bytes = [],
  116. chr = 0;
  117. for (var i = 0; i < str.length; i++) {
  118. chr = str.charCodeAt(i);
  119. bytes.push(chr & 0xFF);
  120. }
  121. return bytes;
  122. }
  123. this._byteToString = function(byte) {
  124. var asString = byte.toString(16).toUpperCase();
  125. while (asString.length < 2) {
  126. asString = '0' + asString;
  127. }
  128. return '0x' + asString;
  129. }
  130. this._init(source);
  131. }