Skip to content

Instantly share code, notes, and snippets.

Created November 2, 2011 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/1333180 to your computer and use it in GitHub Desktop.
Save anonymous/1333180 to your computer and use it in GitHub Desktop.
Array to String
var MyModule = {
writePosition : -1,
readPosition : 0,
myBuffer : [],
itemCount : 0,
defaultSize : 20,
carriageReturn : "99",
myArray : [],
//creates the buffer
setup : function(){
MyModule.myBuffer = [];
MyModule.myBuffer.length = this.defaultSize;
},
////////////////////////////////myModule methods///////////////////////////
//increments the write pointer
incrementWPosition : function(x){
if(x + 1 == this.myBuffer.length)
this.writePosition = x = 0;
x++;
this.writePosition = x;
return this.writePosition;
},
//increments the read pointer
incrementRPosition : function(x){
if(x + 1 == MyModule.myBuffer.length)
this.readPosition = x = 0;
x++;
this.readPosition = x;
return readPosition;
},
//adds an item into the buffer with perameter: matchedstring from my regex
addItem : function(feedbackItem, matchedString){
if(matchedString == MyModule.carriageReturn)
MyModule.onCarriageReturn();
MyModule.incrementWPosition(this.writePosition);
MyModule.myBuffer[this.writeposition] = matchedString;
this.itemCount++;
CF.log("new buffer element entered:" + matchedString);
},
getNoItems : function(){
return this.itemCount;
},
//extracts the wanted elements between the read pointer and the write pointer and returns a new array
getWantedValues : function(){
var element = 0;
var end = this.writePosition;
var start = this.readpostion;
do{
for (var i = start; i < end; i++)
{
MyModule.myArray[i] = MyModule.mybuff[i];
MyModule.incrementRPosition(this.readpostion);
}
element++;
}while (this.readpostion < this.writePosition);
return MyModule.myArray;
},
//this function executes the above function and then converts the array to the String
onCarriageReturn : function(){
MyModule.getWantedValues();
MyModule.array2String(MyModule.myArray);
},
//flushes the buffer
makeEmpty : function(){
this.itemCount = 0;
this.writePosition = -1;
this.readPosition = 0;
CF.log("Buffer has been flushed");
},
//this is my prefered function to convert my array into a string
//converts an array to string with perameter being the array
array2String : function(array) {
var result = "";
for (var i = 0; i < array.length; i++) {
result+= String.fromCharCode(parseInt(array[i], 2));
}
CF.setJoin("s55", result);
CF.log("bin to string function called");
},
//the showHex function that yields the error
showHEX: function (a) { var b=""; var ch=""; if(a=="")
return ""; if(a==undefined) return "(Undefined)";
for(var i=0; i<a.length;
i++) { ch=a.charCodeAt(i); if(ch<16) ch="0"+ch.toString(16).toUpperCase();
else ch=ch.toString(16).toUpperCase(); b+=ch+" "; };
return b; }
};
////////////////////////////////Main method///////////////////////////
CF.userMain = function() {
CF.watch(CF.FeedbackMatchedEvent, "GlobalCache", "My_IP", MyModule.addItem);
};
////////////////////////////////push module function///////////////////////////
CF.modules.push({
name : "My module",
setup : MyModule.setup,
object : MyModule
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment