Using .net to parse a csv with quote delimination

So I have a csv file, and it has quote delimination, eg:
one,two,"with , comma",four

There are a million examples of baroque regex strings to parse these, but I couldn’t seem to get any of them to work. So I wrote a small bit of code to parse each line.

Essentially pass each line into this code as a string, and it returns an array of values. Sorry for the bad spacing, wordpress y’all.

class Program
{
public enum parseState
{
word,
comma,
quote
}

public Program(){
}

public ArrayList parse(string text)
{

ArrayList vals = new ArrayList();
int i =0;
while (i < text.Length)
{
vals.Add(GetWord(text,ref i));
}
//special handling if the last char is empty
if (text[text.Length -1].ToString() == ",")
{
//append an empty val
vals.Add(String.Empty);
}

return vals;

}

private string GetWord(string text, ref int position)
{
parseState state = parseState.word;
string word = string.Empty;
while (position < text.Length)
{
string letter = text[position].ToString();
position++;
switch (letter)
{
case ",":
if (state == parseState.word)
{
//were done;

return word;
}
else if (state == parseState.quote)
{
//were in a quoted section add it
word += letter;
}
else if (state == parseState.comma)
{
//empty string, fair enough, return it
//were done;
return word;
}
break;
case "\"":
if (state == parseState.word || state == parseState.comma)
{
//beginning of quoted section
state = parseState.quote;
}
else if (state == parseState.quote)
{
//end of a quoted section
//were done
return word;
}
break;
default:
if (state == parseState.word)
{
//normal
word += letter;
}
else if (state == parseState.quote)
{
//were in a quoted word
word += letter;
}
else if (state == parseState.comma)
{
//start of a word, fine.
word += letter;
state = parseState.word;
}
break;

}

}
return word;
}
}

Your First Facebook Application

Reposted from an article I wrote for Codesta.com article here


Writing a Facebook Application

With the rise of social network applications such as Facebook over the last 3 - 5 years, it’s become very attractive to be present in that space.  Facebook applications are most often games that allow users to play against each other, or themselves in the case of solo-games, but there is no technology limit to this. The application could easily be an extension of your own application.

One of the many great things you get from using the Facebook platform, is an
enormous list of potential clients who already have user/login info
for your application! Couple this with the outsourcing of user login/management/authentication to another provider (Facebook), access to the Facebook messaging API, and it’s easy to see the return on investment.

Getting Started

Assuming you have a Facebook account , you will need to install the Facebook developer application . Once you have the developer application installed, you should be directed to the developer home page, and here you can apply for an application key. When you apply for an application key, it asks for an application name, we’ll call ours Hello World. Now that you have a Facebook application registered, you can retrieve your API key, and secret key from the installed applications page.

We will be building a .NET application using the .NET Facebook dev kit from Microsoft , but you can use any modern language. There are development libraries available for all of them, and the developers wiki is a great resource… here is the link.

Assuming you will be developing using your local machine, we need to configure a couple values in your Facebook application, which can be done on the application settings you can access from the applications page here

  • Set your Callback URL to your local instance url (e.g. http://localhost/); if you are using Visual Studio’s built in server/debugging, then you will need to fix the port that it uses, and enter that as your Callback URL. If you are using a public server, then enter the URL for that server in this box.
  • Set your Canvas Page URL to anything you like, this is the url that Facebook users will use to access your application.
  • Set the application to iFrame - This allows you to have any content you choose, without being limited by the FBML (Facebook Markup Language) and it’s slow rendering times.
  • Set the application type to Website.

There are many more options there that you can play with , but these are the ones you need to get your application running.

After installing the Facebook Dev Kit from Microsoft, you will have the Facebook libraries located in the install location (C:\Program Files\Facebook Developer Toolkit). There will now be Facebook components available in Visual Studio, but we will do our example by hand.

The Code

Create a new web page project, and open Default.aspx to insert the following content:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HelloWorld.aspx.cs" Inherits="HelloWorld._default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>FaceBook Hello World</title>
</head>
<body>
    <form id="form1" runat="server">
        <h1>Ours</h1>
    <asp:Repeater ID="friendList" runat="server">
    <HeaderTemplate>
    <table>
    <tr>
    <td><b>Name</b></td><td><b>Photo</b></td>
    </tr>
    </HeaderTemplate>
    <ItemTemplate><tr><td><%#DataBinder.Eval(Container.DataItem,"Name")%></td>
    <td><img src="<%#DataBinder.Eval(Container.DataItem,"PictureUrl")%>" /></td></tr></ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    </form>
</body>
</html>

Now open the code behind file Default.aspx.cs , and populate it with this

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.ObjectModel;
namespace
HelloWorld
{
    public partial class _default : System.Web.UI.Page
    {
        private Facebook.Components.FacebookService _facebookApi = new Facebook.Components.FacebookService();

        protected void Page_Load(object sender, EventArgs e)
        {

            // ApplicationKey and Secret are acquired when you sign up for
            _facebookApi.ApplicationKey = "[YOUR KEY]";
            _facebookApi.Secret = "[YOUR SECRET KEY]";
            _facebookApi.IsDesktopApplication = false;

            string sessionKey = Session["Facebook_session_key"] as String;
            string userId = Session["Facebook_userId"] as String;

            // When the user uses the Facebook login page, the redirect back here will will have the auth_token in the query params

            string authToken = Request.QueryString["auth_token"];

            if (!String.IsNullOrEmpty(sessionKey))
            {
                _facebookApi.SessionKey = sessionKey;
            }
            else if (!String.IsNullOrEmpty(authToken))
            {
                _facebookApi.CreateSession(authToken);
                Session["Facebook_session_key"] = _facebookApi.SessionKey;
            }
            else
            {
                Response.Redirect(@"http://www.Facebook.com/login.php?api_key=" + _facebookApi.ApplicationKey + @"&v=1.0");
            }

            if (!IsPostBack)
            {
                // Get our list of friends
                Collection<Facebook.Entity.User> friends = _facebookApi.GetFriends();

                //bind our repeater
                friendList.DataSource = friends;
                friendList.DataBind();
            }
        }
    }
}

The basics here should be clear. You set your application and secret keys, and then your basically good to go! The Facebook.Components.FacebookService class provides access to the current user, and if they aren’t logged in you can bounce them to the Facebook login screen.

To go live with your project, simply move the code to a public server, change your callback url in the application settings page within Facebook and select the ‘add application to directory‘ check box on that same page.

Now that you have the user authenticated, and access to their friends list, the sky is the limit. We chose to store user keys in our database for customizations which we simply did using their Facebook ID.

In Conclusion

In practice I found the Facebook API to be very usable, and there were no unexpected hurdles to work around. We did have an occasional user authentication error, which may have been caused by having mutliple users logged in on the same machine, but this was an edge case.

In all, I wouldn’t hesitate to recommend the platform as an extension of an existing business giving a low-cost, low-risk entry into the seemingly persistent world of Social Networking Applications. However, I would hesitate before putting serious development into a FBML application, or anything that specifically required Facebook, for the same reasons I wouldn’t want to own a million dollar application that only ran in Friendster, Tribe.net or any of the other Social Networking sites that had their time in the sun.

The move to ubuntu

I’d been running Ubuntu in a dual boot environment for about 8 months (using the great Wubi to install it) , and finally decided to take the plunge, and go ubuntu as my main OS.

Installing Ubuntu is dead easy, although each time I’ve done it, i’ve had to use the alternate cd which is a text based install, not sure why that is.

Being a windows developer, I needed to maintain an environment for development, and I had decided to use VMWare to do this. After seeing the $200 price tag, i decided it was worth a look around for alternatives, enter VirtualBox.

To support the move I got all new hardware as my old AMD 2800XP was showing its age badly, the reason i moved to Ubuntu in the first place.

After a few steps, namely downloading the deb from their site, instead of using the repository, and there are some steps to get usb working on Ubuntu here ( they say they are for 7.04, i did these steps for 7.1 without a problem) It was up and running like a champ!

Installing windows on the virtualBox machine, took about 10 minutes, the fastest by far I have ever seen it install, of course this is thanks to my new hardware, but I was very impressed!

And here we are, running windows on Ubuntu, full colour and all!
VirtualBox on Ubuntu

—update–

After installing VirtualBox’s guest additions into my windows environment (same as the virtual pc, or VMware additions) I can arbitrarily resize my virtualbox, including fullscreen. Which, is pretty amazing, check the video.


Million Dollar Idea

My new database design is unstoppable!



Interface Idea for a decision tree

Often we force users to make a series of choices, which is fine, but how do we make it easy for them to keep track of their choices? And how do they know how to change their minds?

A common solution is the cookie trail, which is alright, but its dual purposing from navigation to choice trail never sat right with me.

Playing with some of the iPhone navigation , I got thinking that this would be a good way to do a choice tree, and this is my not very pretty version of that (IANAD), so imagine it all rounded and grey/blue.

The hooks are all in place where this could be tied to some ajax to load the questions/save the choices, but here we have a rough version of the functionality.

Some flickering is present, i understand this can be corrected in scriptaculous, but that’s not what I’m here for.
In both of these, make sure and try to change one of your decisions, by clicking it.

Heres one version, which is good for a web interface I think

This version, I think the idea would map to an iphone style interface nicely.

Adobe apollo / air

So,

I’ve seen this one coming for a while, and I’m in favor. Essentially AIR (adobe integrated runtime) is a standalone browser. So you can run websites like applications on your desktop.

The freedom of developing web tech without worrying about browser compatibility is a grand thing indeed. I knocked out this sample app in about 45 minutes, it’s enabled by the fact that air lets you do cross domain httprequests.

You’ll need to install the air platform from here

Heres the source code for the slideshow

So here you go, its a random slide show of the vicemagazine dos’ and donts’.

air

Enjoy

Micro Loans

So, this is interesting.

You give small loans to entrepeneurs in devloping countries, they run the business, and then pay you back… Kiva.org is the big site it would seem for this kind of thing, and apparently they have a very high (>95%) payback rate… So I’m trying it out.. I’ve given small amounts to 3 different people.

I think that’s pretty darn neat stuff.

Installing Image Magick, Magick Wand, PHP, FreeType

This is largely by another fellow from

here , I added some updated version #’s, as well as freetype support.

——————-

I did the build in tmp,

Get your ImageMagick
wget ftp://ftp.imagemagick.net/pub/ImageMagick/ImageMagick-6.2.9-8.tar.gz

Get your delegates

wget wget ftp://ftp.imagemagick.org/pub/ImageMagick/delegates/jpegsrc.v6b.tar.gz
wget wget ftp://ftp.imagemagick.org/pub/ImageMagick/delegates/jasper-1.701.0.zip
wget ftp://ftp.imagemagick.org/pub/ImageMagick/delegates/libpng-1.2.12.tar.gz
wget http://www.peregrinehw.com/downloads/gd/ghostscript-8.15.tar.bz2
wget http://www.peregrinehw.com/downloads/gd/ghostscript-fonts-std-8.11.tar.gz

extract them all.

copy the ghostscript fonts to the default dir.
mkdir /usr/local/share/ghostscript/fonts

cp ./ghostscript-fonts-std-8.11/* /usr/local/share/ghostscript/fonts/

make an ‘ext’ directory in ImageMagick
mkdir ./ImageMagick-6.2.9-8/ext

copy all the delegate folders there, give them nicer names like
jpeg
jasper
libpng
ghostscript

./configure and build jpeg
copy the jpeg folder into the ghostscript folder

./configure and build ghostscript

./configure and build jasper

./configure and build and install libpng

i use a text file for my configure params for ImageMagick
cd /tmp/ImageMagick-6.2.9-8/

touch myconf

this is the contents of MY configuration
./configure  –with-windows-font-dir=/usr/share/fonts/monotype/TrueType –enable-shared –with-exif=yes –enable-lzw=yes –with-gs-font-dir=/usr/local/share/ghostscript/fonts -without-x CPPFLAGS=’-I/tmp/ImageMagick-6.2.9/ext/jpeg -I/tmp/ImageMagick-6.2.9/ext/libpng -I/tmp/ImageMagick-6.2.9/ext/jasper’ LDFLAGS=’-L/tmp/ImageMagick-6.2.9/ext/jpeg -L/tmp/ImageMagick-6.2.9/ext/libpng -L/tmp/ImageMagick-6.2.9/ext/jasper -lfreetype’

you can see where you point ImageMagick at the various delegates

so, then I configure
./myconf > configure.log

after that happens, inspect the configure.log  , with any luck it’ll look like this, the only thing i didn’t get was jpeg2000 (thats jasper) , I’m not to worried about it, but possibly if you did a make build on jasper before configuring image magick it would work.

—-
Shared libraries  –enable-shared=yes        yes
Static libraries  –enable-static=yes        yes
Module support    –with-modules=yes        yes
GNU ld            –with-gnu-ld=yes        yes
Quantum depth     –with-quantum-depth=16    16

Delegate Configuration:
BZLIB             –with-bzlib=yes        yes
DPS               –with-dps=yes        no
FlashPIX          –with-fpx=no        no
FontConfig        –with-fontconfig=no        no
FreeType          –with-freetype=yes        yes
GhostPCL          None                pcl6 (unknown)
Ghostscript       None                gs (8.15)
Ghostscript fonts –with-gs-font-dir=/usr/local/share/ghostscript/fonts    /usr/local/share/ghostscript/fonts/
Ghostscript lib   –with-gslib=yes        no
Graphviz          –with-gvc=yes        no
JBIG              –with-jbig=yes        no
JPEG v1           –with-jpeg=yes        yes
JPEG-2000         –with-jp2=yes        no
LCMS              –with-lcms=yes        no
Magick++          –with-magick-plus-plus=yes    yes
PERL              –with-perl=yes        /usr/bin/perl
PNG               –with-png=yes        yes
RSVG              –with-rsvg=no        no
TIFF              –with-tiff=yes        no
Windows fonts     –with-windows-font-dir=/usr/share/fonts/monotype/TrueType    /usr/share/fonts/monotype/TrueType/
WMF               –with-wmf=yes        no
X11               –with-x=no            no
XML               –with-xml=yes        yes
ZLIB              –with-zlib=yes        yes
——–

then do make && make install , walk away, it takes about forever..

that should do it! try this out
echo “This is a test” | convert -background yellow -page 200×50 text:- mediocreimage.jpg

got me a blackberry.

Got a blackberry 7100t, $100 at tiger direct, hoping to use it to organise GTD lists.
It’s not super duper (bluetooth very limited, cant use data services on fido despite massive internet rumours to the contrary) but i really just want the pim functionality.
I didn’t realise the OS wasn’t open, so there’s not as much software as for a palm, i need dope wars.

A Blog you say?

Not one to normally go in for this, I’m looking at implementing wordpress for a client, so I may as well be productive with it!

I intend to use this to record handy things that I’ve learned.