Skip to content

Instantly share code, notes, and snippets.

@fpillet
Forked from anonymous/MyBuffer.js
Created November 2, 2011 09:26
Show Gist options
  • Save fpillet/1333248 to your computer and use it in GitHub Desktop.
Save fpillet/1333248 to your computer and use it in GitHub Desktop.

Revisions

  1. fpillet revised this gist Nov 2, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MyBuffer.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ var MyModule = {
    buffer : "",
    defaultSize : 20,
    carriageReturn : "99"
    }
    },

    setup : function() {
    // we could remove this function if there is nothing to do
  2. fpillet revised this gist Nov 2, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MyBuffer.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ var MyModule = {
    var self = {
    buffer : "",
    defaultSize : 20,
    carriageReturn : "99",
    carriageReturn : "99"
    }

    setup : function() {
    @@ -30,7 +30,7 @@ var MyModule = {
    makeEmpty : function(){
    self.buffer = "";
    CF.log("Buffer has been flushed");
    },
    }
    };

    ////////////////////////////////Main method///////////////////////////
  3. fpillet revised this gist Nov 2, 2011. 1 changed file with 21 additions and 133 deletions.
    154 changes: 21 additions & 133 deletions MyBuffer.js
    Original file line number Diff line number Diff line change
    @@ -1,138 +1,36 @@
    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;

    var self = {
    buffer : "",
    defaultSize : 20,
    carriageReturn : "99",
    }

    setup : function() {
    // we could remove this function if there is nothing to do
    },

    ////////////////////////////////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;
    addItem : function(feedbackItem, matchedString) {
    if (matchedString == self.carriageReturn) {
    CF.setJoin("s55", self.buffer);
    self.buffer = "";
    } else {
    self.buffer = self.buffer + String.fromCharCode(parseInt(matchedString, 10));
    }
    CF.log("new buffer element entered:" + matchedString);
    },

    //this function executes the above function and then converts the array to the String
    onCarriageReturn : function(){

    MyModule.getWantedValues();
    MyModule.array2String(MyModule.myArray);

    getNoItems : function() {
    return self.buffer.length;
    },

    //flushes the buffer
    makeEmpty : function(){

    this.itemCount = 0;
    this.writePosition = -1;
    this.readPosition = 0;
    CF.log("Buffer has been flushed");
    self.buffer = "";
    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///////////////////////////
    @@ -148,14 +46,4 @@ CF.modules.push({
    setup : MyModule.setup,
    object : MyModule

    });










    });
  4. @invalid-email-address Anonymous created this gist Nov 2, 2011.
    161 changes: 161 additions & 0 deletions MyBuffer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,161 @@
    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

    });