Saturday, July 17, 2010

Array of Strings

// 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 *GetToken(char *buf, char *tok, int toksize)
{
char *start;
int i = 0;

start = SkipChars(buf, ' ');
while (start && (*start != 0) && (*start != ' ') && (i < toksize)) {
tok[i++] = *start;
start++;
}
tok[i] = 0;
return start;
}


int ParseLine(char *buf, char delim, char **toks, int tokSize, int n)
{
    char *start, *item;
    int i = 0;

    if ((!buf) || (!toks)) return NULL;
    start = SkipChars(buf, delim);
if (*start==0) return NULL;
while (start && (*start) && (i<n)) {
  /* convert to first-major pointer */
item = ((char *)&toks[0] + tokSize*i);
start = GetToken(start, (char *)item, tokSize);
i++;
}
    return i;
}

char *GetStringVal(char *buf, char *key, char *val, int valSize)
{
char *p = SkipString(buf, key);
if (p) {
p = GetToken(p, val, valSize);
}
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";


#define TOKEN_LEN 80
int _tmain(int argc, _TCHAR* argv[])
{
    int i,balance,len;
    char *p, *q, **pp;
    char token[100];
char tokens[3][TOKEN_LEN] = {"saya", "makan", "nasi"};

#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");
printf("address of tokens[0][0] = %X\n", &tokens[0][0]);
//printf("q = %X\n", q);
            len = ParseLine(p, ' ', (char **)&tokens, TOKEN_LEN, 3);
for(i=0; i<len; i++) {
printf("token%d = %s\n", i+1, tokens[i]);
}
            printf("%s\n", p);
            printf("--------------------------------------\n");
            p = strtok(NULL, "\n");
        }
    }
printf("alarm=%s\n", GetStringVal(set, "alarm", token, sizeof(token)));
printf("clear=%s\n", GetStringVal(set, "clear", token, sizeof(token)));
return 0;
}

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!