* On failure throws std::runtime_error
*/
void write_file(const std::uint8_t* data, std::size_t len, std::string path, std::size_t offset,
- bool rewrite) {
+ bool truncate) {
ScopeLogger();
- FILE* file = fopen(path.c_str(), rewrite ? "w" : "r+");
+ FILE* file = fopen(path.c_str(), truncate ? "w" : "r+");
if (!file) {
throw std::runtime_error("cannot open file to write");
CHECK_EXIST(args, "location", out)
CHECK_EXIST(args, "data", out)
CHECK_EXIST(args, "offset", out)
- CHECK_EXIST(args, "rewrite", out)
+ CHECK_EXIST(args, "truncate", out)
const std::string& location = args.get("location").get<std::string>();
const std::string& str = args.get("data").get<std::string>();
size_t offset = static_cast<size_t>(args.get("offset").get<double>());
- bool rewrite = static_cast<bool>(args.get("rewrite").get<bool>());
+ bool truncate = static_cast<bool>(args.get("truncate").get<bool>());
const std::string& encoding =
args.contains("encoding") ? args.get("encoding").get<std::string>() : "utf-8";
if (encoding == "iso-8859-1") {
std::vector<std::uint8_t> data;
latin1::from_utf8(str, data);
- write_file(data.data(), data.size(), location, offset, rewrite);
+ write_file(data.data(), data.size(), location, offset, truncate);
} else { // default: UTF-8
const std::uint8_t* buf = (const std::uint8_t*)str.c_str();
std::size_t len = str.length();
- write_file(buf, len, location, offset, rewrite);
+ write_file(buf, len, location, offset, truncate);
}
} catch (std::runtime_error& e) {
LoggerE("Cannot write to file %s, cause: %s", location.c_str(), e.what());
CHECK_EXIST(args, "location", out)
CHECK_EXIST(args, "data", out)
CHECK_EXIST(args, "offset", out)
- CHECK_EXIST(args, "rewrite", out)
+ CHECK_EXIST(args, "truncate", out)
const std::string& location = args.get("location").get<std::string>();
const std::string& str = args.get("data").get<std::string>();
size_t offset = static_cast<size_t>(args.get("offset").get<double>());
- bool rewrite = static_cast<bool>(args.get("rewrite").get<bool>());
+ bool truncate = static_cast<bool>(args.get("truncate").get<bool>());
try {
std::vector<std::uint8_t> data;
decode_binary_from_string(str, data);
- write_file(data.data(), data.size(), location, offset, rewrite);
+ write_file(data.data(), data.size(), location, offset, truncate);
} catch (std::runtime_error& e) {
LoggerE("Cannot write to %s, cause: %s", location.c_str(), e.what());
PrepareError(FilesystemError::Other, out);
CHECK_EXIST(args, "location", out)
CHECK_EXIST(args, "data", out)
CHECK_EXIST(args, "offset", out)
- CHECK_EXIST(args, "rewrite", out)
+ CHECK_EXIST(args, "truncate", out)
const std::string& location = args.get("location").get<std::string>();
const std::string& str = args.get("data").get<std::string>();
size_t offset = static_cast<size_t>(args.get("offset").get<double>());
- bool rewrite = static_cast<bool>(args.get("rewrite").get<bool>());
+ bool truncate = static_cast<bool>(args.get("truncate").get<bool>());
std::vector<std::uint8_t> data;
try {
}
try {
- write_file(data.data(), data.size(), location, offset, rewrite);
+ write_file(data.data(), data.size(), location, offset, truncate);
ReportSuccess(picojson::value{(double)data.size()}, out);
} catch (std::runtime_error& e) {
LoggerE("Cannot write to %s, cause: %s", location.c_str(), e.what());
get: function() {
return _totalBytes < _position;
},
- set: function(v) {
- },
+ set: function(v) {},
enumerable: true
},
position: {
get: function() {
return this.eof ? -1 : Math.max(0, _totalBytes - _position);
},
- set: function(v) {
- },
+ set: function(v) {},
enumerable: true
},
- _mode: {
- value: mode,
- writable: false,
- enumerable: false
- },
- _encoding: {
- value: encoding,
- writable: false,
- enumerable: false
- },
- _file: {
- value: data,
- writable: false,
- enumerable: false
- },
- _closed: {
- value: false,
- writable: true,
- enumerable: false
- },
- _rewrite: {
- value: mode === 'w' ? true : false,
+ _mode: {value: mode, writable: false, enumerable: false},
+ _encoding: {value: encoding, writable: false, enumerable: false},
+ _file: {value: data, writable: false, enumerable: false},
+ _closed: {value: false, writable: true, enumerable: false},
+ _truncate: {
+ value: mode === 'w', // 'w' truncates file to zero length
writable: true,
enumerable: false
}
function closeFileStream() {
this._closed = true;
-};
+}
FileStream.prototype.close = function() {
closeFileStream.apply(this, arguments);
}
}
-/* returns array of numbers */
-function string_to_array( str ) {
- var output = [];
- var len = str.length;
- for( var i = 0; i < len; i++ ) {
- output.push( str.charCodeAt(i) );
- }
- return output;
-}
-
-/* receives array of numbers, returns string */
-function array_to_string( data ) {
- var output = "";
- var len = data.length;
- for( var i = 0; i < len; i++ ) {
- output += String.fromCharCode(data[i] & 0xFF); // conversion to octet
- }
- return output;
-}
-
function read() {
- var args = validator_.validateArgs(arguments, [
- {
- name: 'charCount',
- type: types_.LONG
- }
- ]);
+ var args = validator_.validateArgs(arguments, [{name: 'charCount', type: types_.LONG}]);
_checkClosed(this);
_checkReadAccess(this._mode);
if (!arguments.length) {
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
- 'Argument "charCount" missing');
+ throw new WebAPIException(
+ WebAPIException.INVALID_VALUES_ERR, 'Argument "charCount" missing');
}
if (!type_.isNumber(args.charCount)) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Argument "charCount" must be a number');
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR, 'Argument "charCount" must be a number');
}
if (args.charCount <= 0) {
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
+ throw new WebAPIException(
+ WebAPIException.INVALID_VALUES_ERR,
'Argument "charCount" must be greater than 0');
}
- if(this.eof) {
- throw new WebAPIException(WebAPIException.IO_ERR, 'Stream is marked as EOF.');
+ if (this.eof) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'Stream is marked as EOF.');
}
var _count = this.bytesAvailable;
this.position += outData.length;
can_change_size = false;
} else {
- this.position += 1; // Set EOF
+ this.position += 1; // Set EOF
}
return outData;
-};
+}
FileStream.prototype.read = function() {
return read.apply(this, arguments);
};
function readBytes() {
- var args = validator_.validateArgs(arguments, [
- {
- name: 'byteCount',
- type: types_.LONG
- }
- ]);
+ var args = validator_.validateArgs(arguments, [{name: 'byteCount', type: types_.LONG}]);
_checkClosed(this);
_checkReadAccess(this._mode);
if (args.byteCount <= 0) {
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
+ throw new WebAPIException(
+ WebAPIException.INVALID_VALUES_ERR,
'Argument "byteCount" must be greater than 0');
}
throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'Could not read');
}
- var decoded = string_to_array( native_.getResultObject(result) );
+ var decoded = StringToArray(native_.getResultObject(result), Array);
if (decoded.length) {
can_change_size = true;
this.position += decoded.length;
can_change_size = false;
} else {
- this.position += 1; // Set EOF
+ this.position += 1; // Set EOF
}
return decoded;
-};
+}
FileStream.prototype.readBytes = function() {
return readBytes.apply(this, arguments);
FileStream.prototype.readBase64 = function() {
return base64_encode(readBytes.apply(this, arguments));
-}
+};
-function check_characters_outside_latin1( str ) {
+function check_characters_outside_latin1(str) {
var len = str.length;
- for( var i = 0; i < len; ++i ) {
- if( str.charCodeAt(i) > 255 ) {
- throw new WebAPIException(WebAPIException.IO_ERR, 'Invalid character at '+i+': '+str.charAt(i)+' (not ISO-8859-1)');
+ for (var i = 0; i < len; ++i) {
+ if (str.charCodeAt(i) > 255) {
+ throw new WebAPIException(
+ WebAPIException.IO_ERR,
+ 'Invalid character at ' + i + ': ' + str.charAt(i) + ' (not ISO-8859-1)');
}
}
}
function write() {
- var args = validator_.validateArgs(arguments, [
- {
- name: 'stringData',
- type: types_.STRING
- }
- ]);
+ var args =
+ validator_.validateArgs(arguments, [{name: 'stringData', type: types_.STRING}]);
_checkClosed(this);
_checkWriteAccess(this._mode);
if (!arguments.length) {
- throw new WebAPIException(WebAPIException.NOT_FOUND_ERR,
- 'Argument "stringData" missing');
+ throw new WebAPIException(
+ WebAPIException.NOT_FOUND_ERR, 'Argument "stringData" missing');
}
var data = {
encoding: this._encoding,
offset: this.position,
data: args.stringData,
- rewrite: this._rewrite
+ truncate: this._truncate
};
- if( data.encoding == "iso-8859-1") {
+ if (data.encoding == 'iso-8859-1') {
check_characters_outside_latin1(data.data);
}
can_change_size = true;
this.position = this.position + args.stringData.length;
can_change_size = false;
- this._rewrite = false;
-};
+ this._truncate = false;
+}
FileStream.prototype.write = function() {
write.apply(this, arguments);
};
function writeBytes() {
- var args = validator_.validateArgs(arguments, [
- {
- name: 'byteData',
- type: types_.ARRAY,
- values: undefined /* was types_.OCTET, but checking moved to array_to_string for performance */
- }
- ]);
+ var args = validator_.validateArgs(
+ arguments, [{
+ name: 'byteData',
+ type: types_.ARRAY,
+ values: undefined /* was types_.OCTET, but checking moved to ArrayToString for
+ performance */
+ }]);
_checkClosed(this);
_checkWriteAccess(this._mode);
if (!arguments.length) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Argument "byteData" missing');
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR, 'Argument "byteData" missing');
}
var data = {
location: commonFS_.toRealPath(this._file.fullPath),
offset: this.position,
- data: array_to_string(args.byteData),
- rewrite: this._rewrite,
+ data: ArrayToString(args.byteData),
+ truncate: this._truncate,
};
var result = native_.callSync('File_writeBytes', data);
can_change_size = true;
this.position = this.position + args.byteData.length;
can_change_size = false;
- this._rewrite = false;
-};
+ this._truncate = false;
+}
FileStream.prototype.writeBytes = function() {
writeBytes.apply(this, arguments);
};
function writeBase64() {
- var args = validator_.validateArgs(arguments, [
- {
- name: 'base64Data',
- type: types_.STRING
- }
- ]);
+ var args =
+ validator_.validateArgs(arguments, [{name: 'base64Data', type: types_.STRING}]);
_checkClosed(this);
_checkWriteAccess(this._mode);
location: commonFS_.toRealPath(this._file.fullPath),
offset: this.position,
data: args.base64Data,
- rewrite: this._rewrite,
+ truncate: this._truncate,
};
var result = native_.callSync('File_writeBase64', data);
if (native_.isFailure(result)) {
- throw native_.getErrorObject(result);
+ throw native_.getErrorObject(result);
}
var written_bytes = native_.getResultObject(result);
can_change_size = true;
this.position += written_bytes;
can_change_size = false;
- this._rewrite = false;
-};
+ this._truncate = false;
+}
FileStream.prototype.writeBase64 = function() {
writeBase64.apply(this, arguments);