Monday, June 28, 2010

JSON-C Example

So you want to parse JSON with C? Welcome aboard!

First get json-c, configure, compile, and update your ld cache
tar xf json-c-0.9.tar.gz
cd json-c-0.9
./configure && make && sudo make install
sudo ldconfig

Now use one of the test files provided or this example:
cd ..
vim json-example.c
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>

#include "json.h"

int main(int argc, char **argv)
  struct json_object *new_obj;

  // I added some new lines... not in real program
  new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"pageCount\": 100, \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");
  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

  new_obj = json_object_object_get(new_obj, "glossary");
  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

  new_obj = json_object_object_get(new_obj, "pageCount");

  int pageCount = json_object_get_int(new_obj);

  printf("Page count = %d", pageCount);


  return 0;

Put it in a Makefile with the right settings.
vim Makefile
all: static

  gcc -static json-example.c -L/usr/local/lib -ljson -I/usr/local/include/json -o json-example-static

  gcc json-example.c -L/usr/local/lib -ljson -I/usr/local/include/json -o json-example-shared

.PHONY: all static shared

Finally test that it works:
make static
make shared

Hopefully that's enough to get you started. The big thing is that when you compile your own projects, make sure that you include the json library and paths.

Monday, June 21, 2010

Refactor from the Commandline

Here's an example script I used to refactor the names of files in a C project.
It can easily be adapted to refactor variable names, etc in JavaScript, Ruby, Python, PHP (*shudder*), etc.


if [ ! -n "${1}" ]
    echo "
Usage: new_basename old_basename

Note: do not include '.c' or '.h', this is added for you.


svn mv include/${OLD}.h include/${NEW}.h
svn mv ${OLD}.c ${NEW}.c

find ./ -name '*.c' | xargs sed -i "s/${OLD}\.h/${NEW}\.h/gi"
find ./ -name '*.c' | xargs sed -i "s/${OLD}\.c/${NEW}\.c/gi"

find ./ -name '*.h' | xargs sed -i "s/${OLD}\.h/${NEW}\.h/gi"
find ./ -name '*.h' | xargs sed -i "s/${OLD}\.c/${NEW}\.c/gi"

Saturday, June 19, 2010

OSX-style Screen Capture in Ubuntu Linux

Do you ever think to your self "Man, I love doing screen captures, but on Ubuntu Linux it's just cumbersome in comparison to Mac OS X" (notice that Windows 7 followed the trend with its new Snipping Tool too).

Guess What! It doesn't have to be cumbersome anymore:

System -> Preferences -> Keyboard Shortcuts
Name: OSX-style Screen Capture
Command: gnome-screenshot --area
Shortcut: alt-ctrl-4

Unfortunately there's a bug and you can't map alt-shift-4 as you would like. Sad day. Oh well.