<commit_before>var ua = require('../../vigour-js/browser/ua')

module.exports = exports = {}

if(ua.native) {
	(function () {
		var util = require('./util')
			, nativeUtil = require('./nativeUtil')

		exports.readFile = function (path, options, cb) {
			nativeUtil.whenFsReady(function () {
				if (!cb) {
		  		cb = options
		  		options = {}
		  	}
		  	options = util.treatOptions(options)
		  	;(options.url !== false && util.url(path))
					? util.readAjax(path, options, cb)
					: readFile(path, options, cb)
			})
	  }

	  function readFile (path, options, cb) {
		  window.fs.root.getFile(path
		  	, {
		  		create: false
		  	}
		  	, function (fileEntry) {
		    fileEntry.file(function (file) {
		      var reader = new FileReader()
		      reader.onloadend = function (err) {
		      	if (err.error) {
		      		cb(err)
		      	} else {
		        	cb(null, this.result)
		      	}
		      }
		      reader.readAsText(file)
		    }, function (err) {
		    	err.failPoint = 'fileEntry.file'
		    })
		  }, function (err) {
		  	err.failPoint = 'getFile'
		  	cb(err)
		  })
		}

	  exports.writeFile = function (path, data, options, cb) {
	  	nativeUtil.whenFsReady(function () {
	  		if (!cb) {
		  		cb = options
		  		options = {}
		  	}
		  	options = util.treatOptions(options)
		  	;(options.url !== false && util.url(data))
					? exports.writeHttp(path, data, options, cb)
					: writeFile(path, data, options, cb)
	  	})
	  }

	  function writeFile (path, data, options, cb) {
			window.fs.root.getFile(path
				, {
					create: true
				}
				, function (fileEntry) {
			    fileEntry.createWriter(function (fileWriter) {
			      fileWriter.onwriteend = function (err) {
			      	if (err.error) {
			        	cb(err)
			        } else {
			        	cb(null)
			        }
			      }
			      fileWriter.onerror = function(err) {
			      	err.failPoint = 'onerror'
			        cb(err)
			      }
			    	fileWriter.write(data)
			    }, function (err) {
			    	err.failPoint = 'createWriter'
			    	cb(err)
			    })
		  }, function (err) {
	    	err.failPoint = 'getFile'
	    	cb(err)
	    })
		}

	  // exports.writeHttp = function (path, url, options, cb) {
	  // 	alert('writeHttp')
	  // 	util.readHttp(url, options, function (err, data) {
	  // 		if (err) {
	  // 			cb(err)
	  // 		} else {
	  // 			writeFile(path, data, options, cb)
	  // 		}
	  // 	})
	  // }

	  // exports.exists = function (path, cb) {
	  // 	whenFsReady(function () {
	  // 		window.fs.root.getFile(path, { create : false }, function () {
   //          cb(true)
   //      }, function () {
   //          cb(false)
   //      })
	  // 	})
	  // }<commit_msg>Added writeHTTP on client<commit_after>var ua = require('../../vigour-js/browser/ua')
	, fsRoot = "cdvfile://localhost/persistent/"
module.exports = exports = {}

if(ua.native) {
	(function () {
		var util = require('./util')
			, nativeUtil = require('./nativeUtil')

		exports.readFile = function (path, options, cb) {
			nativeUtil.whenFsReady(function () {
				if (!cb) {
		  		cb = options
		  		options = {}
		  	}
		  	options = util.treatOptions(options)
		  	;(options.url !== false && util.url(path))
					? util.readAjax(path, options, cb)
					: readFile(path, options, cb)
			})
	  }

	  function readFile (path, options, cb) {
		  window.fs.root.getFile(path
		  	, {
		  		create: false
		  	}
		  	, function (fileEntry) {
		    fileEntry.file(function (file) {
		      var reader = new FileReader()
		      reader.onloadend = function (err) {
		      	if (err.error) {
		      		cb(err)
		      	} else {
		        	cb(null, this.result)
		      	}
		      }
		      reader.readAsText(file)
		    }, function (err) {
		    	err.failPoint = 'fileEntry.file'
		    })
		  }, function (err) {
		  	err.failPoint = 'getFile'
		  	cb(err)
		  })
		}

	  exports.writeFile = function (path, data, options, cb) {
	  	nativeUtil.whenFsReady(function () {
	  		if (!cb) {
		  		cb = options
		  		options = {}
		  	}
		  	options = util.treatOptions(options)
		  	;(options.url !== false && util.url(data))
					? exports.writeHttp(path, data, options, cb)
					: writeFile(path, data, options, cb)
	  	})
	  }

	  function writeFile (path, data, options, cb) {
			window.fs.root.getFile(path
				, {
					create: true
				}
				, function (fileEntry) {
			    fileEntry.createWriter(function (fileWriter) {
			      fileWriter.onwriteend = function (err) {
			      	if (err.error) {
			        	cb(err)
			        } else {
			        	cb(null)
			        }
			      }
			      fileWriter.onerror = function(err) {
			      	err.failPoint = 'onerror'
			        cb(err)
			      }
			    	fileWriter.write(data)
			    }, function (err) {
			    	err.failPoint = 'createWriter'
			    	cb(err)
			    })
		  }, function (err) {
	    	err.failPoint = 'getFile'
	    	cb(err)
	    })
		}

	  exports.writeHttp = function (path, url, options, cb) {
	  	var fileTransfer = new FileTransfer();
			var uri = encodeURI(url);

			fileTransfer.download(
		    uri
		    , fsRoot + path
		    , function (entry) {
		    	cb(null)
		    }
		    , function (err) {
					cb(err)
		    }
		    , false
			);
	  }

	  // exports.exists = function (path, cb) {
	  // 	whenFsReady(function () {
	  // 		window.fs.root.getFile(path, { create : false }, function () {
   //          cb(true)
   //      }, function () {
   //          cb(false)
   //      })
	  // 	})
	  // }
{'fraction_non_alphanumeric': 0.15108575634891425, 'fraction_numerical': 0.0, 'mean_word_length': 1.7602844083291012, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '860460', 'n_tokens_mistral': 2275, 'n_tokens_neox': 1851, 'n_words': 335}