Using/Compiling SQLite with Dev-C++ for c project and cpp project

Win32 API No Comments »

How to compile sqlite with c or c++ project in DEV-C++ IDE?

That was my question when i wanted to use sqlite in my c and cpp program.

I searched the internet for sqlite library for dev c++ and used some which worked for me.

For all the options include sqlite3.h


OPTION 1:
To compile sqlite3.c with a C project

Download http://www.sqlite.org/sqlite-amalgamation-3071502.zip which can be found in http://www.sqlite.org/download.html – the amalgamation.

Open the zip and copy sqlite3.c to your project folder.

I assume that you have created a c project(for cpp i have mentioned below)

In dev c++ ide right click the project name which is listed in the project explorer pane at the left side and select ‘Add to Project’. Choose the sqlite3.c file and after that you can see sqlite3.c listed in project explorer with other files.

Press F9 to check whether it is compiling or not. It should.

By doing this all the functions of sqlite3 are statically included in the final executable executable.


OPTION 2:
To compile sqlite3.c with a C++ project

now we need to create a cpp program which uses sqlite3.

Do as mentioned in STEP 1 and …

Go to Project >> Project Options >> Files.

Select sqlite3.c and untick the “Compile file as C++” option. (If we do not do this step then we get invalid conversion errors)

This should inform Dev-C++ that it should invoke gcc.exe, and not g++.exe cause gcc is for c files and g++ for cpp and since sqlite3.c is a c file we have to compile with a c compiler.

The reason for doing these i want to know how to include a source file into a project instead of using a .lib file or a .dll file


OPTION 3
using sqlite DEVPAK in dev C++

This option works for both c project and cpp project.

Download sqlite devpak from
http://www.ce.unipr.it/people/medici/DevPak/sqlite-3.6.22-2pm.DevPak which is present in devpak site http://devpaks.org/details.php?devpak=281

(OR)

http://sourceforge.net/projects/devpaks/files/SQLite/sqlite%203.5.6/

In dev c++ ide select Tools >> Package Manager

The package manager window will open. In that select Install and it will show a dialog box to select the *.devpak file.
Now select the devpak file you downloaded from the above links. Done.

You can compile sqlite with a c project or cpp project… it worked for me with c++ project and home it will work for a c project as well. i haven’t checked for the latter.


References


—-

That is all folks. Enjoy.

format filesize as bytes kilobyte megabyte kb-mb-gb ending in php

PHP No Comments »

I want to format a filesize in php as either of these bytes, kilobytes, megabytes, gigabytes according to the size of the file.

I found the following codes in php.net comments section

I just copied the following code as it was in the source and i have use only one and not the others.

You can better directly do this url to see the updated function or try the following functions.

You can find these below the documentation of filesize function. (The comments section of the document).

function FileSize($file, $setup = null)
{
    $FZ = ($file && @is_file($file)) ? filesize($file) : NULL;
    $FS = array("B","kB","MB","GB","TB","PB","EB","ZB","YB");
 
    if(!$setup && $setup !== 0)
    {
        return number_format($FZ/pow(1024, $I=floor(log($FZ, 1024))), ($i >= 1) ? 2 : 0) . ' ' . $FS[$I];
    } elseif ($setup == 'INT') return number_format($FZ);
    else return number_format($FZ/pow(1024, $setup), ($setup >= 1) ? 2 : 0 ). ' ' . $FS[$setup];
}

Here is another one

 
function format_bytes($size) {
    $units = array(' B', ' KB', ' MB', ' GB', ' TB');
    for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
    return round($size, 2).$units[$i];
}

and yet the simplest one as mentioned in the docs

 
function format_bytes($bytes) {
   if ($bytes < 1024) return $bytes.' B';
   elseif ($bytes < 1048576) return round($bytes / 1024, 2).' KB';
   elseif ($bytes < 1073741824) return round($bytes / 1048576, 2).' MB';
   elseif ($bytes < 1099511627776) return round($bytes / 1073741824, 2).' GB';
   else return round($bytes / 1099511627776, 2).' TB';
}

besides, the document said

It’s very interesting but I can’t stand the decimals for bytes and KB so here’s another example :

 
function format_size($size) {
      $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
      if ($size == 0) { return('n/a'); } else {
      return (round($size/pow(1024, ($i = floor(log($size, 1024)))), $i > 1 ? 2 : 0) . $sizes[$i]); }
}

and then another contributor said

This is a short and very clever function to get filesizes.

 
function format_size($size) {
      $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
      if ($size == 0) { return('n/a'); } else {
      return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }
}

and it goes on like the above in http://php.net

Reference

http://php.net/manual/en/function.filesize.php

Scroll the page down to see as many as function like the above and suite yourself.

Enjoy.

move values items from one multiple select box to another multiselect in javascript

JavaScript 5 Comments »

This article is about moving values or items between one multiple select box to another. Here i have given the html and the javascript code and have explained the working of it.

I have two multiple select box. One is leftSelect and another is RightSelect.

In between that i have two buttons one is to move items to the right select box and the other is to move items to the left select box.

I have statically filled the left select box with simple values and items and if you want to know how to add values dynamically then you can refer this same code where i have used .add method.

The code is

the left select box is with id=’leftSelect’ and the right select box is id=’rightSelect’

<select class='sel' id='leftSelect' multiple>
<option value='1'>one</option>
<option value='2'>two</option>
<option value='3'>three</option>
<option value='4'>four</option>
<option value='5'>five</option>
<option value='6'>six</option>
</select>
 
<input type='button' value='>>' onclick='move(0)'>
<input type='button' value='<<' onclick='move(1)'>
 
<select class='sel' id='rightSelect' multiple>
</select>

the right select box is empty the reason is that i am just giving a demo with empty. if you want you can fill it up with values.

the >> button will move the selected values form the left select to right select and the << button will do the reverse. For both the buttons i have called move with value 0 to mean moving to right and value 1 meaning moving left. Now you can see the javascript function which manipulates these select boxes.

function move(direction)
{
	var src = document.getElementById('leftSelect' )
	var trg = document.getElementById('rightSelect' )	
	var tem
 
	if(direction)
	{
		tem = src
		src = trg
		trg = tem
	}
 
	var selected = []
 
	for(var i in src.options)
	{
		if(src.options[i].selected)
		{
			trg.options.add(new Option(src.options[i].text, src.options[i].value));
			selected.unshift(i);
		}
	}
 
	for(i in selected)
		src.options.remove(selected[i]);
}

How does it work?
The working of this function is it copies the selected items from one select box(source) to another(target) and deletes the selected items in the source.

The first for loop will copy the items from source to target.

while copying i am storing the index of the selected items in an array in the reverse order. the reason for reversing is to maintain the indexing of the selected items. to know why reverse is used use selected.push instead of .unshift and see the effect. and the reason for copying the selected item into an array instead of instantly deleting it is because of opera browser. In this browser when an item is deleted all the elements in the select box are deselected.

The second for loop will delete the selected items from the source one by one iterating the array.

How does it work in action?
when >> button is clicked i send 0 as parameter to mean from left to right. so values from left select box are copied to to right select box and the the selected items are deleted.

The condition if(direction) will not execute so left select is source and right select is target.

When << button is clicked i want to consider right select as source and left select as target so i pass 1 as parameter to move function. now the if(selected) works so right select becomes source and left becomes target so values will move from right select to left select. The reason for using this is i want to use the same function for both the operation that is one function to move values both sides. here is another alternate for swapping source and target.

 
var src = document.getElementById( direction ? 'rightSelect' : 'leftSelect' )
var trg = document.getElementById( direction ? 'leftSelect' : 'rightSelect' )
var tem
 
/*	
if(direction)
{
	tem = src
	src = trg
	trg = tem
	}
*/

Hope you can understand the difference. Remove the commented code so that it reduces space and the code becomes small and crisp. I did it the above for my purpose and including both for better understanding. if so.

Enjoy.

using timers in win32api windowless application

Win32 API No Comments »

I wanted to write a windowless win32 program with timers. It will run in the background and will trigger some functions at specific time intervals. so followed the following conditions. Which would not consume more cpu time.

  • No Window
  • No Window Class defined
  • It should not be a console application.
  • Is should be a kind of background process yet not a windows service
  • It should not contain while loops and sleep functions which shall overloads the CPU
  • It will have timers

I had been reading discussions which discouraged the use of while loops and sleep functions if you are not going to have a windowed application. I too felt the same cause that will consume more of the CPU usage were as there should be a way to make the instance wait for a message. and at this point i thought of using only the message loop as suggested by experts and it worked.

The code below works well if you are not going to have threads and other stuff.

If you program is going to be standalone and if it is simple then you can go for this. I have provided reference links at the bottom of this post.

Here is the code i used. I used Dev-C++ IDE to compile.

The following code will call the function timerfunc every 20 seconds.

#include <windows.h>
 
void timerfunc();
 
int main()
{
 
    SetTimer(NULL, 1, 20000, (TIMERPROC) timerfunc);
    MSG msg;
 
 
    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
 
    return 0;
}
 
 
void timerfunc()
{                      
 
//some code here
 
}

Here i tested by giving void main instead of some thing like the following and it works. I have to do another discussion related to this cause it had been a long time since i did win32 api programming.

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)

Besides, the above is what i researched and did for the least. I will have to explore more and would be updating this page.
so, this is all i have for now.

References:

http://stackoverflow.com/questions/13233084/win32-windowless-application-using-timer-using-message-loop-and-without-using-cp

http://forums.codeguru.com/showthread.php?398186-windowless-timers

http://stackoverflow.com/questions/4487038/win32-windowless-application-wait-for-program-exit

how to check whether a javascript object is empty?

JavaScript No Comments »

== Option One ==

 
function isEmpty(ob){
   for(var i in ob){ return false;}
  return true;
}
 
isEmpty({a:1}) // false
isEmpty({}) // true

== Option Two ==

function isEmpty(o) {
  var o = {};
  for(var p in o) {
    if (o[p] != o.constructor.prototype[p])
      return false;
  }
  return true;
}

== Option Three ==

 
function isEmpty(ob){
   for(var i in ob){ if(ob.hasOwnProperty(i)){return false;}}
  return true;
}

== Option Four ==

what about this?

 
var obj={}
Object.prototype.isEmpty = function() {
    for (var prop in this) {
        if (this.hasOwnProperty(prop)) return false;
    }
    return true;
};
alert(obj.isEmpty())

== Option five – Jquery ==

 
jQuery.isEmptyObject({}) // true
jQuery.isEmptyObject({ foo: "bar" }) // false

== References ==

* http://www.webdeveloper.com/forum/showthread.php?t=193474

* http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object-from-json

Similar question here about null object in javascript

* Check javascript object empty or null

set_exception_handler function within a class for example codeigniter

PHP No Comments »

Here is how to use the set_exception_handler function within a class

the defined exception_handler method must be declared as public preferably public static if using the array syntax like array(‘Abc’, ‘exception_handler’) as suggested in php.net site.

class Abc extends CI_Controller {
 
    function __construct()
    {
        parent::__construct();
        set_exception_handler(array('Abc', 'exception_handler');
        //also you can use array($this, 'exception_handler')     
    }       
 
    function exception_handler(Exception $ex)
    {        
        echo $ex->getMessage();
    }
 
    function Index()
    {
        throw new Exception('hehe');
    }
}

The exception handler must be defined before calling set_exception_handler()

so the above code sets the default exception handler if an exception is not caught within a try/catch block. Execution will stop after the exception_handler is called.

imagick why in three or more image resizes one is blurred

Image Manipulation No Comments »

My recent work on image manipulation(image re size) got into trouble.

I used imagick to resize images.

i want to resize a source images into three sizes.

here is the sample code i used

	$image	=	new Imagick($_FILES['pic']['tmp_name']);
	$image->setImageCompression(Imagick::COMPRESSION_JPEG);
	$image->setImageCompressionQuality(100);	
	$image->thumbnailImage(220, 220, true);
	$image->writeImage(DOC_ROOT."/images/pic/$id.jpg");
	$image->thumbnailImage(80, 80, true);
	$image->writeImage(DOC_ROOT."/images/pic/second/$id.jpg");
	$image->thumbnailImage(100, 100, true);
	$image->writeImage(DOC_ROOT."/images/pic/third/$id.jpg");	
	$image->destroy();

with the above code the second image got blurred. after checking for a while my team leader mentioned the fact with how imagick works.

An object is created and it is set to create jpeg files.

The first resize is done. So what happens here is the file from the temp folder is taken and resized into 220 x 220 and is saved in pic folder.
For the second resize instead of using the fresh copy from the temp folder the already resized image of 220×220 is used from the objects memory (not from the pic folder).
So 220×220 is again made a temp copy in the buffer and is resized to 80×80 and is saved in second folder. Now the object has the 80×80 image in its buffer.
So for the third time 80×80 is called from the objects buffer and is resized into 100×100.
Here what the function is doing is it increases 80×80 into 100×100 which makes the image blur.

What i thought was that each time we call the resize function it is using the original copy form the temp folder.

But what the function is doing is it resizes the original copy to 220×200 and it keeps in the temp folder.
so at the first resize the image got resized to 220×220 in the temp folder. and for the second resize it has to use the temp copy which is already resized.

What i thought it would do is make a copy of the original image to the folder we have specified and then it resizes. So what the original file is intact.
This is how i do in my projects using codeigniter but this function resizes the image keeping it all the time in the temp folder.

So every time we call the resize function the file in the temp folder is the one which is manipulated.

First it is in the original size and then it is resized and overwritten to 220×220 in the temp folder.
for the second resize we have only the 220×220 and not the original size.
and this goes for the third resize…. oops…

So what is supposed to be done?

That is have a fresh initialization each time or use it in descending order. that is first resize the image to 220 and then to 100 and then to 80 so it will retain the quality.
If you do it in the mixed order then if you specify a size which is greater than the current image size then you get a blurr output.

facebook on authorization the dialogue goes to channel.html?fb_xd_fragment problem

Facebook No Comments »

I was writing a facebook application.

I had a form when the submit button is clicked i show the authorization dialogue.

What happened was when the user either clicked cancel or accept the dialogue redirected to channel.html instead of closing itself.

This happened in Internet Explorer.

On other browsers when i pressed the cancel button it redirects to http://facebook.com/home.php (i think it is a problem with facebook)

After doing trial and errors i found that i had a non https url also when ever i loaded the page it showed the insecure content alert in internet explorer.

After fixing the urls to https making sure that no url is http in my site that insecure content alert did not appear and i did not get the channel.html

so after fixing the insecure content problem and after authorization of facebook the dialogue closed properly as expected.

so if you guys are struck with this then try to change no https url’s to https and the dialogue works fine.

Besides, if you want another work around which is not completely verified way… here we go…

in the channel.html use javascript code to parse the url.

find where the request has come from and according to that use window.close

for example when i clicked cancel i had the content in the url saying “user cancelled” (this is not the exact word i got but i am just giving an example)

so i can parse to find that word and can close the dialouge according to it.

Like this you can check the url and find a text which appears when ever you cancel or authorize and use it to condition to close the dialouge window.

🙂

centring float left elements

HTML No Comments »

I wanted to center the divs which are floated left.

I searched the web and found this and it is working for me.

http://stackoverflow.com/questions/1084666/how-can-i-center-the-contents-of-a-div-that-float-left

This is just a copy for my reference even if that content is getting deleted future.

Besides, the following is the code which does the exact trick which is referred in the above stackoverflow url.

http://www.pmob.co.uk/temp/centred-float4.htm

<style type="text/css">
html,body{
	margin:0;
	padding:30px 10px;
	text-align:center;
}
#outer{
	width:780px;
	margin:auto;
	text-align:left;
	border:1px solid #000;
}
#outer{position:relative}
.navwrap{
	float:right;
	position:relative;
	left:-50%;
	text-align:left;
	margin-bottom:2em;
}
.navwrap ul{
	list-style:none; 
	position:relative;
	left:50%;
	margin:0;
	padding:0;
} 
.navwrap li{
	border:1px solid #eff2df;
	float:left;
	margin:0 10px 0 0;
	background:#809900;
}
.navwrap li.last{margin-right:0}
.navwrap li a{
	float:left;
	border:1px solid #4c7300;
	position:relative;
	left:-2px;
	top:-2px;
	background:#eff2df;
	color:#4c7300;
	text-decoration:none;
	padding:6px 10px;
	font-weight:bold;
}
.navwrap li a:hover{
	background:#809900;
	color:#fff;
}
p{clear:both;padding:10px}
h1{text-align:center;margin:1em 0;}
.clearer{
	height:1px;
	overflow:hidden;
	margin-top:-1px;
	clear:both;
}
 
 
 
</style>
<!--[if IE ]>
<style type="text/css">
.navwrap ul{float:left;} 
 
</style>
<![endif]-->
 
<div id="outer">
	<h1>Centred Widthless floats</h1>
	<div class="navwrap">
 
		<ul>
			<li><a href="#">Button 1</a></li>
			<li><a href="#">Button 2's a bit longer</a></li>
			<li><a href="#">This is Button 3</a></li>
			<li><a href="#">B 4</a></li>
			<li><a href="#">Button 5</a></li>
 
			<li class="last"><a href="#">This is Button 6</a></li>
		</ul>
	</div>
	<div class="clearer"></div>
	<!-- ie needs this clearer -->
	<div class="navwrap">
		<ul>
			<li><a href="#">Button 1</a></li>
 
			<li><a href="#">Button 2's a bit longer</a></li>
			<li class="last"><a href="#">This is Button 3</a></li>
		</ul>
	</div>
	<div class="clearer"></div>
	<!-- ie needs this clearer -->
	<div class="navwrap">
		<ul>
 
			<li class="last"><a href="#">Button 1</a></li>
		</ul>
	</div>
	<p>The above navigation is floated using widthless floats and centred using a technique that moves the right floated parent wrapper 50% back towards the left using relative positioning -50%) and then applying a +50% left shift on the inner element also using relative positioning.</p>
	<p> The combined effect of this is to center the navigation within our layout</p>
</div>

facebook comments set 100% percent width for facebook comments

Facebook 1 Comment »

I wanted to set percentage width for facebook comments plugin.

I searched the web and i got the following information from stackoverflow.

What i previously had was the following

<div id="fb-root"></div>
<div class="fb-comments" data-href="http://vikku.info/programming/" data-num-posts="100" data-width="500"></div>
</div>

I was trying to set data-num-posts=’100%’ which was not working. and then change the above to the following

<div id="fb-root"></div>
<div class="fb-comments" data-href="http://vikku.info/programming/" data-num-posts="100" data-width="500" width="100%" style="width: 100%;"></div>
</div>

and then added the following style in the style section

/* for facebook width 100% */
iframe.fb_ltr { width:90% !important; }

That’s it. It is working. …

oh… after a few days of working with the above i added a like button. the above style affected the like button and i was wondering how to change the width to 100% without affecting the like button.
Then got this from stackoverflow. So i placed only the following and removed the styles specified above.

remove width=”100%” style=”width: 100%;” from <fb:comments tag and add only the following in the style. voila it works.

.fb-comments, .fb-comments iframe[style] {width: 100% !important;}

the above was used from this url http://stackoverflow.com/questions/7447483/how-to-make-facebook-comments-widget-a-fluid-width

References

http://stackoverflow.com/questions/5305685/facebook-comments-plugin

http://stackoverflow.com/questions/7447483/how-to-make-facebook-comments-widget-a-fluid-width

Entries RSS