Library of your own – object file.

Sometimes you want to have a library of your functions within one object file that you can use for other applications without having to re-compile and import each time to each project.

Well, if you create a object of the list of functions because a object file is the intermediate between code and linking (to create the executable file), but with this object file you will need to have created a header file (.h) that will allow the projects to know what functions are within the object file and how to call them. So to start with here is the header file (I have called it libraryHelloWorld.h)

#include <iostream>
 
using namespace std;
 
void SayHelloWorld();
void SayWord(std::string st);

It just defines the two functions that I have written, and here are the implementation of the two functions above.

#include "libraryHelloWorld.h"
#include <iostream>
 
using namespace std;
 
void SayHelloWorld()
{
    cout << "Hello World" << endl;
}
 
void SayWord(string st)
{
    cout << "Line is \"" << st << "\"" << endl;
}

if you save that as libraryHelloWorld.cpp, if you notice that at the top I am including the header file to this cpp file because the header files normally have a class definition inside them and also struct’s etc, which the functions within the cpp file may require.

To compile up, to create the object file you just need to do

g++ libraryHelloWorld.cpp -c

which basically means (-c) just compile and do not link, this will create a libraryHelloWorld.o file (the object file).

To make use of this object file, you just need to add in the header file to your project and then call the functions as though you have re-written them within your new project, like so.

#include "libraryHelloWorld.h"
 
int main()
{
  SayHelloWorld();
  SayWord("Genux is great");
  return 0;
}

Then the main part, which is including the object file created before into the linking part of the computation of this program. (save the above as callHelloWorld.cpp)

g++ callHelloWorld.cpp libraryHelloWorld.o -o callHelloWorld

the above includes the object file, libraryHelloWorld and after the linking part of the compilers job, the output file name (-o) will be callHelloWorld.

This just saves allot of time when you are coding, try to keep different projects having similar libraries that you know and trust to work.

changing the hostname

To change the hostname of a linux distribution you can either change the hostname on-the-fly, but it does not restore it after a reboot. To change the hostname on-the-fly you can do

echo "your new hostname" > /proc/sys/kernel/hostname

on a ubuntu/debian system to alter the hostname after a reboot you can alter the

/etc/hostname

file, if you cannot find the hostname file there, you can search for the file within the /etc directory by

grep -R "your hostname" /etc

it will display a few files, but you should find out where your hostname is set, normally either within /etc/sysconfig or /etc/network directories.

Blob to store data in mysql database

To store data within a blob in a database can be a good thing at times because then you can just copy the database from one place to another and use the access rights on the database to restrict access to the “files” within the database.

There could be a few reasons why you want to store the data within a blob in the database, but here how the basics would work.

To start with you have to create a database and a table to store the data/file within the blob, of course if you have already created the database and/or the tables then alter as you think, but here is the basics.

  CREATE DATABASE phptestplace;
  CREATE TABLE storingData (id INT NOT NULL auto_incremenet, lblob BLOB, PRIMARY KEY (id));

And then within a php file you can access the database and a file.

  $link = mysql_connect('localhost', 'username', 'userpassword');
  if (!$link) {
      die('Could not connect: ' . mysql_error());
  }
// alter to your database name
  mysql_select_db("phptestplace", $link);

and now access the file and read in file

  $filename = "filetoload.txt";
  $handle = fopen($filename, "r");
  $contents = fread($handle, filesize($filename));
  fclose($handle);
 
// to insert into the database you need to add in the slashes for characters like / \ etc.
  $contents = addslashes($contents);

to insert into the blob you just, change the table and table name to what may have called it.

  $sqlquery = "insert into storingData(largeblob) values ('$contents')";
  mysql_query($sqlquery) or die("ERROR");*/

to get the data back (I am calling back the last inserted value into the table)

// get the data into a result variable
  $return = mysql_query ("select lblob from storingData where id = (select max(id) from storingData)") or die("LLL");
// get the contents of the blob from the return variable (it returns a array of data) and the list takes out the data from a array each part at time.
  list($newcontents) = mysql_fetch_array($return);

and then store the data from the database pull into a file, I have called it newfile.txt, but it is up to you.

  $fp = fopen('NEWFILE.txt', 'w');
  fwrite($fp, $newcontents);
  fclose($fp);

Of course can do it via a web page, using a HTML FORM enctype=”multipart/form-data” within the form tag otherwise it may not work.

dynamic_cast – cpp

The casting of variables, is when you change one type of variable to another, for example to change a integer value to a double value. But when it comes to classes, casting is allot more fun. Because since a casting variable has to be compatible with the return casting type, the base class will need to be polymorphic for this to work (virtual functions within the base class).

The basis of a dynamic_cast syntax is as follows.

return variable = dynamic_cast<return type>(casting value)

To try and demonstrate how dynamic_cast(ing) works here is a example code broken down into chunks. Here is the base class, that has the virtual int returnX() method.

class baseClassDoesWork
{
  protected : 
     int x;
  public : 
     baseClassDoesWork()  { x = 55; };
     virtual int returnX() { cout << "Base class" << endl; return x;}
     void setX(int newXValue) { x = newXValue;}
};

and here is the sub class that will inherit the base class and re-do the virtual method

class subBaseClassDoesWork : public baseClassDoesWork
{
  public : 
      subBaseClassDoesWork() : baseClassDoesWork() {};
      int returnX() { cout << "Subclass" << endl; return x;}
};

here, there is no real difference between the two functions, because the demo is about how dynamic casting works, but I have added in some debugging code to say what class is being called.

Since a dynamic_cast will cast the virtual functions, then the polymorphism effects the casting and newer virtual function that is in the subclass still will call called. But the casting to the class other functions will not “follow” the cast. In the example below first we are creating a subclass and then creating a base class from a dynamic_cast of the subclass (as stated before the base class casting will still include the virtual function from the subclass), so that if you want to convert to another class that also inherited from the base class but not the subclass then you can now convert to the new class.

Here is the code.

#include <iostream>
 
using namespace std;
 
class baseClassDoesWork
{
  protected : 
     int x;
  public : 
     baseClassDoesWork()  { x = 55; };
     virtual int returnX() { cout << "Base class" << endl; return x;}
     void setX(int newXValue) { x = newXValue;}
};
 
class subBaseClassDoesWork : public baseClassDoesWork
{
  public : 
      subBaseClassDoesWork() : baseClassDoesWork() {};
      int returnX() { cout << "Subclass" << endl; return x;}
};
 
int main()
{
    // this one does work because of the virtual method in the base class.
    // but we can create a sub class of the base class first.
    subBaseClassDoesWork *subBase = new subBaseClassDoesWork();
    // convert it to the base class
    baseClassDoesWork *baseDoes = dynamic_cast<baseClassDoesWork*>(subBase);
 
    // check to see if the casting worked e.g. not equal to 0
    if (baseDoes == 0)
      cout << "Bad casting" << endl;
    else
      cout << baseDoes->returnX() << endl;
 
    // re-set the value of X
    baseDoes->setX(30);
 
    // convert it back again to the sub class and things are still great :).
    subBaseClassDoesWork *subBase2 = dynamic_cast<subBaseClassDoesWork*>(baseDoes);
    if (subBase2 == 0)
      cout << "Bad casting" << endl;
    else
      cout << subBase2->returnX() << endl;
 
    return 0;
}

The output would be

Subclass
55
Subclass
30

Where as this would not work because there is no virtual functions to really link the classes together (polymorphic), error included in the source where the error would be.

#include 

using namespace std;

class baseClassNotWork
{
protected :
int x;
public :
baseClassNotWork() { x = 0; };
int returnX() { return x;}
};

class subBaseClassNotWork : public baseClassNotWork
{
public :
subBaseClassNotWork() : baseClassNotWork() {};
int returnX() { return x;}
};

int main()
{
baseClassNotWork *baseNot = new baseClassNotWork();
//error: cannot dynamic_cast

Static_cast – cpp

Static casting (static_cast) is when you want to change the type of the variable e.g. if you want to have a the number of pies that are left to be 0.5 instead of a whole value then you would need to change the integer value to a double. The static_cast can change one type to another, as long as it is compatible.

The general syntax for static_cast is

returnvalue = static_cast<returntype>(value to cast)

Where the return value is the value returned from the casting, returntype is the variable type that you want to have (e.g. double) and the value to cast speaks for itself.

For the example above about pies, the code would be

     int pie = 3;
     double fl = static_cast<double>(pie);

The static_cast(ing) and also other types of casting are the long form as such compared to the standard C version of

   double fl = (double)pie;

But with the static_cast you can be sure that the implementation will be type checked and also because of the classes etc then you need to type check the results unless you may have some value that is not want you was wanting.

Linux file structure – comparsion with Windows

Here is the Linux file structure from the root, which some explanations. The main explanation for the /bin, /boot /lib are the basic files that are required on boot up that the kernel needs to “run” as such. The files within the /usr are the files that user programs use e.g. games.

Anyway here is a list of directories on my linux setup.

bin binary files for boot up e.g. mount e.g.
boot boot files – kernel images etc.
dev devices on the computer.
etc configuration files for the programs
home home users, e.g. your name /home/ian
lib libraries
lib32 libraries for the 32bit programs
lib64 /lib (link to the libraries since I am using the 64 linux version)
lost+found
media media that is going to be mounted (cd-rom’s)
mnt media that is going to be mounted (Hard drives etc)
opt opitional programs e.g. things like google chrome, they are normally place in here is not distro specific.
proc processes that are happening on the computer, all process you can “talk” to
root root home files.
sbin sbin, booted up at initial stage of the boot process, things like modprobe for setting up systems items.
sys system image of devices attached and also file systems that are loadable.
tmp tempoary files.
usr user files, e.g. games, libraries, binary files
bin games include lib lib32 lib64 local sbin share src
it has its own includes, libraries, sbin and bin directories for all of the files within that user directory.
var variable files, e.g. logs, apache www hosting files.

The Windows equivalent would be that most of the / (root) directory is within the c:/windows directory, apart from the /home which is the c:/Users or c:/Documents depending on your Windows version.

bin /Windows /Windows/System32 /Windows/System
boot boot.ini file that points to what to do.
dev Does not appear to have something similar on the file system
etc /Program Data (depending on Windows versions)
home /Users
lib /Windows /Windows/System32 /Windows/System /Windows/.Net (for .Net stuff) etc.
lib32
media Does not appear to have a link to the different mount points but display them on the windows explorer
mnt Does not appear to have a link to the different mount points but display them on the windows explorer
opt /Program files/
proc Does not appear to have a list of running process on the file system, but you can view them with pslist pskill
root /Documents/Admin user account
sbin /Windows
sys Does not appear to have a list of devices attached
tmp /tmp
usr /Program files/
var /Program files/ or where ever you want them.

That kinder helps me to understand that there is more details on the command line, directory structure to actual processes and devices attached than Windows, well of course there is the regviewer that can display options like the /etc in the linux but nothing as structured, things just across like a mess (to me anyway).