Execution order

Execution order of programs code is very much a vital thing to understand and where some fault finds will take a long while to figure out. Basically like in maths where there is a order of calculating, well in coding structures and also multitasking and multi-threading setups the execution order may be incorrect for lines of code.

Here is some examples, the first is when will a function be called and the later when post/pre incrementation will take place.

#include <iostream>
 
using namespace std;
 
int value =1;
 
int setvalue2()
{
	cout << "setting value"<<endl;
	value = 2;
	return value;
}
 
int returnvalue()
{
	cout << "renting value"<< endl;
	return value;	
}
 
int main()
{
	// depending on the order of execution the value may be
	/* setvalue2 called first
		(setvalue2 = 2 / returnvalue = 2) = 1
	   returnvalue called first
		(setvalue2 = 2 / returnvalue = 1) = 2
	*/
	cout << setvalue2() / returnvalue() << endl;
 
	int i;
	i = i++ - ++i;	// not sure what i will be because the pre/post increaments 
	i = 3, i++, i++; // i will equal 5 because in correct order.
}

Dynamic Casting

Dynamic casting, means that you can convert one object into another that is off the same type. For example, if you had a base class called Shape and a inherited class called Rectangle then you are able to convert a Shape object into a Rectangle.

Rectangle *rec = dynamic_cast<Shape *>(shapeobject);

sort of thing, there has to be a virtual function within the base class otherwise the compiler will complain, but apart from that that is about it.

Dynamic casting allows for NULL returns which is the best thing, because you can test to see if the casting actually worked and not to do anything silly on a NULL object which would crash the program.

Pointer casting uses the sytnax

<type> *p_subclass = dynamic_cast<<type> *>( p_obj );

Reference will not throw an error/expcetion so will need to check std::bad_cast< typeinfo header >, here is the syntax

<type> subclass = dynamic_cast<<type> &>( ref_obj );

Hopefully this will make more sense for how and why it works.

#include <iostream>
 
using namespace std;
 
class classA 
{
	public	:
		int x;		// should be private
		char y;		// should be private
 
		classA();
 
 		virtual ~classA() {}; 	// need to have a virtual function for dynamic casting
};
 
// basic classA constructor
classA::classA()
{ 
	cout << "classA" << endl; 
	x = 1; 
	y = 'a';
}
 
 
class classB : public classA
{	
	public :
		int b;		// should be private
 
		classB();
 
		~classB() {};	// complete the virtual
};
 
// basic classB constructor
classB::classB()
{
	cout << "classB" << endl;
	x = 2;
	y = 'b';
}
 
int main()
{
	classA newa;	// classA obj
	cout << "class A constructed" << endl;
	classB newb;	// classB obj
	cout << "class B constructed" << endl;
 
	cout << "NewA X " << newa.x << endl;
	cout << "NewB X " << newb.x << endl;
 
	// point a classB to a already created classB object
	classB *normalB = &newb;
	// dynamic_cast a normalB object (newb) to another classA object
	classA *dynA = dynamic_cast<classB *>(normalB);	
	cout << "dynamic A X " << dynA->x << endl;
 
	// does not work, because you cannot convert classA into a classB, but if classA was pointing to a classB type.
	classA *normalA = &newa;
	classB *dynB = dynamic_cast<classB *>(normalA);
	if (dynB)	// above produces a 0 because invalid.
	{
		cout << "dynamic B X " << dynB->x << endl;
		cout << "dynamic B X " << dynB->b << endl;
	}
 
	// this does work because it is converting from a pointer of classB type, which was "sitting" in a classA container
	// and then is converted back to a classB
	classA *normalA2 = &newb;
	classB *dynB2 = dynamic_cast<classB *>(normalA2);
	if (dynB2)	// above produces a 0 because invalid.
	{
		cout << "dynamic A2X " << normalA2->x << endl;	// output is 2 because it was a classB constructed
		cout << "dynamic B X " << dynB2->x << endl;
		cout << "dynamic B X " << dynB2->b << endl;
	}
 
}

Hello World

This is a tutorial that will create the text “Hello World” within the current document of a OpenOffice Writer document.

To be able to get into the editor, if you click Tools->Macros->Organize Macros->OpenOffice Basic and then select Edit on the module1 within MyMacros->Standard.

The code required builds up an object to document and also a network object server that communicates with the document frame (createUnoService) the objects within the parameters e.g com.sun.star.frame.DispatchHelper are services that enable communication with dispatch object which in-turns ‘talks’ to the document object.

The code

sub helloworld
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
rem ----------------------------------------------------------------------
rem Create a universe network object service with the frame.
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
 
rem ----------------------------------------------------------------------
rem The type is a text string and the value of the string is Hello World
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Text"
args1(0).Value = "Hello World"
 
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args1())
 
rem ----------------------------------------------------------------------
rem Create a new paragraph
dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())
end sub

if you open up a new document and then goto the macro editor (click Tools->Macros->Organize Macros->OpenOffice Basic and then select Edit on the module1 within MyMacros->Standard) insert the code into the editor. To run the code, click on the Tools->Macro->Run Macro, then select the Helloworld subroutine from the MyMacros->Standard->Module1.

That is it, and the start of tutorials for OpenOffice being 🙂

HTML + CSS

An Cascading Style Sheet (CSS) is basically styles of a page within a block of text. The cascading part means that the last value for a set style will be the value taken, e.g. if at the top of a style sheet there is a value of black for the back ground colour and then white is below that, the white value will take precedence over the black.

Here is a example of styling within the tag body.

<html>
       <title>Background colour</title>
       <body bgcolor="#BCBCBC">
              hi world
       </body>
</html

And this is an example with CSS within the html code.

<html>
       <title>Background colour with css</title>
       <style type="text/css">
              body 
              {
                     background : #DCDCDC;       
              }
       </style>
       <body>
              Hi World
       </body>
</html>

There are many styles options within the html styling list, hopefully shall cover most of these in the future.

Hello World

The (X)HTML uses tags to describe the area of the page, e.g. title tag means the title of the page. The below code displays “Hello World” in the page and the title of the page is “Hello World Tutorial”.

Cut the code from here

<html>
<title>Hello World Tutorial</title>
<body>
Hello World
</body>
</html>

if you save that as helloworld.html, and then open up the saved page with your browser of choice.

Grab data from tables

Alter the variable @tableName to the table and also the @tableWhere for the where condition, I found that if you use the standard sql dumps that you was taking allot of other crap with you as well.

The code

DECLARE @colName VARCHAR(100), @colSql VARCHAR(500), @colSQLInsert VARCHAR(500), @TYPE INT, @auto INT, @tableName VARCHAR(50), @tableWhere VARCHAR(500);
SET @tableName = 'tablename';
SET @tableWhere = 'the where condition';
 
-- grab the table column names
DECLARE tablecol cursor FOR
SELECT name, typestat, autoval FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = @tableName);
 
--The @auto is the auto generated fields e.g. primary key. </b>
SET @colSql = '';
SET @colSQLInsert = '';
OPEN tablecol;
fetch tablecol INTO @colName, @TYPE, @auto;
while (@@fetch_status = 0)
BEGIN
       IF (@auto IS NULL)
       BEGIN
              IF (charindex('.',@colName) > 0) SET @colName = '['+@colName+']';
              SET @colSQL = @colName + ',' +@colSQL;
              IF (@TYPE = 2)
                     SET @colSQLInsert = '''''''+isnull(' + @colName + ',0)+'''''',' + @colSQLInsert;
              ELSE
                     SET @colSQLInsert = '''+str(isnull(' + @colName + ',''''))+'',' + @colSQLInsert;
       END
       fetch NEXT FROM tablecol INTO @colName, @TYPE, @auto;
END
close tablecol;
deallocate tablecol;
-- to build the sql statement, since it stops at 255 charactes split the outputs</b>
SELECT 'select (''insert into '+@tableName+' (';
DECLARE @loopingVal INT;
SET @loopingVal =0;
while (len(@colSQL) > @loopingVal)
BEGIN
       SELECT SUBSTRING(@colSQL, @loopingVal, 255);
       SET @loopingVal = @loopingVal + 255;
END
SELECT ') values (';
 
SET @loopingVal =0;
while (len(@colSQLInsert) > @loopingVal)
BEGIN
       SELECT SUBSTRING(@colSQLInsert, @loopingVal, 255);
       SET @loopingVal = @loopingVal + 255;
END
SELECT ')'') from '+@tableName + ' ' + @tableWhere;

The output will be x lines, and if you just copy them into a single line and this will display (once executed) the insert lines.

If anyone has any better methods, please comment 🙂

Hello World!!

Well, might as well do a Hello world in all of the languages. Once you have a database installed, e.g. MySQL, start up the database and goto the query line so that you are able to *talk* to the database. Type in

SELECT 'Hello World';

This should show Hello World in the output of the database.

I shall have to do Hello World in all languages now 🙂