Archive for the ‘Development’ Category

Lightweight IPv4

Published in: Development, Language, PHPPosted by Jdub7 on 08/03/09


For a midsize project I wanted to store IP ranges in the database with the option to to also store CIDR blocks. CIDR blocks, though powerful are somewhat difficult for a typical user. Additional using them is not as precise for all ip ranges. There are a plethora of tools available on the internet that will do what I would like. However, incorporating these tools would not be practical. What I want is to mimic the functionality of these tools so that it can be easily imported into any of my projects. The most coveted tool for me would be to convert an IP range to a precise range of CIDR blocks. This required specific functionality not naturally provided in PHP.

  • Check for Valid Netmask
  • Check whether an IP address is within a CIDR block.
  • Take user input and a Netmask and make it into a valid CIDR block.
  • CIDR number into Netmask
  • Netmask to CIDR
  • Take an IP range and fit it into an exact range of CIDR blocks.

This presents some difficulty in that PHP’s network functions are not thorough enough. The revelation came when I realized that an IP address is merely a number. In fact the whole protocol is rooted in binary using very specific patterns. With that in mind I thought we could develop very light weight methods to solve our problem.

The Code

It is important to note that the methods provided are meant for IPv4 addresses only are only tested on a 32bit system. Also, I did not care to do much in the way of error checking, but doing so, like testing whether the CIDR number is unsigned and less than or equal to 32, should be trivial.

Though the solution I sought after would require PHP I didn’t limit myself to that language only. In fact the PHP code I found seemed inefficient. Most involved a number conversions or parsing the address using sprintf using loops and nested if statements. Indeed the most efficient code, which shouldn’t surprise most was in ANSI C. Bit Twiddling Hacks resource proved very useful.

About Binary

I am not attempting to teach binary math. Since the code does not “read like prose” a small amount of knowledge is required in order to understand the code. Some excellent resources are Wikipedia’s article on CIDR and PHP binary operators.

Joy Of Groovy

Published in: Development, Groovy, LanguagePosted by yighu on 07/29/09

In my work and my volunteer, I need to do a lot of scripting for all kinds of things such as system administration, data migration etc. I have used shell script, perl and many other tools on and off for a long time before. Scripting with Groovy brings me much joy and fun.

Groovy brings the full Java language and its own flavors to your finger tips while you are typing and thinking your tasks and you can do a lot of things very easily and much simpler than other scripting. Here I will put up a few examples that demonstrate a few tricks that can simplify a lot of daily scripting work:

  1. File Manipulation
  2. Configuration
  3. Database Access
  4. Logging

Suppose you want to create a file and put stuff in it. This is how you do it:

def file=new File(“test.txt”)

do this: file.append(“line 1\n”) file.append(“line 2\n”)

Now the file has 2 lines.

Later some where, you want to do something with the data in the file, for example email the data to some one, this is how you get the content for the file: def content=file.txt()

Suppose you want to write a method, for example log(String text). The log method basically append what ever text into a log file.

binding[‘writer’]=new File(“log.txt”)

def log(String text){ writer.append(text+”\n”) }

Now suppose, instead of putting the log into a log file, I want to put the log into a database table for easy to search or may be for data warehousing purpose, you can easily change the above log method without touching the rest of your code.

def log(String text){ sql.execute(“insert into usertrace(id,description) value(?,?)”,[nextid(),text]) }

where nextid() is simply a method that return you the next id of table usertrace.

Now what is sql here? Before I talk sql, I need to touch the groovy configuration a bit.

The ConfigSlurper is a cool thing.

You might have been used to manipulate the java Property class which you need to load the property file and then use getter method to fetch the string property you want. The groovy way is much simpler.

In this code, MyConfig.groovy is a groovy class which contains name/value pairs, one of them is called sql like this:


This is how you get the property sql.

binding[‘cfg’]=new ConfigSlurper().parse(new File(‘MyConfig.groovy’).toURL()) binding[‘sql’]=cfg.sql

Notice that the new variable sql is an object, not just a String as in Java Property file. Then you can execute the method of the object sql, like we did in the above log method.

Have fun so far! As you explore Groovy, you will discover them more.

What’s New With Snipplr

Published in: Design, Development, NewsPosted by Jason Tan on 01/17/08

I wanted to write a quick post to update you all on what’s been going on with Snipplr.

First, thank you for your feedback on the new design, both positive and negative. It’s hard to be all things to all people, but we’re going to give it our best shot. Trey is busy working on an alternate style that is more minimalistic, smaller, and has more room to view code. We’d love to give you more control over your preferred interface. In the meantime, I’ve made some small changes to the interface – like making the code editing block more suitable for code on both the new and edit snippet pages.

Aside from the design, I’ve been working to make some system changes:

  • Search. Searching now works more like how you would expect. I’ve also added an advanced search, which allows you to search source code and filter by language.
  • Languages. I added some new languages that were requested and also upgraded GeSHI (for syntax highlighting) to the latest version.
  • Spam. I replaced the math captcha for new user registration with reCAPTCHA and haven’t seen any spam bots try to create an account since.
  • Details. There are lots more minor changes and fixes around the site on both the front and back end. As always, please contact us with any bugs or suggestions you have.

We have lots of more ideas and things in the works, so keep checking back here for the latest news.

Snipplr has moved!

Published in: Development, NewsPosted by admin on 10/12/07

In case you hadn’t noticed, Snipplr moved to a new hosting provider, Pair is by far the most reliable and best performing shared hosting environment we have ever worked with. Snipplr will now be faster than ever!

Now that the move is behind us, we’ll be focused on a new development. We expect to launch our new design in the next few weeks, and after that we’ll be focused on some cool new features for Snipplr.

New Snipplr TextMate Bundle (Beta)

Published in: Brainstorming, Development, News, PluginsPosted by Tyler on 05/09/07

Today I want to share with everyone a beta version of a new TextMate bundle I’m working on for Snipplr. It’s a major improvement over the old bundle.

Previously, it would pop-up a box for you to type in some keywords. Then it would search Snipplr and show you a list of matching snippets you could insert into your document. That worked pretty well, but you had to be online for it to communicate with Snipplr. You’d lose access to your snippets if you weren’t connected to the net.

This new version syncs your snippets and favorites directly into TextMate’s bundle menu – giving you offline access to all your code stored in Snipplr. If you don’t mind me saying so, this rocks. Here’s a screen shot:


Keep in mind that this bundle is still in development. Everything is working fine for me, but your mileage may vary. You can download the new bundle here. Note: You must be logged in to Snipplr for the download to work. Also, make sure you uninstall the old bundle from TextMate before installing this one.

For the nerds in the audience…

This bundle was a lot of fun to write because it forced me to dig into TextMate’s bundle format. I’ve written some advanced bundles before (at least I consider them to be . . . at work we use a bundle that lets us stage and deploy our clients’ websites on remote servers via TextMate) but nothing like this.

When you download the bundle from Snipplr, PHP generates a custom bundle on the fly based on the snippets in your account. It creates all the .tmSnippet and .tmCommand files as well as the OS X property listings. It’s by no means brain surgery, but it definitely took some tinkering to make it come out right.

The bundle also includes a command to refresh itself. It does this by downloading a new bundle in the background and rsync’ing it with the pristine copy stored in TextMate. I consider this a cheap hack – I’m hoping TextMate 2.0 provides an official API for bundles to self-update. (Ideally, each bundle .plist would contain a URL and version number that TextMate could use to search for updates and, if available, update the bundle à la Firefox extensions. You there, Allan? :)) It tells TextMate to update its bundle listing using a quick AppleScript command (osascript -e 'tell app "TextMate" to reload bundles').

There’s probably a better way to do all this – I need to search the TextMate mailing list.

Next up, I’m going to try and register the update command with launchd so the bundle can stay in sync automatically – preferably when TextMate isn’t active.

I’d appreciate any feedback from the Snipplr community on this bundle. Bugs and feature requests are always welcome.

Also, if anyone is interested in beta testing the new version of Snipplr we’re working on, let me know.

Snipplr API Updated

Published in: API, DevelopmentPosted by Tyler on 01/30/07

I bet you all thought Snipplr was left for dead, eh? Sorry about that. In the last couple months I’ve gotten married, bought a house, and helped roll out a new service. Things have been busy 🙂

Back to business. I’ve updated the API’s method to allow specifying a language type. To do so, pass an (optional) fourth parameter containing the language ID. What is the language ID you may ask? It’s the URL formatted language name found in all of Snipplr’s URLs. For example:

In these URLs, the language IDs are “javascript”, “c-plus-plus”, “c-sharp”, and “cold-fusion”, respectively.

If that’s not helpful enough, I’ve also added languages.list method which returns a list of language url names and their corresponding pretty names. i.e., c-sharp and C#, c-plus-plus and C++.

You can view the API reference here.

Let me know if you run into any issues.

Snipplr Search Integrated With Firefox 2.0

Published in: Development, News, Plugins, ScreencastsPosted by Tyler on 10/25/06

Yesterday, Mozilla released Firefox 2.0 and with it a slew of new features. One of them is autodiscovery of websites that support Amazon’s OpenSearch format. Amazon describes OpenSearch as “a set of simple formats for the sharing of search results.”

What this means to you is Snipplr (and other websites with searchable content) can be searched directly from Firefox’s search box. There’s no need to first go to Snipplr and then do a search. Now you can search from inside your browser no matter what site you’re currently on. Here’s how it works.

Using Firefox 2.0, go to – you’ll see the small arrow next to Firefox’s search box light up.

Firefox Search Box

Click on the arrow and choose “Add Snipplr” to add it to your list of search engines.

Firefox Search Engines

Now, you can search Snipplr using Firefox just like you do Google. Snipplr will even suggest searches as you type them! Here’s a short screencast showing the whole process in action.

TextMate Bundle Updated

Published in: Development, News, PluginsPosted by Tyler on 10/05/06

I’ve updated the Snipplr TextMate bundle to fix a small bug. Now it’ll give you a nice error message if you search for snippets and don’t find any. Thanks to Brian for pointing this out.

Snipplr Redesign – Things to Come

Published in: Design, Development, NewsPosted by Tyler on 10/03/06

As some of you know, Snipplr was bought by Sitening last month. It’s a great oppurtunity to help Snipplr continue to grow and even let me get paid for it 🙂 One of the first improvements coming to the site is a new design. Here’s a quick preview:

Snipplr Design Preview

Updated Snipplr API

Published in: API, Development, NewsPosted by Tyler on 09/20/06

Based on visitor feedback (thanks, Hernán!) I updated the Snipplr API this morning. The snippet.get method now returns a list of the snippet’s tags. Also, I added a much needed snippet.delete method that (you guessed it) lets you delete snippets from your account. Click here for the API documentation.