Friday, July 16, 2010

Get String

// strtok.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *SkipChars(char *buf, const char charToSkip)
{
    char *p = buf;
    while (p && (*p) && (*p == charToSkip)) p++;
    return p;
}


char *SkipString(char *buf, const char *str)
{
    char *p;
    p = strstr(buf, str);
    if (p) {
        p += strlen(str);
    }
    return p;
}


char *ParseLine(char *buf, char delim, char *token)
{
    char *start, *end;
    int len;

    if ((!buf) || (!token)) return NULL;
    start = SkipChars(buf, delim);
if (*start==0) return NULL;
    end=NULL;
    if (start) {
         end = start;
         while ((end) && (*end) && (*end != delim)) end++;
         len = end-start;
         strncpy(token, start, len);
         token[len] = 0;
    }
    start = SkipChars(end, delim);
    return start;
}

char *GetStringVal(char *buf, char *key, char *val)
{
char *p = SkipString(buf, key);
if (p) {
p = ParseLine(p, ' ', val);
}
return val;
}

const char desc[] = "This is just an example for skipstring function";
char res[] = "Mac             Type      Interface\n"
             "1111.2222.3333  S         fe0/0\n"
             "aabc.ddef.8754  S         fe0/0\n"
             "553a.4455.6789  D         fe0/1\n"
             "Total Macs 0\n";
char set[] = "mac-learning alarm 90 clear 60";


int _tmain(int argc, _TCHAR* argv[])
{
    int i,balance,len;
    char *p, *end;
    char token[100];
    char *saveptr;

#if 0
    /* Program entry point */
    if (argc) {
        for( i=0; i<argc; i++) {
            printf("argv[%d] = %s\n", i, argv[i]);
            p = SkipChars(argv[i], ' ');
            p = SkipChars(p, '"');
            while (p) {
                if (balance) balance=0;
                 end = SkipChars(p, '"');
                 if (end) {
                    balance = 1;
                    len = end-p;
                    strncpy(token, p, len);
                    token[len] = 0;
                    printf("token=%s\n", token);
                    p = end;
                    printf("p = %s\n", p);
                }
                if ((!balance) || (!len))
                    break;
            }
        }
    }
    printf("original string: %s\n", desc);
    p = SkipString((char*)&desc[0], "an example for ");
    if (p) {
        printf("After SkipString: %s\n", p);
    }
#endif
    p = SkipString((char *)res, "Interface\n");
    if (p) {
        p = strtok(p, "\n");
        //printf("original p=%s\n", p);
        while (p) {
            if (strstr(p, "Total Mac")) break;
            printf("\n\n--------------------------------------\n");
            #if 1
            end = ParseLine(p, ' ', token);
            printf("end=%s\n", end);
            i = 0;
            while (end){
                p = end;
                printf("Token%d=%s\n", i+1, token);
                end = ParseLine(p, ' ', token);
                i++;
            }
            #endif
            printf("%s\n", p);
            printf("--------------------------------------\n");
            p = strtok(NULL, "\n");
        }
    }
printf("alarm=%s\n", GetStringVal(set, "alarm", token));
printf("clear=%s\n", GetStringVal(set, "clear", token));
return 0;
}

Parse token

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *SkipChars(char *buf, const char charToSkip)
{
    char *p = buf;
    while (p && (*p == charToSkip)) p++;
    return p;
}


char *SkipString(char *buf, const char *str)
{
    char *p;
    p = strstr(buf, str);
    if (p) {
        p += strlen(str);
    }
    return p;
}


char *ParseLine(char *buf, char delim, char *token)
{
    char *start, *end;
    int len;

    if ((!buf) || (!token)) return NULL;
    start = SkipChars(buf, delim);
    if (!*start) return NULL;
    end=NULL;
    if (start) {
         end = start;
         while ((end) && (*end) && (*end != delim)) end++;
         len = end-start;
         strncpy(token, start, len);
         token[len] = 0;
    }
    return end;
}

const char desc[] = "This is just an example for skipstring function";
char res[] = "Mac             Type      Interface\n"
             "1111.2222.3333  S         fe0/0\n"
             "aabc.ddef.8754  S         fe0/0\n"
             "553a.4455.6789  D         fe0/1\n"
             "Total Macs 0\n";


void main( int argc, char **argv )
{
    int i,balance,len;
    char *p, *end;
    char token[100];
    char *saveptr;

    /* Program entry point */
    if (argc) {
        for( i=0; i<argc; i++) {
            printf("argv[%d] = %s\n", i, argv[i]);
            p = SkipChars(argv[i], ' ');
            p = SkipChars(p, '"');
            while (p) {
                if (balance) balance=0;
                 end = SkipChars(p, '"');
                 if (end) {
                    balance = 1;
                    len = end-p;
                    strncpy(token, p, len);
                    token[len] = 0;
                    printf("token=%s\n", token);
                    p = end;
                    //printf("p = %s\n", p);
                }
                if ((!balance) || (!len))
                    break;
            }
        }
    }
    printf("original string: %s\n", desc);
    p = SkipString((char*)&desc[0], "an example for ");
    if (p) {
        printf("After SkipString: %s\n", p);
    }

    p = SkipString((char *)res, "Interface\n");
    if (p) {
        p = strtok(p, "\n");
        //printf("original p=%s\n", p);
        while (p) {
            if (strstr(p, "Total Mac")) break;
            printf("\n\n--------------------------------------\n");
            end = ParseLine(p, ' ', token);
            //printf("end=%s\n", end);
            i = 0;
            while (end){
                p = end;
                //printf("end:**%s\n", p);
                printf("Token%d=%s,\t", i+1, token);
                end = ParseLine(p, ' ', token);
                i++;
            }
            printf("\n");
            printf("%s\n", p);
            printf("--------------------------------------\n");
            p = strtok(NULL, "\n");
        }
    }
}

Thursday, July 15, 2010

Trick to get (almost) free International call on AT&T cellphones

  1. Subscribe to Skype, set automatic withdrawal from your credit card
  2. Activate Skype To-Go service and select a local number from its list (US)
  3. Goto www.att.com/wireless and activate "A-List" feature/service (which is free).  Please remember that this service is only available if you have family plan and/or $60/month or more in monthly bill.
  4. Add the Skype To-Go number in A-List
  5. Now you're ready to make an international calls for a very cheap from your cellphone.  Just call the Skype To-Go number and follow voice instruction to dial in an international numbers.  You will only pay Skype calls (which is damned cheap, for example to call to Jakarta it's only 4c/minute).  That's it!

Thursday, June 10, 2010

CLIE UDEV RULES to support pilot device in Linux

Content of my udev rules to have /dev/pilot whenever my Sony CLIE NX70 PDE is syncing:

my-desktop:/dev/.udev/rules.d$ cat 10-local.rules

SUBSYSTEMS=="usb", ATTRS{product}=="Palm Handheld", KERNEL=="ttyUSB*", SYMLINK+="pilot"
BUS=="usb", SYSFS{product}=="Palm Handheld*", KERNEL=="ttyUSB[13579]", SYMLINK+="pilot"

On my Ubuntu 10.04 Karmic, actually there's a rule definition already written, but it is missing SYMLINK.  Below is my modified rule file.

vi /lib/udev/rules.d/40-libpisock9.rules  reveals:


# $Id: 60-libpisock.rules,v 1.4 2007/02/16 18:26:41 desrod Exp $
#
# udev rules file for pilot-link's libpisock library, enabled for libusb
#
SUBSYSTEMS!="usb", ACTION!="add", GOTO="libpisock_rules_end"

# Sony handheld devices
ATTRS{idVendor}=="054c", ATTRS{idProduct}=="0038", GROUP="dialout", MODE="0664"
ATTRS{idVendor}=="054c", ATTRS{idProduct}=="0066", GROUP="dialout", MODE="0664"
ATTRS{idVendor}=="054c", ATTRS{idProduct}=="0095", GROUP="dialout", MODE="0664"
ATTRS{idVendor}=="054c", ATTRS{idProduct}=="009a", GROUP="dialout", MODE="0664"
ATTRS{idVendor}=="054c", ATTRS{idProduct}=="00da", GROUP="dialout", MODE="0664",SYMLINK+="pilot"
ATTRS{idVendor}=="054c", ATTRS{idProduct}=="00e9", GROUP="dialout", MODE="0664"
ATTRS{idVendor}=="054c", ATTRS{idProduct}=="0144", GROUP="dialout", MODE="0664"
ATTRS{idVendor}=="054c", ATTRS{idProduct}=="0169", GROUP="dialout", MODE="0664"
....

Saturday, May 22, 2010

Small script to build Linux Kernel

Make sure we have busybox, otherwise install it:


sudo apt-get install busybox


And then save the following lines to o file and execute it:


VERSION=2.6.34-p4
make
make modules && sudo make modules_install
sudo make install
sudo mkinitramfs -o /boot/initrd.img-${VERSION} ${VERSION}

Thursday, May 6, 2010

How to calculate tax

The following code is to calculate tax amount we will pay for tax year 2010.





#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


typedef struct {
double min;
double max;
double taxPct;
} Bracket_t;


typedef enum {
single,
married_jointly,
married_separately,
head_of_household
} FilingStatus_t;




// married filing separately
Bracket_t BracketTable2008[] = {
{0.0, 8025.0, .10},
{8025.0,32550.0, .15},
{32550.0,65725.0, .25},
{65725.0,100150.0, .28},
{100150.0,178850.0, .33},
{178850.0,-1.0, .35},
{-1.0,-1.0,0.0}
};




Bracket_t BracketTable2010[] = {
{0.0, 16750.0, .10},
{16750.0,68000.0, .15},
{68000.0,137300.0, .25},
{137300.0,209250.0, .28},
{209250.0,373650.0, .33},
{373650.0,-1.0, .35},
{-1.0,-1.0,0.0}
};


double TaxCalc(double agi, Bracket_t *brYear)
{
int i;
double totalTax, tax, income;




if (!brYear)
return -0.0;
i = 0;
tax = 0.0;
totalTax = 0.0;
income = agi;
printf("agi = %9.2lf\n", income);
while (brYear[i].min > -1.0) {
if ((brYear[i].max > -1.0) && (income > brYear[i].max)) 
{
tax = (brYear[i].max - brYear[i].min) * brYear[i].taxPct;
}
else
{
tax = (income - brYear[i].min) * brYear[i].taxPct;
totalTax += tax;
printf("end of tax; bracket=%4.2lf, tax = %9.2lf\n", brYear[i].taxPct, tax);
break;
}
totalTax += tax;
printf("%d) tax = %9.2lf (%4.2lf), taxable income = %9.2lf\n", i, tax, brYear[i].taxPct, income);
i++;
}
printf("%d) taxable income = %9.2lf => tax = %9.2lf\n", i, income, totalTax);
return totalTax;




}




int main(const int argc, const char *argv[])
{
double agi, tax;
Bracket_t *tbl;


if (argc > 1)
{
printf("You entered %s\n", argv[1]);
agi = strtod(argv[1], NULL);
tbl = BracketTable2010;
}
else {
                // demo only for tax year 2008
tbl = BracketTable2008;
agi = 1e5;
}
tax = TaxCalc(agi, tbl);


printf("Final tax amount = %9.2lf (%4.2lf%%)\n", tax, tax/agi * 100.0);
}

Example:
$ ./tax 100000
You entered 100000
agi = 100000.00
0) tax =   1675.00 (0.10), taxable income = 100000.00
1) tax =   7687.50 (0.15), taxable income = 100000.00
end of tax; bracket=0.25, tax =   8000.00
2) taxable income = 100000.00 => tax =  17362.50
Final tax amount =  17362.50 (17.36%)

The income we enter is the AGI (Adjusted Gross Income), which is our total gross income minus all the deductions.