Simple string manipulation in C++

January 24th, 2010

The code bellow is a simple example, howto pass string to a function.


#include
#include
using namespace std;
void display(string a, string b, string comp){
cout << "The word \""<< a << "\" is " << comp << " the word \"" << b << "\"" << endl;
}
int main(int argc,char **argv){
if(argc < 3){
cout << "Please type two words after the program name." << endl;
}else{
string a = argv[1];
string b = argv[2];
if(a < b){
display(a,b,"less than");
}else if(a > b){
display(a,b,"greater than");
}else if(a == b){
display(a,b,"equal to");
}
}
return 0;
}

  • Share/Bookmark

Setting up to compile C++ on Linux

January 24th, 2010

Firts, check if you have g++ instaled.
Run this on console:

g++ -v

If show version, g++ installed.

Now, create a simple shell script to compile you cpp file

#/bin/bash
# gpp compiller
g++ -pedantic-errors -ansi -Wall -o $1 $1.cpp

Now, to compile the str.cpp use this:

gpp str

  • Share/Bookmark

Generate a Certificate Signing Request (CSR) on CentOS

January 17th, 2010

Follow this steps

openssl genrsa -des3 -out elmaqu.es.key 1024

Now, you have the RSA PRIVATE, let’s create de CSR…

openssl req -new -key elmaqu.es.key -out elmaqu.es.csr

Don’t type additional password. Yum must fill all fields correctly.

Now, cat elmaqu.es.csr and pest on website

  • Share/Bookmark

Install imagemagick with PHP imagick extension on CentOS

January 17th, 2010

To install imagemagick with imagick extension on centos follow these steps:

yum install -y ImageMagick ImageMagick-devel imagick

After, include file for imagick.so module and restart apache:

echo “extension=imagick.so” > /etc/php.d/imagick.ini
/etc/init.d/httpd restart

  • Share/Bookmark

Install GeoIP on CentOS

January 17th, 2010

This is a very simple step-by-step..

1.Install geoip via yum: yum install geoip geoip-devel

2.You also might want to download database with ip addresses from Maxmind website and place it in /usr/share/GeoIP (which is a default location of geoip upon installation).
3.Install PECL extension: pecl install geoip

4.Add extension=geoip.so to your /etc/php.ini

5.Restart: apachectl restart

  • Share/Bookmark

Using in memory caching on PHP. The easy way.

August 31st, 2009

With memcached you can store array and other data in server memory. For example, you can use memcache to store data from a mysql_query in memory to expire some time in future. Using memcache you connect to database or other resource only when needed.

I’m using memcache to store mysql querys to reduce connection on database, but, you can use to store ldap, imap and any other data type. But careful with complex objects like XML, some times the object get corrupted, becouse XML use very complex in-memory data structures.

Here is two function use to simple put and get data in memcached daemon:

// connect to a memcache server on localhost
if( class_exists('Memcache') ){
	$memcache = new Memcache;
	if(!@$memcache->connect('localhost', 11211)){
		unset($memcache); // no valid memcache connection
	}else{
		define("memcache",true); // enable memcache use
	}
}
 
// store data on cache memory for five minutes (default ins 300 seconds)
function cache_set($key,&$data,$ttl=300){
	global $memcache;
        // here is the trick, set timestamp in the future.
	$ttl = strtotime("+{$ttl} seconds");
	if(!defined("memcache")) return false;
	return $memcache->Set($key, $data, false, $ttl);
}
// get pointer for data in memory, if expired return false
function &cache_get($key){
	global $memcache;
	if(!defined("memcache")) return false;
	return $memcache->Get($key);
}
?>

And, here how to use the code above:

<?php
 
$my_data = cache_get("my_key");
 
// if the cache has expired, fetch data from database
if( $my_data === false ){
      // connect to mysql and fetch data
      $sql = "select * from mysql.users";
      $tmp_resource = mysql_query($sql) or die(mysql_error());
      while( $tmp_row = mysql_fetch_assoc($tmp_resource) ){
            $tmp_data[] = $tmp_row;
      }
      // now, store tmp_data on memory cache for five minutes (300 seconds)
      cache_set("my_key",$tmp_data,300);
      // link tmp_data with my_data variable
      $my_data =& $tmp_data;
}else{
      // do noting, my_data has been loaded from memory cache
}
print_r($my_data);
?>

How to enable memcache on you phpinstalation.

First, you need to install the service, on Microsoft Windows, you can install the “memcached for Win32″, this port is easy to install and configure. On Linux Centos you can simple use yum, if you have online repository configure, use this command: yum install memcached or if not, you can install manual following tips on ”
Memcached: How do you install memcached? (CentOS 64 bit, Linux, Redhat, Fedora)”. After installation check if service is running, on Microsoft Windows check on taskmanager for the service memcached or on Linux simple type in console: ps ax | grep memcache and check the process.

After installing memcached on you system, you need to enable de php_memcached.dll extension on you Microsoft Windows installation or php_memcachd.so on Linux. Restart Apache and test function.

  • Share/Bookmark

Hello world!

August 25th, 2009

I’m back, after long-long time out of bloging world…

  • Share/Bookmark