Archive for the 'Source Code' Category

C, Source Code

Growing String in c

This article contains the source code for a c utility that manages a growing string, and a brief explanation of the code.

To obtain this, we make something similar to a c++ class: a struct with associated functions. The functions add characters to the string in the struct, and allocate more memory space when the string is full.

This is usefull when we are working with string with an unknow length (e.g. we are reading a file).

The struct containing the string is the following:

// Struct containing the growing string
typedef struct
{
	// string
	char * text;
	// string length (with zero terminator)
	int length;
	// string capacity
	int capacity;
} growing_string;

The text variable is where the string is allocated, length is the current string lenght and capacity is the memory allocated for the string.

To better understand the difference between length and capacity, look at the next image:

Capacity-Length

Capacity is the total space allocated for the string, while length is the actual number of character of the string, including the \0 terminator (the special character that close the string). If we add enough characters and the length is greater than the capacity, we need to allocate more memory for the string.

Continue Reading »

C, Source Code

How to use poEdit

In this guide I will explain how to simplify the localization process of a software written in c++ using “wxWidget (a cross platform toolkit), with poEdit.
poEdit offers to developers a conventient approach to localizazion, and there is no need to modify the source code by hand to modify translations, because they are all stored on a catalog file that is easy editable.

Logo poEdit

To prepare the source code to the localization, we must sorround all strings that we want to localize with the macro “_()”, for example:

...
wxMessageBox( _("localized string") );
...
wxMessageBox( wxT("not localized string") );

Continue Reading »

C, Source Code, USBAutoStart

First public version of USBAutoStart!

USB AutoStart

Usbautostart is a Windows utility that performs autoplay for usb mass storage device. Windows avoid the autoplay on the removable medias for security reasons, but with this program you can bypass this limit and you can choose to launch a program in the usb pen when you plug it.
The program will also close all applications running from the device when you try to “safety remove” it.

Screenshots:

Usb peripheral detection (the red icon on the left is Mozup, the program started from the usb pen):
USB AutoStart Inserting device

Usb removal:
USB AutoStart Removing device

The project is hosted on sourceforge, and it’s released under GPL license, so it’s completely free and open source! :)

Download Usb Autostart

MySQL, Source Code, php

MySQLDump 2.0

This is a guest post from Daniele

After a lot of work, I released the new version (2.0) of the MySQLDump class, already introduced by this article.
MySQLDump is a class that allows to do a complete MySQL database backup with php pages.

After the inandrea’s good work there wasn’t too much space for improvements, but I think that these addition can be usefull: now it’s possible to export structures and/or data not only for the selected database but also for a single table.

The following example will explain how to export the structure and the data for the table mytable in the db mydb (the interface to the class is slightly changed from the previous version).

//Include the library
@include_once('lib_dump.php');
//Db connection
$connection = @mysql_connect('127.0.0.1','username','password');
//Create the MySQLDump class instance
//1° parameter: db name
//2° parameter: the exported file that will contain the dump
//3° parameter: create zipped file (true = zipped, false = normal)
//4° parameter: data encode (true = hexadecimal, false = plain text)
$dumper = new MySQLDump('mydb','dumpfile.sql',false,false);
//Structure export of the table 'mytable'
$dumper->getTableStructure('mytable');
//Data export of the table 'mytable'
$dumper->getTableData('mytable');

Continue Reading »

C, Source Code, UDP Communication

UDPListener: Receiving UDP messages in C#

This post is part of the UDPCommunication project.

UDP - User Datagram Protocol

The class in this post allows, in an easy and fast manner, to receive UDP messages. You don’t need to be a socket expert to use the class, cause you only need to write few rows of code to use it. Following, a little example showing how to use the class:

private void InitConnection()
{
	m_udpListener = new UDPListener(m_port, new byte[4] { 0, 0, 0, 0 });
	m_udpListener.MessageReceived += new UDPMessageReceivedDelegate(udpListener_MessageReceived);
}

Continue Reading »

C, Source Code, Windows

Reboot or Shutdown a pc

This is a little how-to that explains how to shutdown or reboot a Windows machine in c++ .
In the source code you will find the function ShutDown, definied as follow:

bool ShutDown(bool restart)

The function shutdowns or reboots the pc, and returns false if the shutdown can’t be done (for example a process denied the power off).

Shut Down

So, to shutdown the pc, you only have to call the function and check if the shutdown was done.

if (!ShutDown(false))
{
	// shutdown not done.
}

Part of source code following:
Continue Reading »

C, Source Code, UDP Communication

UDPSender: Sending UDP messages in C#

This post is part of the UDPCommunication project.

Header UDP

This is a class that allows to send UDP messages in C# with an unbelievable semplicity.
To create the class you only need to create it, specifying a port for the udp communication, eg 3125 in this case:

try
{
	INetworkSender udpSender = new UDPSender(3125)
}
catch(UDPSenderException e)
{
        	// error during the socket initialization
}

Sending a message to an host is simple: call the SendMessage method specifying the receiver (with an host name, like “www.coders4fun.com“, or with an ip, like “192.168.10.6“) and a byte array containing the message.
Continue Reading »

News, Source Code, Wiki-Dashboard, Wordpress, php

Wordpress Plugin: Wiki-Dashboard 0.1

Wiki-Dashboard is a Wordpress plugin that allows to all registered user on a blog to share and edit a text like a wiki, and can be usefull even on mono-author blogs, giving a space to write notes.

Features:

  • Dashboard subpage with the text where to put notes
  • Text shared to all blog authors
  • bbCode parser for text formatting
  • Internationalization support

Installation:
Simply download the Zip-Archive and extract all files into your wp-content/plugins/ directory. Then go into your WordPress administration page, click on Plugins and activate it.
After that you will have a new submenu called “Wiki” under the “Dashboard” menu.

Download Wiki Dashboard 0.1. Downloads: 758

Screenshots:
Wiki-Dashboard showing the text:

Wiki-Dashboard View

Wiki-Dashboard editing the text:

Wiki-Dashboard Edit
Continue Reading »

C, Source Code

Edit form elements from a Thread

Scenario:
The program has to execute a long operation (such as a downloaded file from the internet) when the user clicks on a button, but during the operation the user has the opportunity to interact with the program, and the program gives a visual feedback of the task completion percentage.
Easy, but not working, solution:
To solve a problem like that we need to use a Thread that executes the task in a separate context from the main Form, otherwise, if we execute the long operation in the onClick method of the button, the form will remain frozen during the task execution. The problem comes out when the program tries to update the form from the thread:

private void button1_Click(object sender, EventArgs e)
{
    testThread = new Thread(threadProc);
    testThread.Start();
}

public void threadProc()
{
    // wast a lot of time...
    for (int i = 1; i <= 10; i++)
    {
        Thread.Sleep(250);
        progressBar.Value = i * 10;
    }
    MessageBox.Show("Operation Completed!");
}

Unfortunately, the code that assigns the value to the progressBar will produce an exception like that:
“Illegal cross-thread operation: Control ‘progressBar1′ accessed from a thread other than the thread it was created on.”

lock

Continue Reading »

MySQL, Source Code, Web, php

MySQLDump backup class interface

MySQLDump class is now at version 2.0 and this article contains obsolete informations about the version 1.0. For information about the new version check this article

After the success of the article PHP backup of a mysql database, I decided to write a short post about a file that can be usefull to download the backup of your db site/wordpress blog directly from your browser.
To use the script, you can simply download it and edit the first lines with the basic configuration:

// DB Configuration
$server = "IP of your server (localhost will work in most cases)";
$username = "Valid username for your mysql db";
$password = "Password";
$dbToDump = "Db to dump";
// password to access the php page. PLEASE change the default password!!!
$backupPassword = "UltraSecretKeyThatYouMUSTInsertHere";
// filename to save
$filename = "backup.sql.gz";

After that you can upload the file to your server, in the same folder where is located the class MySQLDump.
To access the script you have to specify two parameters: the password to access the script (pass), and another parameter (t) that if it’s 1 will separate the SQL inserts every 100 rows.
For example, if you upload the file to the root directory of yoursite, you have to write in the address bar of the browser:

http://www.yoursite.com/backup.php?pass=UltraSecretKeyThatYouMUSTInsertHere&t=1

…and your favorite browser will open the dialog to save the file with the db backup!

Download MySqlDump Interface.php. Downloads: 451

For more info about the MySQLDump class, read the original post, or download the source from here:

Download MySQLDump. Downloads: 2354

Next »