Parsing XML in Run BASIC

One of the important feature that a Web 2.0 language needs is an XML parser. Run BASIC now has one built in. The XMLPARSER statement parses an XML string and returns an XML accessor object with a bunch of handy built-in methods for making your way through an XML document.
Here is a simple example of what that sort of code looks like:

a$ = "<program name=""myprog"" author=""Carl Gundel""/>"
xmlparser #parser, a$
print #parser key$()
for x = 1 to #parser attribCount()
key$ = #parser attribKey$(x)
print key$; ", ";
print #parser attribValue$(x)
next x

This short program produces:

program
name, myprog
author, Carl Gundel

And here is a short program which will display the tag names and contents of an artibrarily nested XML document:

xmlparser #doc, s$
print #doc key$()
call displayElements #doc
end

sub displayElements #xmlDoc
count = #xmlDoc elementCount()
for x = 1 to count
#elem = #xmlDoc #element(x)
print "Key: "; #elem key$();
value$ = #elem value$()
if value$ <> "" then
print " Value: "; value$
end if
print
call displayElements #elem
next x
end sub


Do-it-yourself programming

A friend of mine pointed me at this article "Do-It-Yourself Software" in the Wall Street Journal.

http://online.wsj.com/public/article/SB119023041951932741.html

Run BASIC and Liberty BASIC are both aimed at this market, and in fact this is the traditional niche of BASIC language products.

More on modularity

Run BASIC gives you the ability to create web pages that are component based. You define your own components and the ease at which you can plug these things together comes essentially for free. Here is a really simple example that I posted in our beta testing forum:

[masterPage]
cls
html "Program manager"
link #wiki, "Wiki", [runTheWiki]
print " ";
link #multi, "Multicounter", [runTheMulticounter]
print
if launchFlag then render #subProg
wait

[runTheWiki]
run "runWiki", #subProg
launchFlag = 1
goto [masterPage]

[runTheMulticounter]
run "multicounter", #subProg
launchFlag = 1
goto [masterPage]

Here is a version that doesn't use any GOTOs:

global launchFlag
call displayMasterPage
wait

sub displayMasterPage
cls
html "Program manager"
link #wiki, "Wiki", runSubprogram
#wiki setkey("runWiki")
print " ";
link #multi, "Multicounter", runSubprogram
#multi setkey("multicounter")
print
if launchFlag then render #subProg
end sub

sub runSubprogram linkKey$
run linkKey$, #subProg
launchFlag = 1
call displayMasterPage
end sub

So what does this do? It creates a simple web page with a title and two links. Click on the wiki link and the wiki program becomes part of the web page. Click on the multicounter link and the multicounter replaces the wiki part of the page. You can switch back and forth between the wiki and the multicounter at will with just the click of a mouse. What's even more interesting is that the multicounter is already a modular program, so you get three levels of modularity but you aren't limited to that.

So for programmers who like to put all their code in one file, there's nothing to prevent that. But people who like modules can have a field day.

Six sixes in one over in Twenty20 Cricket - World record

Six sixes in one over in Twenty20 (Twenty Twenty, T20) Cricket? Oh! World record!!! It was an amazing over; all the balls went over the rope like rockets. The Start was Yuvraj Singh from India, and the poor bowler was Stuart Broad (England). This happened on 2007-09-19, in Twenty20 world up match. Yuvraj did this in the 19th over.

Unsurprisingly, he scored 50 runs in 12 deliveries, creating another world record. And think for a minute? He scored 50 with 3x4 and 6x6, which adds upto 48 runs in 9 balls. This world record will not be broken for a longer time and will stay than any other record?

With this six sixes, Yuvraj joined the club of Six Sixers as the 4th person. Before him; West Indies great Sir Garfield Sobers and India's Ravi Shastri joined there while Herschelle Gibbs joined this club this year in March. Well done Yuvraj.

[Java Tips] Add Array into a List and convert a List into an Array

With Java, putting contents of an Array into a new List object or adding into an existing List object can be achieved easily using a for() loop; just by going through each and every element in array and adding to List one at a time. But that is not needed. Java has provided a method to achieve that easily; just with one method call. Following code snippet shows how.

//import java.util.List;
//import java.util.Arrays;
String[] array = {"one", "two", "three"};
List newListObject = Arrays.asList(array);

//adding to existing List
String[] newArray = {"four", "five"};

List all = new ArrayList();
all.addAll(newListObject);
all.addAll(Arrays.asList(newArray));


Also creating a new Array object using an existing List object can be done using another for() loop; by creating a new Array object with a size matching to list size and adding each on at a time. But for this requirement, there are a set of methods.

List list = new ArrayList();
list.add("one");
list.add("two");

Object[] array1 = list.toArray(); //1
String[] array2 = (String[])list.toArray(new String[0]); //2

String[] array3 = new String[2];
list.toArray(array3); //3


With Line #1, returned array is of Object type, while #2 returns a new array object of String type.
Line #3 uses the same method used in #2, but in this case we have provided an array object with the same size as the list. Because of that in line #3, the provided array object is populated with list elements.

QuickTime security issue fixed with Firefox 2.0.0.7 new security release

Firefox 2.0.0.7 has released a new security release again. As addicted users of FF, we are really happy how Firefox progresses. When ever some issues are fixed, they provide us with a new release. Also user does not need to think twice in installing the new release as it does not add more issues to your existing installation. When Internet Explorer (IE) 7 came out users had issues; also still we have so many issues with IE7, but not with Firefox.

With this release they fixed the bug related to QuickTime; "Code execution via QuickTime Media-link files".
QuickTime Media-Link files contain a qtnext attribute that could be used on Windows systems to launch the default browser with arbitrary command-line options. When the default browser is Firefox 2.0.0.6 or earlier use of the -chrome option allowed a remote attacker to run script commands with the full privileges of the user. This could be used to install malware, steal local data, or otherwise corrupt the victim's computer. Read more

We really appreciate quick response to security issues like that.

boost::any

boost::any is a strong concept and a much better replacement to void* to hold any type of data. You can make heterogenous containers using it as well. Let us see how it works in a very simplified way. The idea is to have a template class that can wrap all types and a value associated with that type. Something like this:

template<typename T>
class HoldData
{
    T t;
};

And then having a base class from which this wrapper would derive, so the above becomes adding a constructor that needs the type to be stored in it to be copy constructible:

class BaseHolder
{
    public:
        virtual ~BaseHolder(){}
};

template<typename T>
class HoldData : public BaseHolder
{
    public:
        HoldData(const T& t_) : t(t_){}
    private:
        T t;
};

Now, you would have a class, name it Variant that will take inputs of all types and then has a pointer to this wrapper's base type. So, now you have (including above classes):

class BaseHolder
{
    public:
        virtual ~BaseHolder(){}
};

template<typename T>
class HoldData : public BaseHolder
{
    public:
        HoldData(const T& t_) : t(t_){}
    private:
        T t;
};

class Variant
{
    public:
        template<typename T>
        Variant(const T& t) : data(new HoldData<T>(t)){}
        ~Variant(){delete data;}
    private:
        BaseHolder* data;
};

You construct the corresponding type's wrapper objects and save their pointer into the another class that you call variant, that can hold and help retrieve any data type and does not lose the respective type information. That is actually what boost::any does. Take a look at the code here - boost::any code.

The documentation on it can be found here - boost::any documentation.

svnadmin create a new empty project repository in subversion (svn) in Linux

If you have installed subversion (used for version control) and looking for creating a repository inside that, you are at the right place. Command to create new project repositories inside subversion is svnadmin create.

# svnadmin create repoPath
- this will create an empty repository.
- repoPath has to be a path to a folder, if that folder does not exists; new folder will be created.

Consider following examples.
# svnadmin create /usr/local/svn/clients/MyProject
or
# svnadmin create .

First command will create a repository inside "/usr/local/svn/clients/MyProject" while the second command creates a repository in your current directory.

After creating the repository, you must alter access controls. For that open conf/svnserver.conf found inside newly created repository folder.
Common values to alter are;
anon-access
- access control for non authenticated users
- better to set it to none (anon-access = none)
auth-access
- access control for authenticated users
- will need set it to "read" or "write" (auth-access = read)

Call javascript in body tag on different events

Javascript can be called inside body tag of a web page. Mainly Javascript functions are called on events like Loading a web page, Clicking on a button, Moving mouse, Focusing on an element, etc. For each of these events, there are defined methods and they are fired at that particular event. For example; onclick() is triggered on a mouse click event while onmouseover() is called when mouse moves over an element. But you can call Javascript functions even without any event.

Available events for body tag can be listed as follows.

ONCLICK : mouse button clicked
ONDBLCLICK : mouse button double-clicked

ONMOUSEDOWN : mouse button is pressed
ONMOUSEOVER : mouse moved onto an element
ONMOUSEMOVE : mouse moved over an element
ONMOUSEOUT : mouse moved out of an element
ONMOUSEUP : mouse button is released

ONKEYPRESS : key pressed and released
ONKEYDOWN : key pressed
ONKEYUP : key released

There are two special events that are specific to body tag. Those are;

ONLOAD : document loaded completely
ONUNLOAD : document unloaded

Calling a Javascript method
At any of the above events, you can call any javascript function from your body tag. A Javascript function named testAlert() can be called as below.
<body onload="testAlert();">

To call move than one Javascript function, you have to write the function named separating by semi-colons ( ; ) as below.
<body onclick="validate(); calculate(); submit();">

Which software (geek) monkey worth the most?

An interesting conversation!!!

A tourist walked into a pet shop and was looking at the animals on display. While he was there, another customer walked in and said to the shopkeeper, "I'll have a C monkey please." The shopkeeper nodded, went over to a cage at the side of the shop and took out a monkey. He fit a collar and leash, handed it to the customer, saying, that'll be $5000." The customer paid and walked out with his monkey. Startled, the tourist went over to the shopkeeper and said, "That was a very expensive monkey. Why did it cost so much?" The shopkeeper answered, "Ah, that monkey can program in C very fast, tight code, no bugs, well worth the money."

The tourist looked at the monkey in another cage. "That one's even more expensive! $10,000!What does it do?". "Oh, that one's a C++ monkey; it can manage object- oriented programming, Visual C++, even some Java. All the really useful stuff," said the shopkeeper. The tourist looked around for a little longer and saw a third monkey in a cage of its own. The price tag around its neck read $50,000. He gasped to the shopkeeper, "That one costs more than all the other put together! What on earth does it do?"

The shopkeeper replied, "Well, I haven't actually seen it doing anything, but the other monkeys call him the project manager."


Is this true? Can we work on huge software projects without project managers? Who would do estimations and resource utilization? To be frank I can not agree 100% with this, but there are so many people who survive being a non-working project manager. They usually come to work early, but does nothing for the sake of his team members. Some of them even have no idea about the responsibilities of their role in a software development process. Which type of a project manager are you?

Run BASIC enters beta testing

Well, it's been quiet here for a couple of months but now that the summer activities are over things have begun to pick up steam. In particular we started beta testing Run BASIC Personal Server a few weeks ago. We are still looking for a few more people to help test.

Run BASIC Personal Server is an all in one web app server, BASIC scripting language, database engine and more. It offers an extremely easy way to get into web application development. When I say extremely easy I am not exaggerating. We are talking "a child could do it" easy web programming.

When I asked my testers how they would describe Run BASIC, here is what some of them said:

" - - Run BASIC provides a complete alternative to the complex development languages that have evolved to script web content. Run BASIC wrests control back to you, allowing BASIC language scripting of web content. Create web pages in an easy to use project development environment and publish on the web at the click of a mouse."

" - - If you've ever used one of the classic BASIC interpreters, then you already know most of what you need to build dynamic websites using Run Basic."

" - - Run BASIC moves desktop programming out onto the internet. The screen displays, forms, interactions, graphics, and data-handling processes you create with clear, understandable BASIC programs suddenly become web applications, usable by anyone, on any platform -- Windows, Mac, Linux -- on any computer with browser access to the internet. With Run BASIC, you can write your program from anywhere, on any computer, and run it everywhere, on every computer."

If you are interested in Run BASIC and feel you have enough time to spend at least a few hours testing it out, please send an email to me at carlg@libertybasic.com and explain why you would like to be a beta tester.

We will accept only a certain number of applicants and I'll post a note here when we have enough.

Thanks!

-Carl Gundel

Some Operations on Matrix

A few days back someone asked me a question via email which I thought might
be useful to others too. So I’m listing that question along with its answer
below.


Q. I want to write a program such that users enter the value of matrix
and each operation (listed below) is performed by functions. I want to use switch
structure to call the functions.


1. Rotate the matrix around the diagonal.


Example:


   1 2 3 ---> 1 4 7
4 5 6 2 5 8
7 8 9 3 6 9

2. Rotate the matrix around the middle row.


Example:


   1 2 3 ---> 7 8 9
4 5 6 4 5 6
7 8 9 1 2 3

3. Rotate the matrix around the middle column.


Example:


   1 2 3 ---> 3 2 1
4 5 6 6 5 4
7 8 9 9 8 7

4. Set the upper triangle to zero.


Example:


   1 2 3 ---> 1 0 0
4 5 6 4 5 0
7 8 9 7 8 9

Ans. The following program does it. Please note that the matrix
is declared as global so as to reduce complications in the program. Better way
should have been to pass the matrix (local) to the functions from main().



// Program that does some Rotations
// about certain axes in two
// dimensional arrays
#include <iostream.h>

// change this to hold more
// values in the array
#define MAX 3

// it is declared to be global
// so that every function can access it
int ar[MAX][MAX];

// function prototypes
void EnterVal();
void rDiagonal();
void rMidRow();
void rMidCol();
void sUpperZero();
void Show();

void main()
{
int ch;

// loop until 'quit' is not selected
while(ch!=6)
{
cout<<"1> Enter Values\n";
cout<<"2> Rotate around Diagonal\n";
cout<<"3> Rotate around the Middle row\n";
cout<<"4> Rotate around the Middle column\n";
cout<<"5> Set the Upper triangle to zero\n";
cout<<"6> Quit\n\n";

cin>>ch;

// do as per choice
switch(ch)
{
case 1:
EnterVal();
break;

case 2:
rDiagonal();
Show();
break;

case 3:
rMidRow();
Show();
break;

case 4:
rMidCol();
Show();
break;

case 5:
sUpperZero();
Show();
break;
}
}
}

// --------------------
// Function Definitions
void EnterVal()
{
int i,j;

cout<<"Enter Values\n";

for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
cin>>ar[i][j];
}

void rDiagonal()
{
int temp,i,j;

for(i=0;i<MAX;i++)
for(j=i;j<MAX;j++)
{
temp=ar[i][j];
ar[i][j]=ar[j][i];
ar[j][i]=temp;
}
}

void rMidRow()
{
int temp,i,j;

for(i=0;i<(MAX-1)/2;i++)
for(j=0;j<MAX;j++)
{
temp=ar[(MAX-1)-i][j];
ar[(MAX-1)-i][j]=ar[i][j];
ar[i][j]=temp;
}
}

void rMidCol()
{
int temp,i,j;

for(i=0;i<MAX;i++)
for(j=0;j<(MAX-1)/2;j++)
{
temp=ar[i][(MAX-1)-j];
ar[i][(MAX-1)-j]=ar[i][j];
ar[i][j]=temp;
}
}

void sUpperZero()
{
int i,j;

for(i=0;i<(MAX+1)/2;i++)
for(j=i;j<MAX-i;j++)
ar[i][j]=0;
}

void Show()
{
int i,j;

for(i=0;i<MAX;i++)
{
for(j=0;j<MAX;j++)
cout<<ar[i][j]<<" ";

cout<<endl;
}
cout<<"\n\n";
}


Related Articles:


Yahoo! Photos closing on different dates for different users



Today I got a mail from Yahoo informing that Yahoo Photo is shutting down. But why? As you may guess they are going to stick only to Flickr, which is one of the best solutions if someone wishes to share photos. It will be closed on 2007-10-18 for some of us while the date differs from one user to another. But anyway make sure you transfer all your photos in Yahoo Photo is moved to Flickr or download them to some other place as you will loose them for ever. Only 41 days left for me.

File Splitting Free Software - TheFileSplitter

TheFileSplitter, as name suggests it is a file splitting software. When thinking about file splitting, the first option that comes to our minds is WinZip which is a commercial one (costs $29.95 per standard single user license). But TheFileSplitter is completely free and no need of installing; just extract and use. It is only 71kB in size.

Features
1. Completely free.
2. Installing is not mandatory, just extract and use.
3. User can specify any size for file pieces; starting from a smaller value as 1 byte.
4. This software is not needed at the event of recreating the original file; what??? yes, not needed. It creates a small .exe file (above 40kB) file which can reproduce the original file using the pieces of that original file. So receiver does not have to install or download any splitter software; or even no need of knowing how splitting is done.

Performance Analysis
Software is analyzed by us using different file sizes and different piece sizes. For the same piece size of 500kB; three files are tested and time comparison is shown below. It clearly shows that the file size does matter dramatically on the speed of splitting.

A 1GB file is split for different piece sizes from 500kB to 500MB. Time spend comparison is graphed below which mainly shows that the speed has not much connection into the piece size.


Download here.
Note: This software is only for Windows platform.

Overloading [] Operator II

In the previous article Overloading
[] Operator
, we overloaded the [] operator in a class to access data
within the class by indexing method.


The operator [] function was defined as below:


  int myclass::operator[](int index)
{
// if not out of bound
if(index<num)
return a[index];
}

As you can see, the above operator
function
is returning values, hence it could only be used on the right
hand side of a statement. It’s a limitation!


You very well know that a statement like below is very common with respect
to arrays:


a[1]=10;


But as I said, the way we overloaded the [] operator, statement like the one
above is not possible. The good news is, it is very easy to achieve this.


For this we need to overload the [] operator like this:


  int &myclass::operator[](int index)
{
// if not out of bound
if(index<num)
return a[index];
}

By returning a reference
to the particular element, it is possible to use the index expression on the
left hand side of the statement too.


The following program illustrates this:



// Example Program illustrating
// the overloading of [] operator
// ----
// now the index expression can be
// used on the left side too
#include <iostream.h>

class myclass
{
// stores the number of element
int num;
// stores the elements
int a[10];

public:
myclass(int num);

int &operator[](int);
};

// takes the number of element
// to be entered.(<=10)
myclass::myclass(int n)
{
num=n;
for(int i=0;i<num;i++)
{
cout<<"Enter value for element "<<i+1<<":";
cin>>a[i];
}
}

// returns a reference
int &myclass::operator[](int index)
{
// if not out of bound
if(index<num)
return a[index];
}

void main()
{
myclass a(2);

cout<<a[0]<<endl;
cout<<a[1]<<endl;

// indexing expression on the
// left-hand side

a[1]=21;
cout<<a[1];
}


Related Articles:


Check out this stream