8/24/11

How to Parse JSON Using C# .Net

There are a few libraries you can use to parse json but they can be a pain to initialize and setup when all you need is to grab a specific value given a parameter name and you don't have an array of data.


Here's a function I use for the above scenario:






private string ParseJSON(string getField, string jsonData)
{
string returnValue = "";

returnValue = jsonData.Substring((jsonData.IndexOf(getField) + getField.Length + 3));
if(returnValue.IndexOf(",")<0)
returnValue = returnValue.Substring(0, returnValue.IndexOf("}") - 1);
else
returnValue = returnValue.Substring(0, returnValue.IndexOf(",") - 1);


return returnValue;

}

6/27/10

Installing Ruby on Rails on Ubuntu 10.04

I am really impressed with how Ubuntu 10.04 has resurrected an old computer of mine. The computer was running Windows XP and it was too slow to even start up. Now with Ubuntu 10.04 on it I can develop MySQL/Apache applications/servers with ease. As an experiment, I am going to see how this old computer handles running Ruby on Rails as well. This post describes how to install ruby on rails on a Ubuntu 10.04 server.

Open a terminal and enter the following command:


sudo apt-get install ruby-full build-essential


Install Apache

sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev


Install Mongrel

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart
sudo apt-get install mongrel


I have never heard of Mongrel but this is was I have learned:

"Mongrel is a small library that provides a very fast HTTP 1.1 server for Ruby web applications. It is not particular to
any framework, and is intended to be just enough to get a web application running behind a more complete and robust web
server.

What makes Mongrel so fast is the careful use of an Ragel extension to provide fast, accurate HTTP 1.1 protocol parsing. "

Install Ruby Gems



http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz
tar xzvf rubygems-1.3.7.tgz
cd rubygems-1.3.7
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
sudo gem update --system



Install Rails


sudo gem install rails\




For an IDE/Text Editor there are a lot of options. I am familiar with Eclipse so I am using http://aptana.com/downloads/start

3/19/10

c++ Unix time to string / string to Unix time

Unix time to string:


double doubleUnixTime = 1268974800;
string stringUnixTime;
time_t unixTime = doubleUnixTime ;
doubleUnixTime = asctime(gmtime(&unixTime));


Datetime String to unix time:
Datetime string in this format: yyyy-mm-dd hh:mm:ss

string stringDateTime = "2010-01-11 11:42:58";
struct tm *tmp = new tm;

time_t rawtime;
time ( &rawtime );
tmp = localtime ( &rawtime );

string year = stringDateTime.substr(0,4);
string month = stringDateTime.substr(5,2);
string day = stringDateTime.substr(8,2);
string hour = stringDateTime.substr(11,2);
string min = stringDateTime.substr(14,2);
string sec = stringDateTime.substr(17,2);

if(year != "0000")
tmp->tm_year = atoi(year.c_str()) - 1900;
else
tmp->tm_year = 0;

if(month != "00")
tmp->tm_mon = atoi(month.c_str()) - 1;
else
tmp->tm_mon = 0;

if(day != "00")
tmp->tm_mday = atoi(day.c_str());
else
tmp->tm_mday = 0;

tmp->tm_hour = atoi(hour.c_str());

tmp->tm_min = atoi(min.c_str());

tmp->tm_sec = atoi(sec.c_str());

rawtime = mktime(tmp);

int intRawtime = rawtime;

3/16/10

C++ logging file

This code will create a log file named --today's date--.log and append log messages to the file. Each log message has the current date and time before each message. To compile in linux use this command:


g++ -o example.out logExample.cpp


Enjoy:


//logExample.h file
#ifndef LOGEXAMPLE_H
#define LOGEXAMPLE_H

#include "stdio.h"
#include "stdlib.h"
#include
#include
using std::string;


class log_example
{
public:

log_example();
~log_example();
void printLog(std::string message);
void printLogError(std::string message);

private:
void printDateTime();
FILE * pFile;


};

#endif




//logExample.cpp file
#include "log_example.h"

log_example::log_example()
{
//create log file
char outstr[200];
time_t t;
struct tm *tmp;
t = time(NULL);
tmp = localtime(&t);
strftime(outstr, sizeof(outstr), "%F", tmp);
std::string logFile(outstr);
logFile.append(".log");
pFile = fopen (logFile.c_str(),"a");

printLog( "log_example constructor called");

}

log_example::~log_example()
{
printLog( "log_example destructor called");
fclose (pFile);
}

void log_example::printDateTime()
{
char outstr[200];
time_t t;
struct tm *tmp;
t = time(NULL);
tmp = localtime(&t);
fputs( "\n" ,pFile);
strftime(outstr, sizeof(outstr), "%F", tmp);
fputs( outstr, pFile);
fputs( " " ,pFile);
strftime(outstr, sizeof(outstr), "%T", tmp);
fputs( outstr, pFile);
fputs( " " ,pFile);

}

void log_example::printLog(std::string message)
{
printDateTime();
fputs( message.c_str(),pFile);
fflush (pFile);

}

void log_example::printLogError(std::string message)
{
printDateTime();
fputs( "ERROR: ",pFile);
fputs( message.c_str(),pFile);
fflush (pFile);
}

11/17/09

Android Development: How to Add an Auto Complete View

This tutorial shows you how to add a Country text field with an auto complete feature. Note, the default threshhold for autocomplete is 2, so you will need to enter two characters before autocomplete will recognize your input. You can download the below project.

1. AutoCompleteTextView is an implementation of the EditText widget that will provide auto-complete suggestions as the user types. The suggestions are extracted from a collection of strings.
2. Create a project
3. Open Main.xml and add an AutoCompleteTextView
4. Build project to update the R.java file
5. Add this code in OnCreate:


ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
final AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)
findViewById(R.id.AutoCompleteTextView01);
autoCompleteTextView.setAdapter(adapter);


6. Add this code after OnCreate():


static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
"Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
"Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
"Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
"Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
"British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
"Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
"Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
"Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
"Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
"Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
"East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
"Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
"Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",
"French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
"Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
"Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
"Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",
"Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
"Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
"Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
"Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
"Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",
"Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",
"Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",
"Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
"Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",
"Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",
"Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",
"Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
"Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",
"Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",
"Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",
"The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
"Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
"Ukraine", "United Arab Emirates", "United Kingdom",
"United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",
"Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",
"Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
};