<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coding Friends</title>
	<atom:link href="http://www.codingfriends.com/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Thu, 11 Mar 2010 12:16:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL &#8211; triggers</title>
		<link>http://www.codingfriends.com/index.php/2010/03/11/mysql-triggers/</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/11/mysql-triggers/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 11:58:16 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[SQL (Structured Query Language)]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[triggers]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=801</guid>
		<description><![CDATA[In MySQL version 5 onwards you can use Triggers, triggers are a nice was of checking values within a insert/update/delete process.  A trigger happens either just before the insert actually happens into the database, or just after
Lets say that you create a table as below

CREATE TABLE testTable 
&#40;id int NOT NULL AUTO_INCREMENT, 
  [...]]]></description>
			<content:encoded><![CDATA[<p>In MySQL version 5 onwards you can use <a href="http://dev.mysql.com/doc/refman/5.5/en/triggers.html">Triggers</a>, triggers are a nice was of checking values within a insert/update/delete process.  A trigger happens either just before the insert actually happens into the database, or just after</p>
<p>Lets say that you create a table as below</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> testTable 
<span style="color: #66cc66;">&#40;</span>id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span> 
  guid varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
  name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>and then if you want to insert a name into the name column you could do</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> testTable <span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;thename&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>but you are not inserting the anything into the guid column because you can force a value into that column with a trigger (the id is already creating a value with the primary key, auto_increment).</p>
<p>To create a trigger you need to set the delimiter to &#8220;//&#8221; because within sql you need to use the &#8220;;&#8221; which is the standard delimiter (when the MySQL will try and execute the query)</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">delimiter <span style="color: #66cc66;">//</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> testTable_insert 
BEFORE <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #ff0000;">'testTable'</span> 
<span style="color: #993333; font-weight: bold;">FOR</span> EACH ROW 
BEGIN 
    <span style="color: #993333; font-weight: bold;">SET</span> NEW<span style="color: #66cc66;">.</span>guid <span style="color: #66cc66;">=</span> uuid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
END;
delimiter ;</pre></div></div>

<p>&#8220;CREATE TRIGGER&#8221; is just like creating a table, apart from you are creating a trigger, so the next value is the trigger name, normally it is the table name with what you are doing, e.g. inserting</p>
<p>&#8220;BEFORE INSERT ON&#8221; means before you actually insert the data into the database (it is in a hold area as such). the next value is the actual table that you are linking this trigger to.</p>
<p>&#8220;FOR EACH ROW&#8221; means each row of the insert, since you can insert x amount of lines into a table.</p>
<p>the sql code is between the &#8220;BEGIN&#8221; and &#8220;END&#8221;, and all is what is being set is the guid column within the &#8220;NEW&#8221; (this is the new row to be inserted) and setting that value to <a href="http://dev.mysql.com/doc/refman/5.1/en/miscellaneous-functions.html#function_uuid">uuid</a>() which is a mysql function.</p>
<p>so before you would have inserted into a table and the table would have looked like</p>
<table>
<tr>
<td width="100">
<td width="100">
<td width="100">
</tr>
<tr>
<td>ID</td>
<td>GUID</td>
<td>Name</td>
</tr>
<tr>
<td>1</td>
<td></td>
<td>thename</td>
</tr>
</table>
<p>but once the trigger is in place and you did the same process again of inserting a new name</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> testTable <span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;a new name&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<table>
<tr>
<td width="100">
<td width="100">
<td width="100">
</tr>
<tr>
<td>ID</td>
<td>GUID</td>
<td>Name</td>
</tr>
<tr>
<td>1</td>
<td></td>
<td>thename</td>
</tr>
<tr>
<td>1</td>
<td>3ace82c-2cf1-11df-b1c3-00a0d1a1240a</td>
<td>a new name</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/11/mysql-triggers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Join files together into a single file and expand out</title>
		<link>http://www.codingfriends.com/index.php/2010/03/11/join-files-together-into-a-single-file-and-expand-out/</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/11/join-files-together-into-a-single-file-and-expand-out/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 11:24:42 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[image reader]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[writer file]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=798</guid>
		<description><![CDATA[Again as with the previous post (simple calculator) this was another question to answer, which was fun to do.
&#8220;Write a class containing 2 functions. One function to merge 2 or more images into a single binary file. Write a counterpart function to this,which can extract the original files from the single binary, and write them [...]]]></description>
			<content:encoded><![CDATA[<p>Again as with the previous post (<a href="http://www.codingfriends.com/index.php/2010/03/11/simple-calculator/">simple calculator</a>) this was another question to answer, which was fun to do.</p>
<p>&#8220;Write a class containing 2 functions. One function to merge 2 or more images into a single binary file. Write a counterpart function to this,which can extract the original files from the single binary, and write them out as single images, using their original filenames. Compression is not necessary, as the exercise is only designed to show an<br />
understanding of using binary files with PHP, filesystem error handling, and planning a simple file format.&#8221;</p>
<p>Since we needed to have a simply file structure, so I decided to have a file structure of</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Filename<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>filename<span style="color: #339933;">&gt;</span>
Size<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>size in bytes<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>data<span style="color: #339933;">&gt;</span></pre></div></div>

<p>where the filename, size in bytes and data (of the file itself) is filled in when the program runs.</p>
<p>So to start with I created a directory structure to pick up files from within one directory and place into another</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>images
<span style="color: #000000; font-weight: bold;">/</span>newimages</pre></div></div>

<p>and placed some images into the images directory.</p>
<p>To scan within a directory there is a iterator within PHP called <a href="http://uk2.php.net/directoryiterator">DirectoryIterator</a> which will do what it says on the tin, it will give you files within a directory and since being a iterator you can use a <a href="http://uk2.php.net/manual/en/control-structures.foreach.php">foreach</a> to go through each file.  Here is the syntax for DirectoryIterator to loop with a foreach (the $fileDirectory is the directory to scan and the $fileDetails is the files that are within the directory.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DirectoryIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileDirectory</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$fileDetails</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>So what happens is that the program will loop through each file in the directory and open the single to write to, whilst looping through directory output the Filename to the single file and the Filesize, then read the actual file into a object to write to the single file within a binary format, and loop until no more files in that directory.</p>
<p>Then to un-single the file, I read the single file and find out the filename and filesize, open a file to write to in the new directory and read from the single file bits with the size of the filesize information from the single file, this was the actual file data, place this file data into the new file within the new directory.  (kinder how <a href="http://en.wikipedia.org/wiki/Tar_%28file_format%29">tar</a> works, builds all files into a single file from a directory and then expand then afterwards).</p>
<p>Here is the full code, it is small enough to hopefully follow.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #000000; font-weight: bold;">class</span> imageBuilder 
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* read files into one file */</span>
    <span style="color: #666666; font-style: italic;">/* file format 
      Filename: &lt;filename&gt;
      Size: &lt;size in bytes&gt;
      &lt;data&gt;
    */</span>
    <span style="color: #000000; font-weight: bold;">function</span> readFiles<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileDirectory</span><span style="color: #339933;">,</span><span style="color: #000088;">$outputName</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$fHandle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$outputName</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;wb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fHandle</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error open file to write to&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DirectoryIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileDirectory</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$fileDetails</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$filesize</span> <span style="color: #339933;">=</span> <span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileDirectory</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$fileDetails</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$filesize</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$fileDetails</span> <span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;..&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  <span style="color: #000088;">$rHandle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileDirectory</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$fileDetails</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;rb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error open file to read from&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #000088;">$readInFile</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #339933;">,</span><span style="color: #000088;">$filesize</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$readInFile</span><span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error on reading file to read from&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fHandle</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Filename: <span style="color: #006699; font-weight: bold;">$fileDetails</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error writting data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fHandle</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Size : <span style="color: #006699; font-weight: bold;">$filesize</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error writting data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fHandle</span><span style="color: #339933;">,</span><span style="color: #000088;">$readInFile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error writting data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* write files back out from the single file*/</span>
    <span style="color: #000000; font-weight: bold;">function</span> writeFiles<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileDriectoryToWrite</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fileToReadFrom</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$rHandle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileToReadFrom</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;rb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error opening file to read&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error reading the filename from file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$filesize</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$filesize</span> <span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error reading the filesize from the file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$filesize</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filesize</span><span style="color: #339933;">,</span> <span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filesize</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$readFileInfo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filesize</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$readFileInfo</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error reading data from file to read from&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$fHandle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileDriectoryToWrite</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;wb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fHandle</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error opening writing to file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fHandle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$readFileInfo</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error writing to file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000088;">$imageB</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> imageBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  try <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$imageB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">readFiles</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;images&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;filetogether&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$ex</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Error building the one file : <span style="color: #006699; font-weight: bold;">$ex</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  try <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$imageB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">writeFiles</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;newimages&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;filetogether&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$ex</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Error writing files back out : <span style="color: #006699; font-weight: bold;">$ex</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>and you need to do is place some files within the /images directory and then run the program and it will create a single file called &#8220;filetogether&#8221; and also expand that file into the /newimages directory.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/11/join-files-together-into-a-single-file-and-expand-out/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple calculator</title>
		<link>http://www.codingfriends.com/index.php/2010/03/11/simple-calculator/</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/11/simple-calculator/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 11:10:30 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[calculator]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[simple]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=796</guid>
		<description><![CDATA[Within a interview they requested me to do some exercises at home and here is the question that they posed.
&#8220;Create a simple stack-based calculator. This should be capable of performing addition, subtraction, multiplication and division. Example input would be: &#8220;2*(3+1)&#8221;. The result should be expressed as a numeric value, so the example should return 8. [...]]]></description>
			<content:encoded><![CDATA[<p>Within a interview they requested me to do some exercises at home and here is the question that they posed.</p>
<p>&#8220;Create a simple stack-based calculator. This should be capable of performing addition, subtraction, multiplication and division. Example input would be: &#8220;2*(3+1)&#8221;. The result should be expressed as a numeric value, so the example should return 8. Usage of the eval() function, although amusing, is not allowed! This test is designed to show general programming skills, rather than an understanding of the programming platform; however the solution should be demonstrated using php as the language.&#8221;</p>
<p>So I decided to post on this site as well the design and program that I did come up with.</p>
<p>Since in maths you have to equate the ( .. ) first and then * ,  / , + ,  &#8211; so to start with I created a function that will pull out the brackets from the string input</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">/* need to find the brackets and then equate the value and build outwards */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> equate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
       <span style="color: #666666; font-style: italic;">// this is the string that was inputted when the class was created ( $this-&gt;calcstr;)</span>
	<span style="color: #000088;">$theStr</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calcstr</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$bracketI</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// there is a bracket</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bracketI</span> <span style="color: #339933;">&gt;=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  <span style="color: #666666; font-style: italic;">// find a bracket to match</span>
	  <span style="color: #000088;">$bracketEnd</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bracketI</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bracketEnd</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	  <span style="color: #009900;">&#123;</span>
	    <span style="color: #666666; font-style: italic;">// match then get the internal bracket value</span>
	    <span style="color: #000088;">$EqualMiddle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bracketI</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bracketEnd</span><span style="color: #339933;">-</span><span style="color: #000088;">$bracketI</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #000088;">$calMiddle</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calculateTheString</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$EqualMiddle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #666666; font-style: italic;">// rebuild the string without the bracket but with the value</span>
	    <span style="color: #000088;">$theStr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bracketI</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$calMiddle</span><span style="color: #339933;">.</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bracketEnd</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #009900;">&#125;</span>
	  <span style="color: #b1b100;">else</span>
	    throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bracket miss matched&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	  <span style="color: #000088;">$bracketI</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bracketI</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$theStr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calculateTheString</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>So what happens is that it will find the last occurrence of a bracket &#8220;(&#8221; and then find the corresponding &#8220;)&#8221; for that bracket, error is not found.  Once found, it will then send to a function called &#8220;calculateTheString&#8221; that will actually calculate the value.</p>
<p>The calculateTheString function</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">private</span> function calculateTheString<span style="color: #008000;">&#40;</span>$theStr<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">// look for * / + - in that order and then get the number each side to do the math</span>
	$mathStr <span style="color: #000080;">=</span> array<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;*&quot;</span>, <span style="color: #FF0000;">&quot;/&quot;</span>, <span style="color: #FF0000;">&quot;+&quot;</span>, <span style="color: #FF0000;">&quot;-&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	foreach <span style="color: #008000;">&#40;</span>$mathStr as $maths<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
	  <span style="color: #0000ff;">do</span> 
	  <span style="color: #008000;">&#123;</span>
	    <span style="color: #666666;">// find each occurence of the math func</span>
	    $mathI <span style="color: #000080;">=</span> strpos<span style="color: #008000;">&#40;</span>$theStr, $maths<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>$mathI <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
	    <span style="color: #008000;">&#123;</span>
	      <span style="color: #666666;">// find the numeric values before and after the math func</span>
	      <span style="color: #0000ff;">try</span> <span style="color: #008000;">&#123;</span>
		$valueBefore <span style="color: #000080;">=</span> $this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getNumbericValue<span style="color: #008000;">&#40;</span>substr<span style="color: #008000;">&#40;</span>$theStr, <span style="color: #0000dd;">0</span>,$mathI<span style="color: #008000;">&#41;</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		$valueAfter <span style="color: #000080;">=</span>  $this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getNumbericValue<span style="color: #008000;">&#40;</span>substr<span style="color: #008000;">&#40;</span>$theStr, $mathI<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	      <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">catch</span> <span style="color: #008000;">&#40;</span>Exception $ex<span style="color: #008000;">&#41;</span>
	      <span style="color: #008000;">&#123;</span>
		echo <span style="color: #FF0000;">&quot;Error : $ex (String = $theStr)&quot;</span><span style="color: #008080;">;</span>
	      <span style="color: #008000;">&#125;</span>
	      <span style="color: #666666;">// do the math func</span>
	      <span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>$maths<span style="color: #008000;">&#41;</span>
	      <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">&quot;*&quot;</span> <span style="color: #008080;">:</span> $newV <span style="color: #000080;">=</span> $valueBefore <span style="color: #000040;">*</span> $valueAfter<span style="color: #008080;">;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">&quot;/&quot;</span> <span style="color: #008080;">:</span> $newV <span style="color: #000080;">=</span> $valueBefore <span style="color: #000040;">/</span> $valueAfter<span style="color: #008080;">;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">&quot;+&quot;</span> <span style="color: #008080;">:</span> $newV <span style="color: #000080;">=</span>$valueBefore <span style="color: #000040;">+</span> $valueAfter<span style="color: #008080;">;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">&quot;-&quot;</span> <span style="color: #008080;">:</span> $newV <span style="color: #000080;">=</span>$valueBefore <span style="color: #000040;">-</span> $valueAfter<span style="color: #008080;">;;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">default</span> <span style="color: #008080;">:</span> $newV <span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	      <span style="color: #008000;">&#125;</span>
	      <span style="color: #666666;">// rebuild string without the math func that is done</span>
	      $theStr <span style="color: #000080;">=</span> substr<span style="color: #008000;">&#40;</span>$theStr,<span style="color: #0000dd;">0</span>,<span style="color: #008000;">&#40;</span>$mathI<span style="color: #000040;">-</span><span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>$valueBefore<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> . <span style="color: #008000;">&#40;</span>$newV<span style="color: #008000;">&#41;</span> . <span style="color: #007788;">substr</span><span style="color: #008000;">&#40;</span>$theStr,<span style="color: #008000;">&#40;</span>$mathI<span style="color: #000040;">+</span><span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>$valueAfter<span style="color: #008000;">&#41;</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	    <span style="color: #008000;">&#125;</span>
	  <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>$mathI <span style="color: #000080;">&gt;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> $theStr<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>will loop through the math functions (*,/,+,-) in order of calculation, if it finds one of them it will equate what the value of that block of math is e.g.</p>
<pre lang="c++">
4*2+1
</pre>
<p>it would equate the 4*2 and then rebuild the string to have the result within the next loop e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">8</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span></pre></div></div>

<p>so that when it gets to the + math function it will add 8 to 1.</p>
<p>To get the number values before and after the math functions (*,/,+,-) I created a function to pull out the values called &#8220;getNumbericValue&#8221;.. since the values will either be before or after the e.g. going backwards or forwards in the string, then you will need to either go in that direction.  Once you have got the area within the string of the numeric values, then use the intval to convert into a integer value.  The is_numeric makes sure that there is still a numeric value present.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* $theStr = string to search, leftOrRight means going left or right in the string, 0 = left, 1=right*/</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getNumbericValue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$leftOrRight</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No number value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$leftOrRight</span> <span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> 
	  <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span> 
	<span style="color: #009900;">&#123;</span>
	  <span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	  <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
 	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$pos</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	  <span style="color: #009900;">&#123;</span>
	    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$leftOrRight</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$pos</span><span style="color: #339933;">--;</span> <span style="color: #b1b100;">else</span> <span style="color: #000088;">$pos</span><span style="color: #339933;">++;</span>
	  <span style="color: #009900;">&#125;</span>
	    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// if just the number at the start</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pos</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// if the start is greater than the end point, change over</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pos</span><span style="color: #339933;">;</span> <span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start</span><span style="color: #339933;">;</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span><span style="color: #000088;">$start</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>To build all that together with</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #000000; font-weight: bold;">class</span> calc 
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$calcstr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$strIn</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calcstr</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$strIn</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setStr<span style="color: #009900;">&#40;</span><span style="color: #000088;">$strIn</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calcstr</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$strIn</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* $theStr = string to search, leftOrRight means going left or right in the string, 0 = left, 1=right*/</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getNumbericValue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$leftOrRight</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No number value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$leftOrRight</span> <span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> 
	  <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span> 
	<span style="color: #009900;">&#123;</span>
	  <span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	  <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
 	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$pos</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	  <span style="color: #009900;">&#123;</span>
	    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$leftOrRight</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$pos</span><span style="color: #339933;">--;</span> <span style="color: #b1b100;">else</span> <span style="color: #000088;">$pos</span><span style="color: #339933;">++;</span>
	  <span style="color: #009900;">&#125;</span>
	    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// if just the number at the start</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pos</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// if the start is greater than the end point, change over</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pos</span><span style="color: #339933;">;</span> <span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start</span><span style="color: #339933;">;</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span><span style="color: #000088;">$start</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> calculateTheString<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// look for * / + - in that order and then get the number each side to do the math</span>
	<span style="color: #000088;">$mathStr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;*&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;+&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;-&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$mathStr</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$maths</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  <span style="color: #b1b100;">do</span> 
	  <span style="color: #009900;">&#123;</span>
	    <span style="color: #666666; font-style: italic;">// find each occurence of the math func</span>
	    <span style="color: #000088;">$mathI</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maths</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$mathI</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	    <span style="color: #009900;">&#123;</span>
	      <span style="color: #666666; font-style: italic;">// find the numeric values before and after the math func</span>
	      try <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$valueBefore</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumbericValue</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$mathI</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$valueAfter</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumbericValue</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mathI</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	      <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$ex</span><span style="color: #009900;">&#41;</span>
	      <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Error : <span style="color: #006699; font-weight: bold;">$ex</span> (String = <span style="color: #006699; font-weight: bold;">$theStr</span>)&quot;</span><span style="color: #339933;">;</span>
	      <span style="color: #009900;">&#125;</span>
	      <span style="color: #666666; font-style: italic;">// do the math func</span>
	      <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$maths</span><span style="color: #009900;">&#41;</span>
	      <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;*&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$newV</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$valueBefore</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$valueAfter</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$newV</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$valueBefore</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$valueAfter</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;+&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$newV</span> <span style="color: #339933;">=</span><span style="color: #000088;">$valueBefore</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$valueAfter</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;-&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$newV</span> <span style="color: #339933;">=</span><span style="color: #000088;">$valueBefore</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$valueAfter</span><span style="color: #339933;">;;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">default</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$newV</span> <span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	      <span style="color: #009900;">&#125;</span>
	      <span style="color: #666666; font-style: italic;">// rebuild string without the math func that is done</span>
	      <span style="color: #000088;">$theStr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mathI</span><span style="color: #339933;">-</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$valueBefore</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$newV</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mathI</span><span style="color: #339933;">+</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$valueAfter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span>
	  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$mathI</span> <span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$theStr</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* need to find the brackets and then equate the value and build outwards */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> equate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$theStr</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calcstr</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$bracketI</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// there is a bracket</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bracketI</span> <span style="color: #339933;">&gt;=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  <span style="color: #666666; font-style: italic;">// find a bracket to match</span>
	  <span style="color: #000088;">$bracketEnd</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bracketI</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bracketEnd</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	  <span style="color: #009900;">&#123;</span>
	    <span style="color: #666666; font-style: italic;">// match then get the internal bracket value</span>
	    <span style="color: #000088;">$EqualMiddle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bracketI</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bracketEnd</span><span style="color: #339933;">-</span><span style="color: #000088;">$bracketI</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #000088;">$calMiddle</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calculateTheString</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$EqualMiddle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #666666; font-style: italic;">// rebuild the string without the bracket but with the value</span>
	    <span style="color: #000088;">$theStr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bracketI</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$calMiddle</span><span style="color: #339933;">.</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bracketEnd</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #009900;">&#125;</span>
	  <span style="color: #b1b100;">else</span>
	    throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bracket miss matched&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	  <span style="color: #000088;">$bracketI</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bracketI</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$theStr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calculateTheString</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000088;">$theequal</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;2*(3+1)&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$thecalc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> calc<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theequal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  try <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$thecalc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">equate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$err</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>	
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$err</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>and the output would be</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">8</span></pre></div></div>

<p>I found it a interesting exercise <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/11/simple-calculator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AJAX &#8211; setup and test</title>
		<link>http://www.codingfriends.com/index.php/2010/03/08/ajax-setup-and-test/</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/08/ajax-setup-and-test/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 09:53:22 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[HTML  & CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[setup and test]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=782</guid>
		<description><![CDATA[AJAX is the way of communicating with a back end server without having to send the full information (you can of course) but for example you could just send a username check to see if it is available, but the main thing is that you do not send back a full page but only the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Ajax_%28programming%29">AJAX</a> is the way of communicating with a back end server without having to send the full information (you can of course) but for example you could just send a username check to see if it is available, but the main thing is that you do not send back a full page but only the part that you want to update.  </p>
<p>With reference to the example of a username, you could just send the username and send back either yes or no response which saves allot of time and traffic from the client to the server (and makes the whole web page experience nicer). </p>
<p>All the AJAX is shorthand for &#8220;Asynchronous JAvascript and Xml&#8221;, asynchronous means that you can do something else whilst waiting for the response (put the kettle on and get a cup for the drink whilst the kettle is boiling) thus with javascript on the client web browser sends a request to a web page on the server with XML wrappings. </p>
<p>To get the basics lets start with the being</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// this function will return a XmlHttpRequest object that allows you to &quot;talk&quot; to the server.</span>
  <span style="color: #003366; font-weight: bold;">function</span> GetXmlHttpObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// IE7+, FF, Chrome, Opera, Safari</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">XMLHttpRequest</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// IE6 , IE5</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">ActiveXObject</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Microsoft.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>the GetXmlHttpObject will return a object that will allow the javascript to talk to the backend server, the newer version is call a XMLHttpRequest whilst on older browsers it was part of the ActiveXObjects.</p>
<p>The next is to send a request to the backend server</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    xmlHttpObject.<span style="color: #660066;">onreadystatechange</span><span style="color: #339933;">=</span>callBackFunction<span style="color: #339933;">;</span>
    xmlHttpObject.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> GetUrl<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    xmlHttpObject.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The onreadystatechange, will call a function (in javascript on the client browser) when the request alters from different states, the different states are </p>
<ul>
<li>1.open method invoked successful, open a connection with the server</li>
<li>2.server responsed with a valid header response.</li>
<li>3.server has sent some data, the response content is started to load.</li>
<li>4.server has finished sending all of data</li>
</ul>
<p>so from reading the states, you are really interested in state 4,  because that will have the data (server response) that you are interested in for this.</p>
<p>The .open forms the request to the XmlHttpRequest object to call (&#8221;GET&#8221; in HTML) the server web site, the GetUrl is just a variable that well call a php page (&#8221;ajaxbackendcall.php&#8221;) which takes a parameter called name and returns a string with the name in reverse (shall include that source code later).</p>
<p>And then the .send will start the ready states to change and sends the request to the backend server, here is the function that is called on the state change</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> callBackFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlHttpObject.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>xmlHttpObject.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>What is happening here, is that from the state stages I am waiting on ready state to equal 4 ( when the server has finished responding) and then output the response from the responseText which is filled from the AJAX call to the backend server.</p>
<p>That is mainly it, here is some full code for you to try out save this as &#8220;codingfriends.com.ajax.test.html&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html3/strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #006600; font-style: italic;">// the xmlHttpRequest object</span>
  <span style="color: #003366; font-weight: bold;">var</span> xmlHttpObject<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// the ajax call </span>
  <span style="color: #003366; font-weight: bold;">function</span> getInnerText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// get the name to send to the server backend page</span>
    <span style="color: #003366; font-weight: bold;">var</span> namehere <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;namehere&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// request the xmlHttpObject </span>
    xmlHttpObject <span style="color: #339933;">=</span> GetXmlHttpObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// if there is not xmlHttpRequest object being allowed to be created then the browser does not support it.</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlHttpObject<span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Browser does not support XmlHttp calls e.g. AJAX&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// fill in the request details, e.g. the url to call and also the query string inserted into the url</span>
    <span style="color: #003366; font-weight: bold;">var</span> GetUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;ajaxbackendcall.php&quot;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;?name=&quot;</span><span style="color: #339933;">+</span>namehere.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// here is the call to the server.</span>
    <span style="color: #006600; font-style: italic;">// to start with setup which function to call when a ready state on the XmlHttpRequest object has changed</span>
    xmlHttpObject.<span style="color: #660066;">onreadystatechange</span><span style="color: #339933;">=</span>callBackFunction<span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// request the html &quot;GET&quot; to obtain the url (e.g. the backend server page, dynmaic normally)</span>
    xmlHttpObject.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> GetUrl<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// and then issue it</span>
    xmlHttpObject.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// the function that is called once the XmlHttpRequest object state has changed</span>
  <span style="color: #006600; font-style: italic;">/* the readyStates are 
    1 open method invoked successful, open a connection with the server
    2 server responsed with a valid header response.
    3 server has sent some data, the response content is started to load.
    4 server has finished sending all of data
  */</span>
  <span style="color: #006600; font-style: italic;">// so we listen for readyState 4 when all finished</span>
  <span style="color: #006600; font-style: italic;">// and output the response text into the div id innertextoutput</span>
  <span style="color: #003366; font-weight: bold;">function</span> callBackFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlHttpObject.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span>
      document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;innertextoutput&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span>xmlHttpObject.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// this function will return a XmlHttpRequest object that allows you to &quot;talk&quot; to the server.</span>
  <span style="color: #003366; font-weight: bold;">function</span> GetXmlHttpObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// IE7+, FF, Chrome, Opera, Safari</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">XMLHttpRequest</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// IE6 , IE5</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">ActiveXObject</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Microsoft.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&lt;/head&gt;
&lt;body&gt;
&lt;a href=&quot;#&quot; onclick=&quot;javascript:getInnerText()&quot;&gt;click here to return text from a ajax call&lt;/a&gt;
&lt;br/&gt;
Enter the name here &lt;input id=&quot;namehere&quot;/&gt;
&lt;br/&gt;
&lt;div id=&quot;innertextoutput&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>and then save this as the ajaxbackendcall.php file to call (in the same directory as the code above and also within a directory that has PHP plugin enable for that webserver be it apache or IIS.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// change the output to say something else, here I am just reversing the name</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// have to insert something into it so that php does not make it into a array but a string</span>
      <span style="color: #000088;">$namereturn</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$namereturn</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;normal name <span style="color: #006699; font-weight: bold;">{$name}</span> and in reverse <span style="color: #006699; font-weight: bold;">{$namereturn}</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;No name inputted&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The output would be similar to the below. OUTPUT<br />
<script type="text/javascript">
  // the xmlHttpRequest object
  var xmlHttpObject;  // the ajax call 
  function getInnerText()
  {
    // get the name to send to the server backend page
    var namehere = document.getElementById("namehere");    
    // request the xmlHttpObject 
    xmlHttpObject = GetXmlHttpObject();    
    // if there is not xmlHttpRequest object being allowed to be created then the browser does not support it.
    if (xmlHttpObject==null)
    {
      alert("Browser does not support XmlHttp calls e.g. AJAX");
      return;
    }     
    // fill in the request details, e.g. the url to call and also the query string inserted into the url
    var GetUrl = "/phptest/ajaxbackendcall.php"+"?name="+namehere.value;
    // here is the call to the server.
    // to start with setup which function to call when a ready state on the XmlHttpRequest object has changed
    xmlHttpObject.onreadystatechange=callBackFunction;
    // request the html "GET" to obtain the url (e.g. the backend server page, dynmaic normally)
    xmlHttpObject.open("GET", GetUrl, true);
    // and then issue it
    xmlHttpObject.send(null);
  }
  // the function that is called once the XmlHttpRequest object state has changed
  /* the readyStates are 
    1 open method invoked successful, open a connection with the server
    2 server responsed with a valid header response.
    3 server has sent some data, the response content is started to load.
    4 server has finished sending all of data
  */
  // so we listen for readyState 4 when all finished
  // and output the response text into the div id innertextoutput
  function callBackFunction()
  {
    if (xmlHttpObject.readyState == 4)
      document.getElementById("innertextoutput").innerHTML=xmlHttpObject.responseText;
  }  
  // this function will return a XmlHttpRequest object that allows you to "talk" to the server.
  function GetXmlHttpObject()
  {
      // IE7+, FF, Chrome, Opera, Safari
      if (window.XMLHttpRequest) return new XMLHttpRequest;      
      // IE6 , IE5
      if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
  }
</script><br />
<a onclick="javascript:getInnerText()">click here to return text from a ajax call</a><br />
Enter the name here<br />
<input id="namehere"/>
<div id="innertextoutput"></div>
<p>END OF OUTPUT</p>
<p>If you do not have a PHP webserver to test with, you can just alter the codingfriends.com.ajax.test.html code by altering the backend web page to call to this</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> GetUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;nonephpbackend.html&quot;</span></pre></div></div>

<p>and create a page within the same directory as the codingfriends.com.ajax.test.html page and place something inside it like</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">hi there</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/08/ajax-setup-and-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>struct &#8211; initialization values</title>
		<link>http://www.codingfriends.com/index.php/2010/03/05/struct-initialization-values/</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/05/struct-initialization-values/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 09:58:15 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[initialization]]></category>
		<category><![CDATA[struct]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=780</guid>
		<description><![CDATA[When you define a new struct(ure) and initialize it you can use the {} to define the internals of the values within for example

typedef struct
&#123;
    int coding;
    int friends;
&#125; newType;
&#160;
newType nT = &#123; 10, 20&#125;;

so coding = 10 and friends = 20
But if you try and over initialize the [...]]]></description>
			<content:encoded><![CDATA[<p>When you define a new <a href="http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29">struct</a>(ure) and initialize it you can use the {} to define the internals of the values within for example</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">struct</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> coding<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> friends<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> newType<span style="color: #008080;">;</span>
&nbsp;
newType nT <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">10</span>, <span style="color: #0000dd;">20</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>so coding = 10 and friends = 20</p>
<p>But if you try and over initialize the values that are not present, e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">newType nT <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">10</span>, <span style="color: #0000dd;">20</span>, <span style="color: #0000dd;">30</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// there is not place to put the 30</span></pre></div></div>

<p>then the compiler will compile with</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">error: too many initializers <span style="color: #000000; font-weight: bold;">for</span> ‘newType<span style="color: #ff0000;">'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/05/struct-initialization-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>memcpy &#8211; copy the memory contents from one place to another</title>
		<link>http://www.codingfriends.com/index.php/2010/03/05/memcpy-copy-the-memory-contents-from-one-place-to-another/</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/05/memcpy-copy-the-memory-contents-from-one-place-to-another/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 09:52:47 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[memcpy]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=778</guid>
		<description><![CDATA[The memcpy function, does not really care about the parameters passed in, if they are the same types or of different types.  Just that they are variables already declared (and of course can hold the data being passed from one place to another, because otherwise there may be a slight overlap if the copied [...]]]></description>
			<content:encoded><![CDATA[<p>The memcpy function, does not really care about the parameters passed in, if they are the same types or of different types.  Just that they are variables already declared (and of course can hold the data being passed from one place to another, because otherwise there may be a slight overlap if the copied value is larger in memory size to the copier).</p>
<p>So the memcpy is just a binary data copy and nothing else, there is no type checking. </p>
<p>Here is a example in c++ code that will copy a newly created struct(ure) from one place to another.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> <span style="color: #0000dd;">memcpy</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>copied_to, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>copier, <span style="color: #0000ff;">size_t</span> num<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>where the return is also the copied_to value, since we are not altering the copier then a const(ant) can be applied and the last parameter is num which is the size of the data you want to copy.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #339900;">#include &lt;string.h&gt;</span>
&nbsp;
<span style="color: #666666;">// diferent type of object, e.g. not a int,char</span>
<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">struct</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> bob<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> insertType<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// create two variables of insertType with default values for bob of 10 and 20</span>
  insertType t1 <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">10</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
  insertType t2 <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">20</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// output the declared values</span>
  <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;t1 = %d<span style="color: #000099; font-weight: bold;">\n</span>t2 = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, t1.<span style="color: #007788;">bob</span>, t2.<span style="color: #007788;">bob</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// do a memory copy of t2 into t1 only copy as big as the variable size.</span>
  <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>t1, <span style="color: #000040;">&amp;</span>t2, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>insertType<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// output the new values</span>
  <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;t1 = %d<span style="color: #000099; font-weight: bold;">\n</span>t2 = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, t1.<span style="color: #007788;">bob</span>, t2.<span style="color: #007788;">bob</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>here is the output</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">t1 = <span style="color: #000000;">10</span>
t2 = <span style="color: #000000;">20</span>
t1 = <span style="color: #000000;">20</span>
t2 = <span style="color: #000000;">20</span></pre></div></div>

<p>also note that the size_t is a unsigned integral type (normal of int, of the base operating system)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/05/memcpy-copy-the-memory-contents-from-one-place-to-another/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>qt &#8211; emit a signal</title>
		<link>http://www.codingfriends.com/index.php/2010/03/04/qt-emit-a-signal/</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/04/qt-emit-a-signal/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 21:30:48 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[emit]]></category>
		<category><![CDATA[QT]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=775</guid>
		<description><![CDATA[When you trying to link the slots and signals together you need to have a QObject::connect and also a emitting, I have created a basic demonstration of this with a QPushButton (link to qt signal and slots, and a QT link for the signal and slots)
The basics of slot(s) and signal(s) with emit(ting) is that [...]]]></description>
			<content:encoded><![CDATA[<p>When you trying to link the slots and signals together you need to have a QObject::connect and also a emitting, I have created a basic demonstration of this with a QPushButton (<a href="http://www.codingfriends.com/index.php/2010/02/24/qt-hello-world-signals-and-slots/">link to qt signal and slots</a>, and a <a href="http://doc.trolltech.com/4.5/signalsandslots.html">QT link for the signal and slots</a>)</p>
<p>The basics of slot(s) and signal(s) with emit(ting) is that a slot is where the emit(ed) signal goes to, when a signal is emit(ed) via a class.  For example if you want to link a value changed event then you would emit a signal from within the class and then use that signal with the QObject::connect to link to a slot within class as well.</p>
<p>I have created a class called EmitterTest that has a function within it called</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">        <span style="color: #0000ff;">void</span> setValueAndEmit<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> v<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>this function will set the value and emit the signal for any QObject::connect to link to a slot.  To emit the signal you just</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">        emit valueChanged<span style="color: #008000;">&#40;</span>v<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>where the valueChanged is the signal within the class definition (you do not define that function because it is kinder like a virtual function).</p>
<p>To allow for these slots and signals to be defined within the class you need to call the macro Q_OBJECT at the top of the class (within a private area) so that all of the necessary attachments to the class can be created (within the moc file associated with the class when you run the qmake later on). </p>
<p>To define a signal and slot there is keywords within the &#8220;new&#8221; class structure available because of the Q_OBJECT and you can just use them like public and private declarations</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #0000ff;">public</span> slots <span style="color: #008080;">:</span>
        <span style="color: #0000ff;">void</span> setValue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> v<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    signals<span style="color: #008080;">:</span>
        <span style="color: #0000ff;">void</span> valueChanged<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> newV<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>signals are always public in there access rights.</p>
<p>What the code below does is to create a slot and signal with a function that will emit a signal when called (the function that is) so the signal is &#8220;fired&#8221; off to what ever is listening to it, to connect a signal to a slot you use the QObject::connect as below</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    EmitterTest em1, em2<span style="color: #008080;">;</span>
&nbsp;
    QObject<span style="color: #008080;">::</span><span style="color: #007788;">connect</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>em1, SIGNAL<span style="color: #008000;">&#40;</span>valueChanged<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,
                     <span style="color: #000040;">&amp;</span>em2, SLOT<span style="color: #008000;">&#40;</span>setValue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>where the EmitterTest is the class that I am creating, and the object em1 signal valueChanged is linked to the same class type but different instance of it, em2 slot setValue.</p>
<p>Here is the code below for the full EmitterTest, here is the header file for the emitting test class, if you save as emitting.h</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#ifndef EMITTING_H</span>
<span style="color: #339900;">#define EMITTING_H</span>
&nbsp;
<span style="color: #339900;">#include &lt;QObject&gt;</span>
&nbsp;
<span style="color: #0000ff;">class</span> EmitterTest <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> QObject
<span style="color: #008000;">&#123;</span>
    Q_OBJECT
&nbsp;
    <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
        EmitterTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> mem_value <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0000ff;">int</span> getValue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> mem_value<span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">void</span> setValueAndEmit<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> v<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// these pick up the emitted signal</span>
    <span style="color: #0000ff;">public</span> slots <span style="color: #008080;">:</span>
        <span style="color: #0000ff;">void</span> setValue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> v<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// these are what are sent out over emitted signals</span>
    <span style="color: #666666;">// you do not implement the signals since they are just virtual functions</span>
    <span style="color: #666666;">// as such that you call with emit then the value within the parameter(s)</span>
    <span style="color: #666666;">// is passed to the slot.</span>
    signals<span style="color: #008080;">:</span>
        <span style="color: #0000ff;">void</span> valueChanged<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> newV<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
        <span style="color: #0000ff;">int</span> mem_value<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#endif // EMITTING_H</span></pre></div></div>

<p>and here is the emitting.cpp file, that holds the main runtime main function.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;QObject&gt;</span>
<span style="color: #339900;">#include &lt;QString&gt;</span>
<span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #339900;">#include &quot;emitting.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">void</span> EmitterTest<span style="color: #008080;">::</span><span style="color: #007788;">setValue</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> v<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// if the value has changed</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;The new value for %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, v<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    mem_value <span style="color: #000080;">=</span> v<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> EmitterTest<span style="color: #008080;">::</span><span style="color: #007788;">setValueAndEmit</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> v<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// if the value has changed</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>v <span style="color: #000040;">!</span><span style="color: #000080;">=</span> mem_value<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        mem_value <span style="color: #000080;">=</span> v<span style="color: #008080;">;</span>
        emit valueChanged<span style="color: #008000;">&#40;</span>v<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    EmitterTest em1, em2<span style="color: #008080;">;</span>
&nbsp;
    QObject<span style="color: #008080;">::</span><span style="color: #007788;">connect</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>em1, SIGNAL<span style="color: #008000;">&#40;</span>valueChanged<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,
                     <span style="color: #000040;">&amp;</span>em2, SLOT<span style="color: #008000;">&#40;</span>setValue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    em1.<span style="color: #007788;">setValueAndEmit</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;The connection from the object connection em1 %d should equal em2 %d <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, em1.<span style="color: #007788;">getValue</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, em2.<span style="color: #007788;">getValue</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    em2.<span style="color: #007788;">setValueAndEmit</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">15</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;The connection from the object connection em1 %d should NOT equal em2 %d <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, em1.<span style="color: #007788;">getValue</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, em2.<span style="color: #007788;">getValue</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// the values are not the same because I have not connected them even though I have called the emit within the</span>
    <span style="color: #666666;">// setValueAndEmit</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>if you save them into a directory and then to compile you need to have the <a href="http://doc.trolltech.com/4.5/qmake-manual.html">qmake</a> and also make (<a href="http://msdn.microsoft.com/en-us/library/dd9y37ha%28VS.71%29.aspx">nmake</a> for windows) to make the relevant files to build a qt project.  The qmake project creates the necessary .pro file to build the project, the qmake will build the necessary files (moc files) for the make (or nmake) to build the project into a executable file</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">qmake <span style="color: #660033;">-project</span>
qmake
<span style="color: #c20cb9; font-weight: bold;">make</span></pre></div></div>

<p>and then there will be a executable file within that directory and the output will be once you have run it</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">The new value <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">10</span>
The connection from the object connection em1 <span style="color: #000000;">10</span> should equal em2 <span style="color: #000000;">10</span>
The connection from the object connection em1 <span style="color: #000000;">10</span> should NOT equal em2 <span style="color: #000000;">15</span></pre></div></div>

<p>as you notice since I only connected em1 signal to em2, if you change em2 mem_value then em1 does not change as well because the em2 signal is not linked back to the em1 slot.</p>
<p>If you get this error when you are trying out any emit test</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">undefined reference to `vtable <span style="color: #0000ff;">for</span> <span style="color: #000040;">---</span> your <span style="color: #0000ff;">class</span> name</pre></div></div>

<p>, it is because for some reason if you use the Q_OBJECT you need to define the class within a .h (header file) and then it will compile instead if you try and use the same .cpp for the main source code then you will get this error message.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/04/qt-emit-a-signal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>hover over to show a hidden element</title>
		<link>http://www.codingfriends.com/index.php/2010/03/03/hover-over-to-show-a-hidden-element/</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/03/hover-over-to-show-a-hidden-element/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 10:46:28 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[HTML  & CSS]]></category>
		<category><![CDATA[css only]]></category>
		<category><![CDATA[hidden]]></category>
		<category><![CDATA[hide]]></category>
		<category><![CDATA[hover]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=764</guid>
		<description><![CDATA[Sometimes you may not have the luxury of having javascript to do some of the great things that it can do, like showing hidden content on the screen when you hover over a HTML element (like a A HTML tag for example).  In CSS you can achieve a similar result with using some of [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you may not have the luxury of having javascript to do some of the great things that it can do, like showing hidden content on the screen when you hover over a HTML element (like a A HTML tag for example).  In CSS you can achieve a similar result with using some of the overflow and styling elements of un/ordered lists to accomplish a similar result.</p>
<p>If you save this code as codingfriends_showhide.html and then open it up in <a href="http://www.mozilla-europe.org/en/firefox/">Firefox</a> or your browser of choice (not sure if it will work in IE6 shall have to check it !). and then just hover over the block and it will display the hidden message <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>CSS - show/hide on hover<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
/* the main ul block, there is no list style at all e.g. all together */
#showhideblock {
	list-style:none;
	margin:0;
	padding:0;
}
/* the main ul block elements push to the left and make there display inline e.g. no breaks */
#showhideblock li {
	float:left;
	width:200px;
	height:55px;
	display:inline;
	border:1px solid black; 
}
&nbsp;
/* the basic showing element of the list */
#showhideblock li .show{
	display:block;
	width:200px;
	height:55px;
}
/* hide the hiding element of the list with the height of 0 and overflow set to hidden */
/* but once shown display in black */
#showhideblock li .hide {
	color : black;
	height: 0; /* hide the .hide element */
	overflow: hidden;
	background:black;
}
/* when you hover over the shown element, set the hidden element sizes */
#showhideblock li:hover .hide, #showhideblock li.over .hide {
	height: 55px;
	width:  200px;
}
/* when you hover over the shown element set the shown element height and overflow values */
#showhideblock li:hover .show, #showhideblock li.over .show {
	height: 0;
	overflow: hidden;
}
&nbsp;
/* set the show a href links to colour of black (on the white background of the shown element */
/* and the textual size to be bigger than normal */
#showhideblock li a {
	color:black;
	font-size:1.5em;
}
/* since changing the background colour of the hidden element to black, change the on hover */
/* to have a textual colour of white */
#showhideblock li a:hover {
	color:white;
}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;showhideblock&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;show&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>  <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;The image to show&quot;</span>&gt;</span>Hover over me, what has been hidden ?<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hide&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;the hidden link&quot;</span>&gt;</span>Codingfriends - I was hidden here all along<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>In essence the main parts of the code are as follows, you need to set the unordered list to have a list style of none with the list elements (li) having a display of being inline (which means that there is no breaks between the elements).</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">#showhideblock {
	list-style:none;
...
#showhideblock li {
	display:inline;</pre></div></div>

<p>with this in place you can use the size of show and hide elements with also the overflow set to hidden (when you hover over the block you need to set the shown element overflow to hidden and set the size of the hidden element so that it shows)</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">#showhideblock li .hide {
	color : black;
	height: 0; /* hide the .hide element */
	overflow: hidden;</pre></div></div>

<p>And once you have saved the above file and opened up in your browser of choice when you now hover over the &#8220;Hover over me&#8221; text you will see the hidden text instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/03/hover-over-to-show-a-hidden-element/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>kernel &#8211; hello world</title>
		<link>http://www.codingfriends.com/index.php/2010/03/01/kernel-hello-world/</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/01/kernel-hello-world/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 10:25:39 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[Kernel]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=761</guid>
		<description><![CDATA[To compile up this module you will need to have the linux header files, which should be installed but if not you just need to either use apt-get, rpm, pacman etc to install the linux header files.
With kubuntu I use the aptitude command to install applications and updates etc, but to get the linux-headers I [...]]]></description>
			<content:encoded><![CDATA[<p>To compile up this module you will need to have the linux header files, which should be installed but if not you just need to either use apt-get, rpm, pacman etc to install the linux header files.</p>
<p>With <a href="http://www.kubuntu.org/">kubuntu</a> I use the aptitude command to install applications and updates etc, but to get the linux-headers I used</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> linux-headers</pre></div></div>

<p>that is normally linked to the latest version of the linux kernel that you are using.</p>
<p>The main part of the program is outputting a potential parameter being passed (passing parameters to a nvidia kernel module <a href="http://www.codingfriends.com/index.php/2010/02/27/kernel-passing-module-parameters/">here</a>) and also saying hello world, kinder two for the price of one as such tutorial. </p>
<p>As in c language there is the <a href="http://en.wikipedia.org/wiki/Printf">printf</a>, but in the kernel there is a printk which is the same similar thing but more for outputting errors/messages to the user.</p>
<p>So for example</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">printk<span style="color: #008000;">&#40;</span>KERN_INFO <span style="color: #FF0000;">&quot;hi there<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>the KERN_INFO is a kernel information marco that sends the details to a information level of output, e.g. /var/log/messages or you could use dmesg</p>
<p>here is the code on how to pull in parameters from a kernel module being loaded, the parameter_int is my parameter to be checked against and the S_IRUSR is the access rights (IR = read permission, IW = write permission, USR = user, GRP = group, OTH = others)</p>
<p>The module_param takes 3 parameters, the first is the variable to place the value into, second is the variable type and the third is the access rights, with the MODULE_PARM_DESC is the description of the parameter to pass, in this case it is just a integer value.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> parameter_int <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
module_param<span style="color: #008000;">&#40;</span>parameter_int, <span style="color: #0000ff;">int</span>, S_IRUSR <span style="color: #000040;">|</span> S_IWUSR <span style="color: #000040;">|</span> S_IRGRP <span style="color: #000040;">|</span> S_IROTH<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
MODULE_PARM_DESC<span style="color: #008000;">&#40;</span>parameter_int, <span style="color: #FF0000;">&quot;An integer value&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Within a normal c/c++ program there is a main function where the program is run from, but in the kernel space since the main program is already running you can define what functions to call when the module is inserted and also removed,</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">module_init<span style="color: #008000;">&#40;</span>hello_init<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
module_exit<span style="color: #008000;">&#40;</span>hello_exit<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>the module_init parameter is the function to call when the module is loaded and the module_exit parameter is the function to call when module is removed from the kernel space.</p>
<p>Here is the full code, if you save this as helloworld_parameter.c</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/* hello world with passing some parameters in the kernel module */</span>
&nbsp;
<span style="color: #339900;">#include &lt;linux/module.h&gt;</span>
<span style="color: #339900;">#include &lt;linux/moduleparam.h&gt;</span>
<span style="color: #339900;">#include &lt;linux/kernel.h&gt;</span>
<span style="color: #339900;">#include &lt;linux/init.h&gt;</span>
<span style="color: #339900;">#include &lt;linux/stat.h&gt;</span>
&nbsp;
MODULE_LICENSE<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;GPL&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
MODULE_AUTHOR<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;genux&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/* need to setup a setup a static variable to hold the parameter value and
   set it to a default value is none is passed */</span>
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> parameter_int <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/* the linux/stat.h has the S_IRUSR definitions etc.. */</span>
<span style="color: #ff0000; font-style: italic;">/* S_IRUSR = read permission, owner
   S_IWUSR = write permission, owner
   S_IRGRP = read permission, group
   S_IROTH = read permission, others
   */</span>
&nbsp;
module_param<span style="color: #008000;">&#40;</span>parameter_int, <span style="color: #0000ff;">int</span>, S_IRUSR <span style="color: #000040;">|</span> S_IWUSR <span style="color: #000040;">|</span> S_IRGRP <span style="color: #000040;">|</span> S_IROTH<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
MODULE_PARM_DESC<span style="color: #008000;">&#40;</span>parameter_int, <span style="color: #FF0000;">&quot;An integer value&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> __init hello_init<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	printk<span style="color: #008000;">&#40;</span>KERN_INFO <span style="color: #FF0000;">&quot;Hello world<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	printk<span style="color: #008000;">&#40;</span>KERN_INFO <span style="color: #FF0000;">&quot;my parameter int value is : %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, parameter_int<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> __exit hello_exit<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	printk<span style="color: #008000;">&#40;</span>KERN_INFO <span style="color: #FF0000;">&quot;Goodbye from hello world parameter<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
module_init<span style="color: #008000;">&#40;</span>hello_init<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
module_exit<span style="color: #008000;">&#40;</span>hello_exit<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>since we are compiling a kernel module we need to link to the loaded kernel modules, here is a Makefile to compile up the program, so save this as &#8220;Makefile&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">obj-m += helloworld_parameter.o
&nbsp;
all:
	<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #660033;">-C</span> <span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>modules<span style="color: #000000; font-weight: bold;">/</span>$<span style="color: #7a0874; font-weight: bold;">&#40;</span>shell <span style="color: #c20cb9; font-weight: bold;">uname</span> -r<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>build <span style="color: #007800;">M</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>PWD<span style="color: #7a0874; font-weight: bold;">&#41;</span> modules
&nbsp;
clean:
	<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #660033;">-C</span> <span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>modules<span style="color: #000000; font-weight: bold;">/</span>$<span style="color: #7a0874; font-weight: bold;">&#40;</span>shell <span style="color: #c20cb9; font-weight: bold;">uname</span> -r<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>build <span style="color: #007800;">M</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>PWD<span style="color: #7a0874; font-weight: bold;">&#41;</span> clean</pre></div></div>

<p>the first is the obj-m += which means compile a object module and you could have more than one file to compile up so use the += to add more files to it, the -C means change directory for the build environment for the kernel space, the M is a parameter passed to the build environment to use this current directory for where the source files are, and the modules means to create kernel modules e.g. filename.ko.</p>
<p>once you have run the</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">make</span></pre></div></div>

<p>there should be a file called helloworld_parameter.ko, to find out details about your new module you can use the modinfo</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">modinfo helloworld_parameter.ko
filename:       helloworld_parameter.ko
author:         genux
license:        GPL
srcversion:     A81F18D40DA3C5FAB1C71FF
depends:
vermagic:       2.6.31-<span style="color: #000000;">17</span>-generic SMP mod_unload modversions
parm:           parameter_int:An interger value <span style="color: #7a0874; font-weight: bold;">&#40;</span>int<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>and the parm: is the important part here, it is what the parameter is called to pass a value to for example to watch the module being inserted if you open up two consoles and on one put</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>messages</pre></div></div>

<p>in the second console do</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">insmod helloworld_parameter.ko
rmmod helloworld_parameter.ko
insmod helloworld_parameter.ko <span style="color: #007800;">parameter_int</span>=<span style="color: #000000;">3</span>
rmmod helloworld_parameter.ko</pre></div></div>

<p>and the output should be in the first console</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Hello world
&nbsp;
my parameter int value is : <span style="color: #000000;">0</span>
Goodbye from hello world parameter
&nbsp;
Hello world
&nbsp;
my parameter int value is : <span style="color: #000000;">3</span>
Goodbye from hello world parameter</pre></div></div>

<p>hope that helps with kernel modules, I am planning on doing a kernel module for a custom built USB device.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/01/kernel-hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>kernel &#8211; passing module parameters</title>
		<link>http://www.codingfriends.com/index.php/2010/02/27/kernel-passing-module-parameters/</link>
		<comments>http://www.codingfriends.com/index.php/2010/02/27/kernel-passing-module-parameters/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 16:19:22 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Linux install and configurations]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[insmod]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[modinfo]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[nvidia]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=759</guid>
		<description><![CDATA[With normal programs that allow you pass parameters on there command line, for example

printoutmyname genux

where the program is called printoutmyname and the first parameter is genux (it is normal that the first parameter [0] is actually the program name and the second parameter [1] is the parameter that is passed)
Well in Linux kernel (where here [...]]]></description>
			<content:encoded><![CDATA[<p>With normal programs that allow you pass parameters on there command line, for example</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">printoutmyname genux</pre></div></div>

<p>where the program is called printoutmyname and the first parameter is genux (it is normal that the first parameter [0] is actually the program name and the second parameter [1] is the parameter that is passed)</p>
<p>Well in <a href="http://www.kernel.org/">Linux kernel</a> (where here is a example on how to <a href="http://www.codingfriends.com/index.php/2010/01/07/compile-a-kubuntu-kernel/">compile</a> the kernel for a ubuntu based setup) you can pass parameters to the modules that are getting loaded.  One module would be your graphics card, in my case a nvidia graphics card. </p>
<p>To find out what parameters can be passed to the graphics card you will have to find the kernel module file (filename.ko, where the ko is the kernel object file), so to search for the nvidia.ko in my case I did</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">locate</span> nvidia.ko</pre></div></div>

<p>and then changed to that directory and did a module information on it</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>modules<span style="color: #000000; font-weight: bold;">/</span>2.6.31-<span style="color: #000000;">17</span>-generic<span style="color: #000000; font-weight: bold;">/</span>updates<span style="color: #000000; font-weight: bold;">/</span>dkms<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">ls</span>
nvidia.ko  vboxdrv.ko  vboxnetadp.ko  vboxnetflt.ko</pre></div></div>

<p>and doing a module information on it you call the <a href="http://linuxcommand.org/man_pages/modinfo8.html">modinfo</a> on the kernel object file as</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">modinfo nvidia.ko
<span style="color: #000000; font-weight: bold;">&lt;/</span>pre
&nbsp;
my output was
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;</span>pre <span style="color: #007800;">lang</span>=<span style="color: #ff0000;">&quot;bash&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
filename:       nvidia.ko
license:        NVIDIA
<span style="color: #7a0874; font-weight: bold;">alias</span>:          char-major-<span style="color: #000000;">195</span>-<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #7a0874; font-weight: bold;">alias</span>:          pci:v000010DEd<span style="color: #000000; font-weight: bold;">*</span>sv<span style="color: #000000; font-weight: bold;">*</span>sd<span style="color: #000000; font-weight: bold;">*</span>bc03sc02i00<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #7a0874; font-weight: bold;">alias</span>:          pci:v000010DEd<span style="color: #000000; font-weight: bold;">*</span>sv<span style="color: #000000; font-weight: bold;">*</span>sd<span style="color: #000000; font-weight: bold;">*</span>bc03sc00i00<span style="color: #000000; font-weight: bold;">*</span>
depends:
vermagic:       2.6.31-<span style="color: #000000;">17</span>-generic SMP mod_unload modversions
parm:           NVreg_EnableVia4x:int
parm:           NVreg_EnableALiAGP:int
parm:           NVreg_ReqAGPRate:int
parm:           NVreg_EnableAGPSBA:int
parm:           NVreg_EnableAGPFW:int
parm:           NVreg_Mobile:int
parm:           NVreg_ResmanDebugLevel:int
parm:           NVreg_RmLogonRC:int
parm:           NVreg_ModifyDeviceFiles:int
parm:           NVreg_DeviceFileUID:int
parm:           NVreg_DeviceFileGID:int
parm:           NVreg_DeviceFileMode:int
parm:           NVreg_RemapLimit:int
parm:           NVreg_UpdateMemoryTypes:int
parm:           NVreg_UseVBios:int
parm:           NVreg_RMEdgeIntrCheck:int
parm:           NVreg_UsePageAttributeTable:int
parm:           NVreg_EnableMSI:int
parm:           NVreg_MapRegistersEarly:int
parm:           NVreg_RmNvclkIdleGraphics:int
parm:           NVreg_RegistryDwords:charp
parm:           NVreg_NvAGP:int</pre></div></div>

<p>where the parm: is a parameter to pass to the module on load (insmod, or loaded via the kernel at boot time which you can force to load via the /etc/modules file and the parameters can be placed in the /etc/modprobe.d directory).</p>
<p>for example to load the nvidia module with a parameter NVreg_NvAGP you would do something like</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">insmod nvidia.ko <span style="color: #007800;">NVreg_NvAGP</span>=<span style="color: #000000;">1</span></pre></div></div>

<p>and the passing value is 1 to the NVreg_NvAGP parameter</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/02/27/kernel-passing-module-parameters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
