function

Trimming spaces and comments in C

This is a simple C function that reads a line from a file and trim all comments and spaces.
You can see that the function receives a pointer to a file, a pointer to a char and an integer holding the size of the line to be read from the file.
The function then returns a line from the file without spaces(leading/trailing) and comments

Function signature:

 
 char * readLine (FILE* fp, char* line, int size)  

One thing I want to point out is the fact that it is possible to trim the string without creating a new one.
By using pointer arithimetics you can manipulate the chars of the line and remove anything you want.

For example, lets say you have a string like this:

*I’m using 0x00n just as an example to demonstrate the memory location of each char.

So the string A1 has two leading and trailling spaces.
Assuming that the line read from the file is: ” a1 ”

1.

First we remove the comments, none is this case:

   
 s = strchr(line, ‘#’);  
 if (s) *s = ‘\0';  

We use strchr to search the string for any occurrences of #

If # is found in the string we set the null byte to the position of the first #

2.

The next step is to remove all the trailing spaces:

 
 s = line + strlen(line) – 1;  
 while (isspace(*s)) s–;  
 *(s+1) = ‘\0';  
  • First we assign to s the position of the last char in the line string.

  • Second we check if the char is a space using the isspace function(it checks not only for spaces, but for other delimiters as well). If the char is a space we subtract one from the s, meaning we subtract a char from s, setting s to point to one char before.

  • Once we find a char that’s not a space we break the loop.

  • Finally, we add one to s, and set the null byte to the position of the first space after the string ends.

3.

To remove leading spaces is even simpler:

 
 s = line;  
 while (isspace(*s)) s++;  

We set s to point to the first char in the string read from the file.
Then we loop through the string checking if the char is a space and incrementing the pointer by 1.

After all the trimming, s will point to the first non space char in the string, and the null byte will be positioned right after the last non space char. It will also have all the comments removed.

One thing to notice with this approach, is that the function must receive a char* and return a char.
The reason beginning is that the char
needs to be declared in the function that is calling readLine, in this example the main. Since if not, the scope of the char* would be tight to the readLine function and thus the calling function would not be able to access the trimmed string.

Another possibility could be to manipulate the char* line itself, removing the necessity to return a new char.

This trim function could be adapt to not only work with files, but with different data structures as well.

If you have any suggestions or tips please leave a comment bellow :D

Full Source Code:

  
 #include  
 #include  
 #include

char *  
 readLine (FILE* fp, char* line, int size)  
 {  
 char* s = NULL;

 while (!feof(fp) && fgets(line, size, fp)) {  
 // Strip comments  
 s = strchr(line, ‘#’);  
 if (s) *s = ‘\0';

 // Remove trailling spaces  
 s = line + strlen(line) – 1;  
 while (isspace(*s)) s–;  
 *(s+1) = ‘\0';

 // Remove leading spaces  
 s = line;  
 while (isspace(*s)) s++;

 // Don’t return empty lines  
 if (*s) return s;  
 printf("empty linen");  
 }

 return NULL;  
 }

int  
 main (void)  
 {  
 FILE* fp = NULL;  
 char line[256];  
 char* s = NULL;  
 fp = fopen("file", "r");  
 if (!fp) return 1;  
 while ((s = readLine(fp, line, sizeof(line)))) {  
 printf("s: %s. || line: %s.n", s, line);  
 }  
 return 0;  
 }  

Test file:

start
a1
a2
a3 #other comment
end

start
b1
b2
start
c1#comment….
c2
end
end
[/sourcecode]

This and That - Understanding Javascript functions

Douglas Crockford mentions in his lectures OnJavascript, that javascript is so powerful that even without understanding the language it’s possible to program with it.
Now I understand what he means.
Javascript was the first language I was introduced to, not learnt, but introduced.
I’ve always used javascript to complement the development of web applications. Usually using frameworks, like jquery or mootols. But never by writing pure javascript code.

Even when I had to write some javascript code, was to use certain APIs, like googlemaps, or groupon for example.
Now that I actually need to write javascript code, I realized that I don’t know javascript as a language.

So what’s the solution? Read some books and write some code :)

This weekend I’ve been trying to understand the library html5PreLoader. The library pre-loads resources into the browser.
While reading the code, I end up looking at some parts and having no clue about what they were doing. Functions inside functions, functions inside objects, functions inside variables, and even functions inside functions.
Totally lost, I went to read about how functions work in javascript.

Here are a few examples of how functions can be used:

Functions used as methods:

A function can be a method in javascript. If the function is stored as a property in an object, it is considered to be a method.
When a method is invoked, this is bound to that object

 
 var myObject = {  
 value: o;  
 increment: function (inc) {  
 this.value += typeof inc === ‘number’ ? inc : 1;  
 };  
 myObject.increment();  
 document.writeln(myObject.value); // 1  
 myObject.increment(2);  
 document.writeln(myObject.value); // 3  

Inner functions:

If a method(function) of an object has a inner function, it is not possible to reference the object properties using the keyword this.
To access the properties of the object, the method must assign this to that. Then the inner function can refer to that as being the object itself.
To fix the problem the value of this is assigned to that

 
 function add(n1, n2)  
 {  
 return typeof (n1 === ‘number’ && typeof n2 === ‘number’) ? n1 + n2 : 0;  
 }  
 myObject.doubleNumber = function () {  
 var that = this;  
 var helper = function () {  
 that.value = add(that.value, that.value);  
 };  
 helper();  
 }  
 myObject.doubleNumber();  
 document.writeln(myObject.value);  

Another workaround for the problem is:

  
 if (!Function.prototype.bind) {

Function.prototype.bind = function (oThis) {

if (typeof this !== "function") // closest thing possible to the ECMAScript 5 internal IsCallable function  
 throw new TypeError("Function.prototype.bind – what is trying to be fBound is not callable");

var aArgs = Array.prototype.slice.call(arguments, 1),  
 fToBind = this,  
 fNOP = function () {},  
 fBound = function () {  
 return fToBind.apply(this instanceof fNOP ? this : oThis || window, aArgs.concat(Array.prototype.slice.call(arguments)));  
 };

fNOP.prototype = this.prototype;  
 fBound.prototype = new fNOP();

return fBound;  
 };  
 }

var x = 9;  
 var module = {  
 x: 81,  
 getX: function() { return this.x; }  
 };  
 document.writeln(getX()); // 81

var getX = module.getX;  
 document.writeln(getX() + ‘
’); // 9, because in this case, "this" refers to the global object // create a new function with ‘this’ bound to module var boundGetX = getX.bind(module); document.writeln(boundGetX()); // 81

What happen is when getX is invoked as a method, it has access to this(module object).
However when the method getX is copied to a variable, it loses the module scope, and this now refers to the global object.
So this.x in getX access the global variable x.
The fix is binding getX to the module object. So then this.x will access the module.x variable.

The only problem with this solution is that isn’t cross browser compatible.

Further readings and references:
Javascript the Good Parts
https://developer.mozilla.org/en/JavaScript/Reference/GlobalObjects/Function/bind
https://developer.mozilla.org/en/JavaScript/Reference/Global
Objects/Object
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function