<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-4928424574574982882</atom:id><lastBuildDate>Wed, 30 May 2012 09:20:04 +0000</lastBuildDate><category>Connection Pool</category><category>install</category><category>cl.exe</category><category>SQL</category><category>CURL</category><category>Nmake</category><category>registry</category><category>Enterprise Edition</category><category>free</category><category>web services tomcat</category><category>struts2</category><category>SQLServer</category><category>VC++</category><category>mbean</category><category>array</category><category>ip</category><category>EJB</category><category>test</category><category>pool</category><category>mtrace</category><category>stdin</category><category>connection string</category><category>publish</category><category>configuration</category><category>helpful</category><category>classes</category><category>web2</category><category>makefile</category><category>servlet</category><category>location.</category><category>JMS</category><category>redirect</category><category>Stateful</category><category>Stateless</category><category>web serivces</category><category>break web proxy</category><category>port 1433</category><category>scripting</category><category>mount NTFS</category><category>snippets</category><category>trail</category><category>helloworld</category><category>producer</category><category>MySQL</category><category>java</category><category>Open JMS</category><category>proxy core</category><category>core</category><category>example</category><category>acronyms</category><category>DLL</category><category>URL</category><category>policy</category><category>parameters</category><category>Enterprise Java Beans</category><category>XML</category><category>Perl</category><category>Java Native Interface</category><category>function pointers</category><category>gems</category><category>fork</category><category>layer</category><category>filter</category><category>SAX</category><category>read</category><category>explicit</category><category>interview</category><category>socket</category><category>live CD</category><category>tutorials</category><category>struts</category><category>multi-threading</category><category>build</category><category>software</category><category>practices</category><category>weblogic</category><category>design</category><category>framework</category><category>ubuntu</category><category>j2ee</category><category>C-API</category><category>widget</category><category>axis 2</category><category>json</category><category>examples</category><category>.NET</category><category>sitemap</category><category>space</category><category>yahoo</category><category>consumer</category><category>RCS</category><category>proxy</category><category>Bean</category><category>design patterns</category><category>dom</category><category>javascript</category><category>oracle client</category><category>Patch</category><category>SQL Server</category><category>recursive</category><category>AJAX</category><category>tomcat</category><category>Oracle</category><category>C++</category><category>google maps api</category><category>download</category><category>JNDI</category><category>OCI</category><category>Entity</category><category>multidimentional</category><category>OCCI</category><category>class</category><category>windows</category><category>Glassfish 2</category><category>image</category><category>crawler</category><category>threaded</category><category>performance counter</category><category>file</category><category>memory leak</category><category>linux</category><category>apache</category><category>gemette</category><category>web 2</category><category>JNI</category><category>persistence.xml</category><category>ant</category><category>GoF</category><category>php</category><category>tzupdater</category><category>tutorial</category><category>securtiy</category><category>daylight saving time</category><category>DSN</category><category>visual C++</category><category>web services</category><category>quiz</category><category>API</category><category>widgets</category><category>thread</category><category>hints</category><category>jwsdp</category><category>SOAP</category><category>database pool</category><category>Callback</category><category>hello world</category><category>blogger</category><category>implicit</category><category>Annotaions</category><category>resonse</category><category>twitter</category><category>pattern</category><category>muntrace</category><category>Connector C++</category><category>ODBC</category><category>tiwtter</category><category>create web proxy</category><category>questions</category><title>Tidy Tutorials</title><description>Programming tutorials, examples and code snippets. C++, Java, Perl, PHP and javascript. Enterprise examples using javaEE, JNDI and Beans. Core tutorials using sockets, threads, database, IPC, XML etc. Web 2 widgets using javascrpt. Simple easy to understand step by step tutorials,</description><link>http://www.tidytutorials.com/</link><managingEditor>noreply@blogger.com (righteous)</managingEditor><generator>Blogger</generator><openSearch:totalResults>118</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/CodeDiaries" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="codediaries" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-6839397138608787818</guid><pubDate>Fri, 02 Nov 2012 11:49:00 +0000</pubDate><atom:updated>2011-10-03T20:52:09.392-07:00</atom:updated><title>Welcome to Tidy Tutorials</title><description>Tidy Tutorials is the best place for C++ examples and Java examples. Code snippets and tutorials covering topics such as templates, pointers, dlls, JNI, JNDI, J2EE and JWSDP. Also covered are topics on Apache, XML, Glassfish, MySQL and Oracle. Easy to understand step by step rutorials and demonstrations. Learn C++ and Java Object Oriented principles, idioms and patterns.&lt;br /&gt;
&lt;br /&gt;
Tidy Tutorials also has scripting examples for Php, Perl and Javascript. Use regualar expressions to create web crawlers and proxies in PHP or Perl. This site has tutorials on javascript and AJAX and explains how to use available apis to create rich and interactive widgets.&lt;br /&gt;
&lt;br /&gt;
C++, Java, Javascript, Perl, PHP we have all the examples. Easy to follow, easy to understand step by step tutorials.&lt;br /&gt;
&lt;br /&gt;
Learn C++ and Java. Learn scripting languages such as Perl, PHP and Javascript. Become a guru and master developer. Code Diaries has tutorials and examples to get you there.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://codediaries.com/"&gt;CodeDiaries Proxy Network&lt;/a&gt; - For free web and ip:port proxies using the Code Diaries proxy core.&lt;br /&gt;
&lt;a href="http://codediariesproxy.blogspot.com/"&gt;&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://dirtcheapproxies.com/"&gt;Dirt Cheap Proxies&lt;/a&gt;&amp;nbsp; - For fast and reliable managed proxies for $5/month&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-6839397138608787818?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/mUDJDlWaiuamG5qeBQoUUkEm6Xg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/mUDJDlWaiuamG5qeBQoUUkEm6Xg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/mUDJDlWaiuamG5qeBQoUUkEm6Xg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/mUDJDlWaiuamG5qeBQoUUkEm6Xg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/_YOqRtCsMcU" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2010/11/welcome-to-tidy-tutorials.html</link><author>noreply@blogger.com (righteous)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-176634224511052104</guid><pubDate>Thu, 19 Jan 2012 09:47:00 +0000</pubDate><atom:updated>2012-03-09T03:41:37.203-08:00</atom:updated><title>C/C++ Regex Replace Function</title><description>The aim is to implement a regex replace function that works in C/C++. Similar to php's preg_replace and perl's '=~'. The function is called regexreplace and supports groupings and back references. This function uses the regexec and regcomp functions, is simple easy to use and doesn't need Boost! One C/C++ function that does regular expression matching and replacement.&lt;br /&gt;
&lt;br /&gt;
int regexreplace(char *pattern, char* replacement, char* instring,&amp;nbsp; char** outstring, int maxlength);&lt;br /&gt;
&lt;br /&gt;
It accepts a pattern to be matched, the replacement, the input string a char** outstring which will contain the result and the maxlength is the limit of the outstring.&lt;br /&gt;
&lt;br /&gt;
Here is an example of how it can be used. Note the &lt;i&gt;&lt;b&gt;#include "regexreplace.h"&lt;/b&gt;&lt;/i&gt; that is required as the functions are defined in there.&lt;br /&gt;
&lt;span class="com"&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;
&lt;table border="1" class="CD_Code_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre class="CD_Code_Words" id="xxpre"&gt;&lt;span class="CD_Code_Num" name="xxhid"&gt; 1. &lt;/span&gt;#include &amp;lt;stdlib.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 2. &lt;/span&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 3. &lt;/span&gt;#include &amp;lt;string.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 4. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 5. &lt;/span&gt;#include "regexreplace.h"
&lt;span class="CD_Code_Num" name="xxhid"&gt; 6. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 7. &lt;/span&gt;int main(int vv, char** cc){
&lt;span class="CD_Code_Num" name="xxhid"&gt; 8. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 9. &lt;/span&gt;    char* result;
&lt;span class="CD_Code_Num" name="xxhid"&gt;10. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;11. &lt;/span&gt;    if(regexreplace("shaped_\\([0-9]*\\)_\\([0-9]*\\)", "quota_policer(\\1,\\2)", "shaped_500_400  and shaped_900_100", &amp;amp;result, 256)==0){
&lt;span class="CD_Code_Num" name="xxhid"&gt;12. &lt;/span&gt;        printf("====&amp;gt;%s&amp;lt;===\n",result); 
&lt;span class="CD_Code_Num" name="xxhid"&gt;13. &lt;/span&gt;        free(result);
&lt;span class="CD_Code_Num" name="xxhid"&gt;14. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;15. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;16. &lt;/span&gt;    if(regexreplace("\\([0-9][0-9]*\\)_\\([abc]\\)", "quota_policer(\\1,\\2)", "hello_100_c mr barbie 987_a naf", &amp;amp;result, 256)==0){
&lt;span class="CD_Code_Num" name="xxhid"&gt;17. &lt;/span&gt;        printf("====&amp;gt;%s&amp;lt;===\n",result); 
&lt;span class="CD_Code_Num" name="xxhid"&gt;18. &lt;/span&gt;        free(result);
&lt;span class="CD_Code_Num" name="xxhid"&gt;19. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;20. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;21. &lt;/span&gt;    if(regexreplace("\\([0-9][0-9]*\\)_\\([abc]\\)", "quota_policer(\\1,\\2-\\3)", "hello_100_c mr barbie 987_a naf", &amp;amp;result, 256)==0){
&lt;span class="CD_Code_Num" name="xxhid"&gt;22. &lt;/span&gt;        printf("====&amp;gt;%s&amp;lt;===\n",result); 
&lt;span class="CD_Code_Num" name="xxhid"&gt;23. &lt;/span&gt;        free(result);
&lt;span class="CD_Code_Num" name="xxhid"&gt;24. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;25. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;26. &lt;/span&gt;    if(regexreplace("\\([0-9][0-9]*\\)_\\([abc]\\)", "", "hello_100_c mr barbie 987_a naf", &amp;amp;result, 256)==0){
&lt;span class="CD_Code_Num" name="xxhid"&gt;27. &lt;/span&gt;        printf("====&amp;gt;%s&amp;lt;===\n",result); 
&lt;span class="CD_Code_Num" name="xxhid"&gt;28. &lt;/span&gt;        free(result);
&lt;span class="CD_Code_Num" name="xxhid"&gt;29. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;30. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;31. &lt;/span&gt;    if(regexreplace("shaped_\\([0-9]*\\)_\\([0-9]*\\)", "quota_policer(\\1,\\2)", "shaped_500_400  and shaped_900_100", &amp;amp;result, 24)==0){
&lt;span class="CD_Code_Num" name="xxhid"&gt;32. &lt;/span&gt;        printf("====&amp;gt;%s&amp;lt;===\n",result); 
&lt;span class="CD_Code_Num" name="xxhid"&gt;33. &lt;/span&gt;        free(result);
&lt;span class="CD_Code_Num" name="xxhid"&gt;34. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;35. &lt;/span&gt;}&lt;/pre&gt;
&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input onclick="ToggleLineNumbers(this.checked, this.parentNode)" type="checkbox" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Below is the listing of the headerfile regexreplace.h. The idea was to keep it simple and not have to arrays and copy between arrays.&lt;br /&gt;
&lt;table border="1" class="CD_Code_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre class="CD_Code_Words" id="xxpre"&gt;&lt;span class="CD_Code_Num" name="xxhid"&gt; 1. &lt;/span&gt;#ifndef REGEXREPLACE_H
&lt;span class="CD_Code_Num" name="xxhid"&gt; 2. &lt;/span&gt;#define REGEXREPLACE
&lt;span class="CD_Code_Num" name="xxhid"&gt; 3. &lt;/span&gt;#include &amp;lt;string.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 4. &lt;/span&gt;#include &amp;lt;stdlib.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 5. &lt;/span&gt;#include &amp;lt;regex.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 6. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 7. &lt;/span&gt;#define MAX_NMATCH    10
&lt;span class="CD_Code_Num" name="xxhid"&gt; 8. &lt;/span&gt;typedef struct{
&lt;span class="CD_Code_Num" name="xxhid"&gt; 9. &lt;/span&gt;    int offset;
&lt;span class="CD_Code_Num" name="xxhid"&gt;10. &lt;/span&gt;    regmatch_t pmatch[MAX_NMATCH];
&lt;span class="CD_Code_Num" name="xxhid"&gt;11. &lt;/span&gt;} regmatch_tt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;12. &lt;/span&gt;/* Move a string into a string */
&lt;span class="CD_Code_Num" name="xxhid"&gt;13. &lt;/span&gt;int s1(char* s, int sl, char* r, int rl){
&lt;span class="CD_Code_Num" name="xxhid"&gt;14. &lt;/span&gt;    memmove(s+rl, s+sl, strlen(s)-sl+1);
&lt;span class="CD_Code_Num" name="xxhid"&gt;15. &lt;/span&gt;    memcpy(s, r, rl);
&lt;span class="CD_Code_Num" name="xxhid"&gt;16. &lt;/span&gt;    return rl-sl;    
&lt;span class="CD_Code_Num" name="xxhid"&gt;17. &lt;/span&gt;}
&lt;span class="CD_Code_Num" name="xxhid"&gt;18. &lt;/span&gt;/* Handle realloc */
&lt;span class="CD_Code_Num" name="xxhid"&gt;19. &lt;/span&gt;int reallocx(char** x, int s, int max){
&lt;span class="CD_Code_Num" name="xxhid"&gt;20. &lt;/span&gt;    char *tmp;
&lt;span class="CD_Code_Num" name="xxhid"&gt;21. &lt;/span&gt;    if(s&amp;gt;max)
&lt;span class="CD_Code_Num" name="xxhid"&gt;22. &lt;/span&gt;        return -1;
&lt;span class="CD_Code_Num" name="xxhid"&gt;23. &lt;/span&gt;    if((tmp = realloc(*x, s))!=0){
&lt;span class="CD_Code_Num" name="xxhid"&gt;24. &lt;/span&gt;        *x = tmp;
&lt;span class="CD_Code_Num" name="xxhid"&gt;25. &lt;/span&gt;        return 0; 
&lt;span class="CD_Code_Num" name="xxhid"&gt;26. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;27. &lt;/span&gt;    else
&lt;span class="CD_Code_Num" name="xxhid"&gt;28. &lt;/span&gt;        return -1; 
&lt;span class="CD_Code_Num" name="xxhid"&gt;29. &lt;/span&gt;} 
&lt;span class="CD_Code_Num" name="xxhid"&gt;30. &lt;/span&gt;/* Use regexec, but to match all occurrences */
&lt;span class="CD_Code_Num" name="xxhid"&gt;31. &lt;/span&gt;int regexec_matchall(regex_t* pregx, const char* instring, size_t nmatch_t, regmatch_tt * pmatcht, int flags ){
&lt;span class="CD_Code_Num" name="xxhid"&gt;32. &lt;/span&gt;    int offset, counter;
&lt;span class="CD_Code_Num" name="xxhid"&gt;33. &lt;/span&gt;    offset = 0;
&lt;span class="CD_Code_Num" name="xxhid"&gt;34. &lt;/span&gt;    counter = 0;
&lt;span class="CD_Code_Num" name="xxhid"&gt;35. &lt;/span&gt;    while (regexec (pregx, instring+offset, MAX_NMATCH, pmatcht[counter].pmatch, REG_EXTENDED)==0 &amp;amp;&amp;amp; counter&amp;lt;nmatch_t) {  /* While matches found. */
&lt;span class="CD_Code_Num" name="xxhid"&gt;36. &lt;/span&gt;        pmatcht[counter].offset = offset;
&lt;span class="CD_Code_Num" name="xxhid"&gt;37. &lt;/span&gt;        offset+= pmatcht[counter].pmatch[0].rm_eo;
&lt;span class="CD_Code_Num" name="xxhid"&gt;38. &lt;/span&gt;        counter++;
&lt;span class="CD_Code_Num" name="xxhid"&gt;39. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;40. &lt;/span&gt;    while(counter&amp;lt;nmatch_t-1){
&lt;span class="CD_Code_Num" name="xxhid"&gt;41. &lt;/span&gt;        pmatcht[counter].offset=-1;
&lt;span class="CD_Code_Num" name="xxhid"&gt;42. &lt;/span&gt;        counter++;
&lt;span class="CD_Code_Num" name="xxhid"&gt;43. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;44. &lt;/span&gt;    if(offset == 0)
&lt;span class="CD_Code_Num" name="xxhid"&gt;45. &lt;/span&gt;        return -1;
&lt;span class="CD_Code_Num" name="xxhid"&gt;46. &lt;/span&gt;    else
&lt;span class="CD_Code_Num" name="xxhid"&gt;47. &lt;/span&gt;        return 0;
&lt;span class="CD_Code_Num" name="xxhid"&gt;48. &lt;/span&gt;}
&lt;span class="CD_Code_Num" name="xxhid"&gt;49. &lt;/span&gt;/* The function that actually does the work */
&lt;span class="CD_Code_Num" name="xxhid"&gt;50. &lt;/span&gt;int preg_replace(regex_t* pregxo, char* replacement,  regex_t*  pregx, char* tomatchx, char** out, int maxlength){
&lt;span class="CD_Code_Num" name="xxhid"&gt;51. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;52. &lt;/span&gt;    int i,j,k, offset, offset2; 
&lt;span class="CD_Code_Num" name="xxhid"&gt;53. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;54. &lt;/span&gt;    regmatch_tt pmatcht[MAX_NMATCH];
&lt;span class="CD_Code_Num" name="xxhid"&gt;55. &lt;/span&gt;    regmatch_tt pmatchto[MAX_NMATCH];
&lt;span class="CD_Code_Num" name="xxhid"&gt;56. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;57. &lt;/span&gt;    regmatch_tt* pt, *mt, *xt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;58. &lt;/span&gt;    regmatch_t * p,  *m,  *x;
&lt;span class="CD_Code_Num" name="xxhid"&gt;59. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;60. &lt;/span&gt;    char    tmpvalue2[256];
&lt;span class="CD_Code_Num" name="xxhid"&gt;61. &lt;/span&gt;    char    *c;
&lt;span class="CD_Code_Num" name="xxhid"&gt;62. &lt;/span&gt;    char    *tomatch = 0;
&lt;span class="CD_Code_Num" name="xxhid"&gt;63. &lt;/span&gt;    int    deltalen;
&lt;span class="CD_Code_Num" name="xxhid"&gt;64. &lt;/span&gt;    char    *tmpreplacement = 0;
&lt;span class="CD_Code_Num" name="xxhid"&gt;65. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;66. &lt;/span&gt;    if(reallocx(&amp;amp;tomatch, strlen(tomatchx)+1,maxlength)==-1)
&lt;span class="CD_Code_Num" name="xxhid"&gt;67. &lt;/span&gt;        goto FINISHED;
&lt;span class="CD_Code_Num" name="xxhid"&gt;68. &lt;/span&gt;    else
&lt;span class="CD_Code_Num" name="xxhid"&gt;69. &lt;/span&gt;        strcpy(tomatch, tomatchx);
&lt;span class="CD_Code_Num" name="xxhid"&gt;70. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;71. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;72. &lt;/span&gt;    if(regexec_matchall(pregxo, replacement, 10, pmatchto, 0)!=0){
&lt;span class="CD_Code_Num" name="xxhid"&gt;73. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;74. &lt;/span&gt;    if(regexec_matchall(pregx, tomatch, 10, pmatcht, 0)!=0){
&lt;span class="CD_Code_Num" name="xxhid"&gt;75. &lt;/span&gt;        return -1;
&lt;span class="CD_Code_Num" name="xxhid"&gt;76. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;77. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;78. &lt;/span&gt;    offset=0;
&lt;span class="CD_Code_Num" name="xxhid"&gt;79. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;80. &lt;/span&gt;    for(i = 0 ; i &amp;lt; MAX_NMATCH &amp;amp;&amp;amp;  pmatcht[i].offset!=-1; i++){
&lt;span class="CD_Code_Num" name="xxhid"&gt;81. &lt;/span&gt;        if(reallocx(&amp;amp;tmpreplacement, strlen(replacement)+1,maxlength)==-1)
&lt;span class="CD_Code_Num" name="xxhid"&gt;82. &lt;/span&gt;            goto FINISHED;
&lt;span class="CD_Code_Num" name="xxhid"&gt;83. &lt;/span&gt;        else
&lt;span class="CD_Code_Num" name="xxhid"&gt;84. &lt;/span&gt;            strcpy(tmpreplacement, replacement);
&lt;span class="CD_Code_Num" name="xxhid"&gt;85. &lt;/span&gt;        offset2=0;
&lt;span class="CD_Code_Num" name="xxhid"&gt;86. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;87. &lt;/span&gt;        for( k = 0 ;  k &amp;lt; MAX_NMATCH &amp;amp;&amp;amp; pmatchto[k].offset !=-1 ;k ++){
&lt;span class="CD_Code_Num" name="xxhid"&gt;88. &lt;/span&gt;            pt = &amp;amp;pmatchto[k];
&lt;span class="CD_Code_Num" name="xxhid"&gt;89. &lt;/span&gt;            p = &amp;amp;pt-&amp;gt;pmatch[0];
&lt;span class="CD_Code_Num" name="xxhid"&gt;90. &lt;/span&gt;            
&lt;span class="CD_Code_Num" name="xxhid"&gt;91. &lt;/span&gt;            memset(tmpvalue2, 0, 256);
&lt;span class="CD_Code_Num" name="xxhid"&gt;92. &lt;/span&gt;            strncpy(tmpvalue2, &amp;amp;tmpreplacement[p-&amp;gt;rm_so+pt-&amp;gt;offset+offset2], p-&amp;gt;rm_eo-p-&amp;gt;rm_so);
&lt;span class="CD_Code_Num" name="xxhid"&gt;93. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;94. &lt;/span&gt;            while((c=strchr(tmpvalue2, '\\'))!=0)
&lt;span class="CD_Code_Num" name="xxhid"&gt;95. &lt;/span&gt;                *c='0';
&lt;span class="CD_Code_Num" name="xxhid"&gt;96. &lt;/span&gt;            j = atoi(tmpvalue2);
&lt;span class="CD_Code_Num" name="xxhid"&gt;97. &lt;/span&gt;            if(j&amp;lt; MAX_NMATCH &amp;amp;&amp;amp; pmatcht[i].pmatch[j].rm_so !=-1 ){
&lt;span class="CD_Code_Num" name="xxhid"&gt;98. &lt;/span&gt;                mt = &amp;amp;pmatcht[i];
&lt;span class="CD_Code_Num" name="xxhid"&gt;99. &lt;/span&gt;                m = &amp;amp;mt-&amp;gt;pmatch[j];
&lt;span class="CD_Code_Num" name="xxhid"&gt;100. &lt;/span&gt;                deltalen=(m-&amp;gt;rm_eo-m-&amp;gt;rm_so)-(p-&amp;gt;rm_eo-p-&amp;gt;rm_so);
&lt;span class="CD_Code_Num" name="xxhid"&gt;101. &lt;/span&gt;                if(deltalen&amp;gt;0 &amp;amp;&amp;amp; reallocx(&amp;amp;tmpreplacement,strlen(tmpreplacement)+deltalen+1, maxlength)==-1)
&lt;span class="CD_Code_Num" name="xxhid"&gt;102. &lt;/span&gt;                    goto FINISHED;
&lt;span class="CD_Code_Num" name="xxhid"&gt;103. &lt;/span&gt;                offset2+=s1(&amp;amp;tmpreplacement[p-&amp;gt;rm_so+pt-&amp;gt;offset+offset2], p-&amp;gt;rm_eo-p-&amp;gt;rm_so, 
&lt;span class="CD_Code_Num" name="xxhid"&gt;104. &lt;/span&gt;                        &amp;amp;tomatch[m-&amp;gt;rm_so+mt-&amp;gt;offset+offset], m-&amp;gt;rm_eo-m-&amp;gt;rm_so);
&lt;span class="CD_Code_Num" name="xxhid"&gt;105. &lt;/span&gt;            }
&lt;span class="CD_Code_Num" name="xxhid"&gt;106. &lt;/span&gt;            else{
&lt;span class="CD_Code_Num" name="xxhid"&gt;107. &lt;/span&gt;                char* x ="";
&lt;span class="CD_Code_Num" name="xxhid"&gt;108. &lt;/span&gt;                offset2+=s1(&amp;amp;tmpreplacement[p-&amp;gt;rm_so+pt-&amp;gt;offset+offset2], p-&amp;gt;rm_eo-p-&amp;gt;rm_so, x, 0);
&lt;span class="CD_Code_Num" name="xxhid"&gt;109. &lt;/span&gt;            }
&lt;span class="CD_Code_Num" name="xxhid"&gt;110. &lt;/span&gt;        }    
&lt;span class="CD_Code_Num" name="xxhid"&gt;111. &lt;/span&gt;        xt = &amp;amp;pmatcht[i];
&lt;span class="CD_Code_Num" name="xxhid"&gt;112. &lt;/span&gt;        x = &amp;amp;xt-&amp;gt;pmatch[0];
&lt;span class="CD_Code_Num" name="xxhid"&gt;113. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;114. &lt;/span&gt;        deltalen=(strlen(tmpreplacement)-(x-&amp;gt;rm_eo-x-&amp;gt;rm_so));
&lt;span class="CD_Code_Num" name="xxhid"&gt;115. &lt;/span&gt;        if(deltalen&amp;gt; 0 &amp;amp;&amp;amp; reallocx(&amp;amp;tomatch, strlen(tomatch)+deltalen+1,maxlength)==-1)
&lt;span class="CD_Code_Num" name="xxhid"&gt;116. &lt;/span&gt;            goto FINISHED;
&lt;span class="CD_Code_Num" name="xxhid"&gt;117. &lt;/span&gt;        offset+=s1(&amp;amp;tomatch[x-&amp;gt;rm_so+xt-&amp;gt;offset+offset], x-&amp;gt;rm_eo-x-&amp;gt;rm_so, tmpreplacement, strlen(tmpreplacement ) );
&lt;span class="CD_Code_Num" name="xxhid"&gt;118. &lt;/span&gt;        free(tmpreplacement);
&lt;span class="CD_Code_Num" name="xxhid"&gt;119. &lt;/span&gt;        tmpreplacement=0;
&lt;span class="CD_Code_Num" name="xxhid"&gt;120. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;121. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;122. &lt;/span&gt;    *out=tomatch;
&lt;span class="CD_Code_Num" name="xxhid"&gt;123. &lt;/span&gt;    return 0;
&lt;span class="CD_Code_Num" name="xxhid"&gt;124. &lt;/span&gt;FINISHED:
&lt;span class="CD_Code_Num" name="xxhid"&gt;125. &lt;/span&gt;    if(tomatch)
&lt;span class="CD_Code_Num" name="xxhid"&gt;126. &lt;/span&gt;        free(tomatch);
&lt;span class="CD_Code_Num" name="xxhid"&gt;127. &lt;/span&gt;    if(tmpreplacement)
&lt;span class="CD_Code_Num" name="xxhid"&gt;128. &lt;/span&gt;        free(tmpreplacement);
&lt;span class="CD_Code_Num" name="xxhid"&gt;129. &lt;/span&gt;    return -1;
&lt;span class="CD_Code_Num" name="xxhid"&gt;130. &lt;/span&gt;}
&lt;span class="CD_Code_Num" name="xxhid"&gt;131. &lt;/span&gt;/* The regexec replace function */
&lt;span class="CD_Code_Num" name="xxhid"&gt;132. &lt;/span&gt;int regexreplace(char * pattern, char* replacement, char* instring,  char** outstring, int maxlength){
&lt;span class="CD_Code_Num" name="xxhid"&gt;133. &lt;/span&gt;    regex_t pregx, pregxo;
&lt;span class="CD_Code_Num" name="xxhid"&gt;134. &lt;/span&gt;    int retvalue;
&lt;span class="CD_Code_Num" name="xxhid"&gt;135. &lt;/span&gt;    const char* patterno = "\\(\\\\[0-9][0-9]*\\)";
&lt;span class="CD_Code_Num" name="xxhid"&gt;136. &lt;/span&gt;    if(regcomp(&amp;amp;pregxo, patterno, 0)!=0){
&lt;span class="CD_Code_Num" name="xxhid"&gt;137. &lt;/span&gt;        return -1; }
&lt;span class="CD_Code_Num" name="xxhid"&gt;138. &lt;/span&gt;    if(regcomp(&amp;amp;pregx, pattern, 0)!=0){
&lt;span class="CD_Code_Num" name="xxhid"&gt;139. &lt;/span&gt;        return -1; }
&lt;span class="CD_Code_Num" name="xxhid"&gt;140. &lt;/span&gt;    retvalue=preg_replace(&amp;amp;pregxo, replacement,  &amp;amp;pregx, instring, outstring, maxlength);
&lt;span class="CD_Code_Num" name="xxhid"&gt;141. &lt;/span&gt;    regfree(&amp;amp;pregxo);
&lt;span class="CD_Code_Num" name="xxhid"&gt;142. &lt;/span&gt;    regfree(&amp;amp;pregx);
&lt;span class="CD_Code_Num" name="xxhid"&gt;143. &lt;/span&gt;    return retvalue;
&lt;span class="CD_Code_Num" name="xxhid"&gt;144. &lt;/span&gt;}
&lt;span class="CD_Code_Num" name="xxhid"&gt;145. &lt;/span&gt;#endif&lt;/pre&gt;
&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input onclick="ToggleLineNumbers(this.checked, this.parentNode)" type="checkbox" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-176634224511052104?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/cv-fpxUGktLfnZbTyU_Clihmq8Y/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/cv-fpxUGktLfnZbTyU_Clihmq8Y/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/cv-fpxUGktLfnZbTyU_Clihmq8Y/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/cv-fpxUGktLfnZbTyU_Clihmq8Y/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/JRBU3mw_o20" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2012/01/cc-regex-replace-function.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-1005805528485392324</guid><pubDate>Thu, 19 Jan 2012 04:06:00 +0000</pubDate><atom:updated>2012-01-19T02:01:10.978-08:00</atom:updated><title>C/C++ Regular Expressions - Regex/Regexec</title><description>&lt;b&gt;Aim&lt;/b&gt;&lt;br /&gt;
The aim of this example is to demonstrate the regex functions available in C/C++. This example is not using Boost. This code snippet shows how you can use regcomp and regexec to match a string with grouping and backreferences. Please use this as an introduction into regular expressions for C/C++ (For a more sophisticated function that does replacement aswell pleas see &lt;a href="http://www.tidytutorials.com/2012/01/cc-regex-replace-function.html"&gt;C/C++ regexreplace).&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;table border="1" class="CD_Code_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre class="CD_Code_Words" id="xxpre"&gt;&lt;span class="CD_Code_Num" name="xxhid"&gt; 1. &lt;/span&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 2. &lt;/span&gt;#include &amp;lt;string.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 3. &lt;/span&gt;#include &amp;lt;regex.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 4. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 5. &lt;/span&gt;int main(int vv, char** c){
&lt;span class="CD_Code_Num" name="xxhid"&gt; 6. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 7. &lt;/span&gt;    regex_t pregx;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 8. &lt;/span&gt;    regmatch_t pmatch[10];
&lt;span class="CD_Code_Num" name="xxhid"&gt; 9. &lt;/span&gt;    int i;
&lt;span class="CD_Code_Num" name="xxhid"&gt;10. &lt;/span&gt;    const char* pattern = "\\([0-9][0-9]*\\)_\\([0-9][0-9]*\\)";
&lt;span class="CD_Code_Num" name="xxhid"&gt;11. &lt;/span&gt;    char tomatch[256] = "This is hello_100_200_";
&lt;span class="CD_Code_Num" name="xxhid"&gt;12. &lt;/span&gt;    char tmpvalue[256];
&lt;span class="CD_Code_Num" name="xxhid"&gt;13. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;14. &lt;/span&gt;    if(regcomp(&amp;amp;pregx, pattern, 0)!=0){
&lt;span class="CD_Code_Num" name="xxhid"&gt;15. &lt;/span&gt;        printf("Pattern failed to compile\n");
&lt;span class="CD_Code_Num" name="xxhid"&gt;16. &lt;/span&gt;        return -1;
&lt;span class="CD_Code_Num" name="xxhid"&gt;17. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;18. &lt;/span&gt;    if(regexec(&amp;amp;pregx, tomatch, 10, pmatch,0)!=0){
&lt;span class="CD_Code_Num" name="xxhid"&gt;19. &lt;/span&gt;        printf("Could not match pattern %s  %s\n",pattern, tomatch);
&lt;span class="CD_Code_Num" name="xxhid"&gt;20. &lt;/span&gt;        return -1;
&lt;span class="CD_Code_Num" name="xxhid"&gt;21. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;22. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;23. &lt;/span&gt;    for(i = 0 ; i &amp;lt; 10 &amp;amp;&amp;amp; pmatch[i].rm_so!=-1 ; i++){
&lt;span class="CD_Code_Num" name="xxhid"&gt;24. &lt;/span&gt;        memset(tmpvalue, 0, 256);
&lt;span class="CD_Code_Num" name="xxhid"&gt;25. &lt;/span&gt;        strncpy(tmpvalue, &amp;amp;tomatch[pmatch[i].rm_so], pmatch[i].rm_eo-pmatch[i].rm_so);
&lt;span class="CD_Code_Num" name="xxhid"&gt;26. &lt;/span&gt;        printf("matched pmatch[%d].rm_so: %d    pmatch[%d].rm_eo: %d  -   \"%s\"\n",
&lt;span class="CD_Code_Num" name="xxhid"&gt;27. &lt;/span&gt;                i, pmatch[i].rm_so, i, pmatch[i].rm_eo, tmpvalue);    
&lt;span class="CD_Code_Num" name="xxhid"&gt;28. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;29. &lt;/span&gt;    return 0;
&lt;span class="CD_Code_Num" name="xxhid"&gt;30. &lt;/span&gt;}&lt;/pre&gt;
&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input onclick="ToggleLineNumbers(this.checked, this.parentNode)" type="checkbox" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
One of the drawbacks of regexec is that it doesn't match multiple times. For example if my string was '&lt;br /&gt;
&lt;i&gt;This is hello_100_200_ and 700_600&lt;/i&gt;' it would only match the 100_200.&lt;br /&gt;
&lt;br /&gt;
&lt;table class="CD_Console_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;br /&gt;
..workspace\&amp;gt;gcc -c reg.c&lt;br /&gt;
..workspace\&amp;gt;./a.out&lt;br /&gt;
matched pmatch[0].rm_so: 14    pmatch[0].rm_eo: 21  -   "100_200"&lt;br /&gt;
matched pmatch[1].rm_so: 14    pmatch[1].rm_eo: 17  -   "100"&lt;br /&gt;
matched pmatch[2].rm_so: 18    pmatch[2].rm_eo: 21  -   "200"
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
As you can see \\0 matches 100_200&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-1005805528485392324?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/7SLZ729Tx-wmLCmGDxdP3BHRkaA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7SLZ729Tx-wmLCmGDxdP3BHRkaA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/7SLZ729Tx-wmLCmGDxdP3BHRkaA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7SLZ729Tx-wmLCmGDxdP3BHRkaA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/QoHh8uXLOV8" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2012/01/cc-regular-expressions-regexec.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-8685790605686730514</guid><pubDate>Sat, 14 Jan 2012 02:18:00 +0000</pubDate><atom:updated>2012-01-15T14:14:08.774-08:00</atom:updated><title>Oracle TimesTen Getting Unixtime or Seconds Since the Epoch UTC</title><description>On normal oracle databases, you can run a simple sql query subtracting the epoch date from sysdate. This doesn't work for TimesTen as you will get an error mentioning '&lt;span style="color: red;"&gt;An interval data type must be specified for a datetime arithmetic result&lt;/span&gt;.' The easiest work around is to cast the subtraction as a bigint and divide by 1000000&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;On TimesTen - sql&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
select cast((sysdate - to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')) as bigint)/1000000 from dual&amp;nbsp;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;On conventional oracle database - sql&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
select (sysdate - to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')) * 86400 from dual&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-8685790605686730514?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/UypRmJrCHwUDewghRwL0eEYjN8c/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/UypRmJrCHwUDewghRwL0eEYjN8c/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/UypRmJrCHwUDewghRwL0eEYjN8c/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/UypRmJrCHwUDewghRwL0eEYjN8c/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/FoZ_sJikSZ8" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2012/01/oracle-timeten-getting-unixtime-or.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-7489706269312633138</guid><pubDate>Wed, 28 Dec 2011 05:42:00 +0000</pubDate><atom:updated>2012-01-02T14:25:42.653-08:00</atom:updated><title>C/C++ Trimming whitespace - Trim</title><description>How to trim strings in C/C++ is a commonly asked question. Below is a simple C/C++ function that will trim leading and trailing whitespaces. Whitespaces include spaces, tabs, carriage return, new lines etc. It get's rid of 'white' spaces at the beginning and at the end of an array of characters.

&lt;table class="CD_Code_Table" border="1"&gt;&lt;tr&gt;&lt;td&gt;&lt;pre id="xxpre" class="CD_Code_Words"&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 1. &lt;/span&gt;int trim(char* c){
&lt;span name="xxhid" class="CD_Code_Num"&gt; 2. &lt;/span&gt;        char * i;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 3. &lt;/span&gt;        if(c==0 || !strlen(c))
&lt;span name="xxhid" class="CD_Code_Num"&gt; 4. &lt;/span&gt;                return 0;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 5. &lt;/span&gt;        for(i=c ; isspace(*i)!=0 &amp;&amp; (i-c)&amp;lt;strlen(c); i++);
&lt;span name="xxhid" class="CD_Code_Num"&gt; 6. &lt;/span&gt;        memmove(c, i, strlen(c)-(i-c)+1);
&lt;span name="xxhid" class="CD_Code_Num"&gt; 7. &lt;/span&gt;        if(!strlen(c))
&lt;span name="xxhid" class="CD_Code_Num"&gt; 8. &lt;/span&gt;                return 0;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 9. &lt;/span&gt;        for(i=(c+strlen(c)-1) ; isspace(*i)!=0 &amp;&amp; i&amp;gt;c; i--);
&lt;span name="xxhid" class="CD_Code_Num"&gt;10. &lt;/span&gt;        *(i+1)='\0';
&lt;span name="xxhid" class="CD_Code_Num"&gt;11. &lt;/span&gt;        return strlen(c);
&lt;span name="xxhid" class="CD_Code_Num"&gt;12. &lt;/span&gt;}
&lt;span name="xxhid" class="CD_Code_Num"&gt;13. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;14. &lt;/span&gt;int main(int vv, char** cc){
&lt;span name="xxhid" class="CD_Code_Num"&gt;15. &lt;/span&gt;        char  xman[] ="  \t String that needs trimming \n ";
&lt;span name="xxhid" class="CD_Code_Num"&gt;16. &lt;/span&gt;        trim(xman);
&lt;span name="xxhid" class="CD_Code_Num"&gt;17. &lt;/span&gt;        printf ("--%s--\n", xman);
&lt;span name="xxhid" class="CD_Code_Num"&gt;18. &lt;/span&gt;}&lt;/pre&gt;&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input type="checkbox" onclick="ToggleLineNumbers(this.checked, this.parentNode)" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-7489706269312633138?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/doiGgSog02UPl4vxN2VH86FpfU0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/doiGgSog02UPl4vxN2VH86FpfU0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/doiGgSog02UPl4vxN2VH86FpfU0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/doiGgSog02UPl4vxN2VH86FpfU0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/zfmDzu1Baq4" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2011/12/cc-trmming-whitespace-trim.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-2884086905166550294</guid><pubDate>Thu, 17 Nov 2011 10:09:00 +0000</pubDate><atom:updated>2011-11-17T02:36:46.200-08:00</atom:updated><title>Image Compression Using Php</title><description>&lt;b&gt;Aim &lt;/b&gt;&lt;br /&gt;
Php has a fair amount of image manipulating functions, but none that really offer any kind of image compression. By image compression we mean sacrificing the quality of the image for faster load times by reducing the size of the image.&lt;br /&gt;
&lt;br /&gt;
The aim is to make a fast and simple function, without the need for temporary files, and without the need to create additional temporary images for interpolation.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Method&lt;/b&gt;&lt;br /&gt;
 The problem is that there is no way to get the 'quality' out of a jpeg, so we estimate the quality based on some assumptions. We also do not try to compress images that are less than 2Mb because the resulting files are usually bigger that the input files.&amp;nbsp;

&lt;br /&gt;
&lt;table border="1" class="CD_Code_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre class="CD_Code_Words" id="xxpre"&gt;&lt;span class="CD_Code_Num" name="xxhid"&gt; 1. &lt;/span&gt;&amp;lt;?php
&lt;span class="CD_Code_Num" name="xxhid"&gt; 2. &lt;/span&gt;ini_set('display_errors', 1); 
&lt;span class="CD_Code_Num" name="xxhid"&gt; 3. &lt;/span&gt;ini_set('log_errors', 1); 
&lt;span class="CD_Code_Num" name="xxhid"&gt; 4. &lt;/span&gt;error_reporting(E_ALL);
&lt;span class="CD_Code_Num" name="xxhid"&gt; 5. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 6. &lt;/span&gt;function LossifyJpegImage(&amp;amp; $data){
&lt;span class="CD_Code_Num" name="xxhid"&gt; 7. &lt;/span&gt;    $size=strlen($data);
&lt;span class="CD_Code_Num" name="xxhid"&gt; 8. &lt;/span&gt;    if($size&amp;lt;2048){
&lt;span class="CD_Code_Num" name="xxhid"&gt; 9. &lt;/span&gt;        echo $data;
&lt;span class="CD_Code_Num" name="xxhid"&gt;10. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;11. &lt;/span&gt;    else if(($im = imagecreatefromstring($data))!==FALSE){
&lt;span class="CD_Code_Num" name="xxhid"&gt;12. &lt;/span&gt;        $x = imagesx ($im);
&lt;span class="CD_Code_Num" name="xxhid"&gt;13. &lt;/span&gt;        $y = imagesy ($im);
&lt;span class="CD_Code_Num" name="xxhid"&gt;14. &lt;/span&gt;        $ratio = intval(($size*100)/($y*$x));
&lt;span class="CD_Code_Num" name="xxhid"&gt;15. &lt;/span&gt;        $quality = intval(log($ratio)/0.043);
&lt;span class="CD_Code_Num" name="xxhid"&gt;16. &lt;/span&gt;        imagejpeg($im, NULL, intval($quality/2));
&lt;span class="CD_Code_Num" name="xxhid"&gt;17. &lt;/span&gt;        file_put_contents('php://stderr', "size $size, assumed quality $quality\n");
&lt;span class="CD_Code_Num" name="xxhid"&gt;18. &lt;/span&gt;        imagedestroy($im);
&lt;span class="CD_Code_Num" name="xxhid"&gt;19. &lt;/span&gt;    }else{
&lt;span class="CD_Code_Num" name="xxhid"&gt;20. &lt;/span&gt;        echo $data;
&lt;span class="CD_Code_Num" name="xxhid"&gt;21. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;22. &lt;/span&gt;}
&lt;span class="CD_Code_Num" name="xxhid"&gt;23. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;24. &lt;/span&gt;$binary_data = file_get_contents($argv[1]);
&lt;span class="CD_Code_Num" name="xxhid"&gt;25. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;26. &lt;/span&gt;LossifyJpegImage($binary_data);
&lt;span class="CD_Code_Num" name="xxhid"&gt;27. &lt;/span&gt;?&amp;gt;&lt;/pre&gt;
&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input onclick="ToggleLineNumbers(this.checked, this.parentNode)" type="checkbox" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Now run the sccript giving it a filename on the command line and pipe the output to a file.
&lt;br /&gt;
&lt;table class="CD_Console_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;br /&gt;
..workspace&amp;gt;php compression.php oldfile.jpg &amp;gt; newfile.jpg&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
The algorithm is modelled very simply and it is based on empirical tests. It is assumed that it holds true for most images. It is based on this equation that the quality of the image is ascertained.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-50_I1AyTQdY/TsThqEskLOI/AAAAAAAABMU/OVeIC_k34No/s1600/php_image_compression.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="192" src="http://3.bp.blogspot.com/-50_I1AyTQdY/TsThqEskLOI/AAAAAAAABMU/OVeIC_k34No/s320/php_image_compression.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-2884086905166550294?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/TKV_P9Mz4DZZPIgDuHpNtLFta3U/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/TKV_P9Mz4DZZPIgDuHpNtLFta3U/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/TKV_P9Mz4DZZPIgDuHpNtLFta3U/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/TKV_P9Mz4DZZPIgDuHpNtLFta3U/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/zZSkgIStd30" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2011/11/image-compression-using-php.html</link><author>noreply@blogger.com (righteous)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-50_I1AyTQdY/TsThqEskLOI/AAAAAAAABMU/OVeIC_k34No/s72-c/php_image_compression.jpg" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-5090521053392569649</guid><pubDate>Sun, 16 Oct 2011 00:51:00 +0000</pubDate><atom:updated>2011-10-15T17:51:58.971-07:00</atom:updated><title>Comment on Free Proxies</title><description>Please feel free to leave any kind of comment. Helpful or not. Maybe you love our proxy, maybe you think it really stinks, we don't care what you think, just leave us any kind of comment and we will delete the unking ones :).&lt;br /&gt;
&lt;br /&gt;
Please state which proxy you were using - web proxy or the ip:port proxy.&lt;br /&gt;
&lt;br /&gt;
Thanks!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-5090521053392569649?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/89mGNRrOT0V_nTNSL2L3HqR-gVA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/89mGNRrOT0V_nTNSL2L3HqR-gVA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/89mGNRrOT0V_nTNSL2L3HqR-gVA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/89mGNRrOT0V_nTNSL2L3HqR-gVA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/dgwKThPBQgw" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2011/10/comment-on-free-proxies.html</link><author>noreply@blogger.com (righteous)</author><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-1341409013724189869</guid><pubDate>Tue, 16 Aug 2011 02:02:00 +0000</pubDate><atom:updated>2011-08-15T19:29:40.312-07:00</atom:updated><title>STL Starter 1 - User Defined Predicate Functions (Standard Template LIbrary)</title><description>&lt;b&gt;Aim&lt;/b&gt;&lt;br /&gt;
The aim of this example is to show how you can use user defined binary and unary predicate functions (not functors) and the bind2nd with the Standard Template Library. We will be using a vector for this purpose, but it applies for all containers.

In this example we use a class that represents a complex number. Complex numbers are compared by squaring both real and imaginary components and adding them together.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Line 50&lt;/b&gt; Use our user defined &lt;i&gt;ValueGreaterThan20&lt;/i&gt; unary predicate in the find_if&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Line 54&lt;/b&gt; Use the &lt;i&gt;ValueGreaterThan&lt;/i&gt; Binary predicate, but we use bind2nd to make it unary.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Line 57&lt;/b&gt; Use the binary predicate &lt;i&gt;SortValueGreaerThan&lt;/i&gt; in the sort function.&lt;br /&gt;
&lt;br /&gt;
Do not forget to use ptr_fun&amp;nbsp; when passing the function pointers to the bind2nd. Ptr_fun takes a function pointer as its argument and returns a function pointer adapter. If you forget you will get some errors such as below &lt;br /&gt;
&lt;i&gt;&lt;span style="color: red;"&gt;../include/c++/backward/binders.h:138: error: 'bool ()(ComplexNumber, int)' is not a class, struct, or union type&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;table border="1" class="CD_Code_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre class="CD_Code_Words" id="xxpre"&gt;&lt;span class="CD_Code_Num" name="xxhid"&gt; 1. &lt;/span&gt;#include &amp;lt;vector&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 2. &lt;/span&gt;#include &amp;lt;functional&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 3. &lt;/span&gt;#include &amp;lt;algorithm&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 4. &lt;/span&gt;#include &amp;lt;iostream&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 5. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 6. &lt;/span&gt;using namespace std;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 7. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 8. &lt;/span&gt;template &amp;lt;class Operation, class T&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 9. &lt;/span&gt;  bool ninjax (const Operation&amp;amp; op, const T&amp;amp; x)
&lt;span class="CD_Code_Num" name="xxhid"&gt;10. &lt;/span&gt;{
&lt;span class="CD_Code_Num" name="xxhid"&gt;11. &lt;/span&gt;    return true;
&lt;span class="CD_Code_Num" name="xxhid"&gt;12. &lt;/span&gt; }
&lt;span class="CD_Code_Num" name="xxhid"&gt;13. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;14. &lt;/span&gt;class ComplexNumber{
&lt;span class="CD_Code_Num" name="xxhid"&gt;15. &lt;/span&gt;public:
&lt;span class="CD_Code_Num" name="xxhid"&gt;16. &lt;/span&gt;    ComplexNumber(int r, int i): real(r), imaginery(i){}
&lt;span class="CD_Code_Num" name="xxhid"&gt;17. &lt;/span&gt;    int real;
&lt;span class="CD_Code_Num" name="xxhid"&gt;18. &lt;/span&gt;    int imaginery;
&lt;span class="CD_Code_Num" name="xxhid"&gt;19. &lt;/span&gt;};
&lt;span class="CD_Code_Num" name="xxhid"&gt;20. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;21. &lt;/span&gt;//Unary predicate
&lt;span class="CD_Code_Num" name="xxhid"&gt;22. &lt;/span&gt;bool ValueGreaterThan20(ComplexNumber c){
&lt;span class="CD_Code_Num" name="xxhid"&gt;23. &lt;/span&gt;    return c.imaginery*c.imaginery+c.real*c.real &amp;gt; 20*20; 
&lt;span class="CD_Code_Num" name="xxhid"&gt;24. &lt;/span&gt;}
&lt;span class="CD_Code_Num" name="xxhid"&gt;25. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;26. &lt;/span&gt;//Binary predicate
&lt;span class="CD_Code_Num" name="xxhid"&gt;27. &lt;/span&gt;bool ValueGreaterThan(ComplexNumber c, int i){
&lt;span class="CD_Code_Num" name="xxhid"&gt;28. &lt;/span&gt;    cout&amp;lt;&amp;lt;c.imaginery&amp;lt;&amp;lt;endl;
&lt;span class="CD_Code_Num" name="xxhid"&gt;29. &lt;/span&gt;    return c.imaginery*c.imaginery+c.real*c.real &amp;gt; i*i;
&lt;span class="CD_Code_Num" name="xxhid"&gt;30. &lt;/span&gt;}
&lt;span class="CD_Code_Num" name="xxhid"&gt;31. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;32. &lt;/span&gt;//Binary predicate
&lt;span class="CD_Code_Num" name="xxhid"&gt;33. &lt;/span&gt;bool SortValueGreaterThan(ComplexNumber c, ComplexNumber b){
&lt;span class="CD_Code_Num" name="xxhid"&gt;34. &lt;/span&gt;    cout&amp;lt;&amp;lt;c.imaginery&amp;lt;&amp;lt;endl;
&lt;span class="CD_Code_Num" name="xxhid"&gt;35. &lt;/span&gt;    return c.imaginery*c.imaginery+c.real*c.real &amp;gt; b.imaginery*b.imaginery+b.real*b.real;
&lt;span class="CD_Code_Num" name="xxhid"&gt;36. &lt;/span&gt;}
&lt;span class="CD_Code_Num" name="xxhid"&gt;37. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;38. &lt;/span&gt;int main(){
&lt;span class="CD_Code_Num" name="xxhid"&gt;39. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;40. &lt;/span&gt;    vector&amp;lt;ComplexNumber&amp;gt; complexNumbersVec;
&lt;span class="CD_Code_Num" name="xxhid"&gt;41. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;42. &lt;/span&gt;    complexNumbersVec.push_back(ComplexNumber(5,5));
&lt;span class="CD_Code_Num" name="xxhid"&gt;43. &lt;/span&gt;    complexNumbersVec.push_back(ComplexNumber(10,15));
&lt;span class="CD_Code_Num" name="xxhid"&gt;44. &lt;/span&gt;    complexNumbersVec.push_back(ComplexNumber(15,5));
&lt;span class="CD_Code_Num" name="xxhid"&gt;45. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;46. &lt;/span&gt;    complexNumbersVec.push_back(ComplexNumber(10,20));
&lt;span class="CD_Code_Num" name="xxhid"&gt;47. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;48. &lt;/span&gt;    //Using bind2nd with user defined unary predicate function
&lt;span class="CD_Code_Num" name="xxhid"&gt;49. &lt;/span&gt;    vector&amp;lt; ComplexNumber &amp;gt;::iterator location =    
&lt;span class="CD_Code_Num" name="xxhid"&gt;50. &lt;/span&gt;        find_if(complexNumbersVec.begin(), complexNumbersVec.end(), ValueGreaterThan20);
&lt;span class="CD_Code_Num" name="xxhid"&gt;51. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;52. &lt;/span&gt;    //Using bind2nd with user defined binary predicate function
&lt;span class="CD_Code_Num" name="xxhid"&gt;53. &lt;/span&gt;    vector&amp;lt; ComplexNumber &amp;gt;::iterator location2 =    
&lt;span class="CD_Code_Num" name="xxhid"&gt;54. &lt;/span&gt;        find_if(complexNumbersVec.begin(), complexNumbersVec.end(), bind2nd(ptr_fun(ValueGreaterThan), 0));
&lt;span class="CD_Code_Num" name="xxhid"&gt;55. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;56. &lt;/span&gt;    //Using sort with user defined binary predicate funcion
&lt;span class="CD_Code_Num" name="xxhid"&gt;57. &lt;/span&gt;    sort (complexNumbersVec.begin(), complexNumbersVec.end(), SortValueGreaterThan);
&lt;span class="CD_Code_Num" name="xxhid"&gt;58. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;59. &lt;/span&gt;    for (location=complexNumbersVec.begin(); location!=complexNumbersVec.end(); location++)
&lt;span class="CD_Code_Num" name="xxhid"&gt;60. &lt;/span&gt;        cout &amp;lt;&amp;lt; " " &amp;lt;&amp;lt; location-&amp;gt;real &amp;lt;&amp;lt;", "&amp;lt;&amp;lt;location-&amp;gt;imaginery&amp;lt;&amp;lt;"i"&amp;lt;&amp;lt;endl;
&lt;span class="CD_Code_Num" name="xxhid"&gt;61. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;62. &lt;/span&gt;}&lt;/pre&gt;
&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input onclick="ToggleLineNumbers(this.checked, this.parentNode)" type="checkbox" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-1341409013724189869?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/TOe6SjEIIKY4MWEHPF1lQ-3Id20/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/TOe6SjEIIKY4MWEHPF1lQ-3Id20/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/TOe6SjEIIKY4MWEHPF1lQ-3Id20/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/TOe6SjEIIKY4MWEHPF1lQ-3Id20/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/sU79MQgyM1Y" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2011/08/stl-starter-1-user-defined-predicates.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-910017140950788708</guid><pubDate>Thu, 14 Jul 2011 09:04:00 +0000</pubDate><atom:updated>2011-07-14T02:17:39.973-07:00</atom:updated><title>Check Page Rank of all Blog Posts</title><description>Here is a nifty tool to ffind out all the page ranks of your individual blog posts. All you have to feed it is your blog's atom or rss feed and the tool will display the PR of all your posts.&lt;br /&gt;
&lt;script src="http://www.google.com/jsapi?key=ABQIAAAAnaimGdQdwepBgDz4IoiW8RSghK-DezSI2aqoaWIe1mHkPbfVshSXFvDV0C3UE-W0jWMOBNop7NItAQ" type="text/javascript"&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;script src="http://codediaries.com/jediseo/seolinkpr.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;form onsubmit="GetLinkPR(document.getElementById('rssfeedurl').value, 'linkpr_container'); return false"&gt;
&lt;table class="SEO_toolcontrols"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Rss feed url &lt;input id="rssfeedurl" size="75" type="text" value="http://www.tidytutorials.com/atom.xml?redirect=false&amp;amp;max-results=20" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;input type="submit" value="Check PR of all posts" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/form&gt;
&lt;br /&gt;
&lt;div id="linkpr_container"&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style="color: red;"&gt;
For wordpress blogs the RSS feed is "/feed" after your blog. ex: http://myblog.wordpress.com/feed&lt;/div&gt;
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-910017140950788708?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/KAAnKB-omJ2w0qD_FC97n8bGD6w/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KAAnKB-omJ2w0qD_FC97n8bGD6w/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/KAAnKB-omJ2w0qD_FC97n8bGD6w/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KAAnKB-omJ2w0qD_FC97n8bGD6w/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/P9spW4iaFFk" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2011/07/check-page-rank-of-all-blog-posts.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-2985921403055685358</guid><pubDate>Thu, 14 Jul 2011 04:25:00 +0000</pubDate><atom:updated>2011-07-14T04:24:43.237-07:00</atom:updated><title>Automatically Create Sitemaps for Blogs</title><description>Automatically create sitemaps for all your blogs by enteirn the atom or RSS feeds. You can create a sitemap in HTML for search bots. In human readable form or even XML using the sitemap protocol.&lt;br /&gt;
&lt;script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAnaimGdQdwepBgDz4IoiW8RSghK-DezSI2aqoaWIe1mHkPbfVshSXFvDV0C3UE-W0jWMOBNop7NItAQ"&gt;&lt;/script&gt;&lt;script type="text/javascript" src="http://codediaries.com/jediseo/sitemapgenerator.js"&gt;&lt;/script&gt;&lt;script&gt;
function SelectSitemapType(x){
    if(x=="xml")
        document.getElementById("xmlcontrols_container").style.visibility="visible";
    else
        document.getElementById("xmlcontrols_container").style.visibility="hidden";        
}
function SubmitSitemap(){
    SiteMapGenerator(document.getElementById("rssfeedurl").value, 
        document.getElementById("typeselect_control").options[document.getElementById("typeselect_control").selectedIndex].value, 
        1000, 
        document.getElementById("xmlcontrols_freq").options[document.getElementById("xmlcontrols_freq").selectedIndex].value,
        document.getElementById("xmlcontrols_date").checked,
        document.getElementById("xmlcontrols_priority").checked,
        "sitemap_container");
}
&lt;/script&gt;&lt;br /&gt;
&lt;form onSubmit="SubmitSitemap();return false"&gt;&lt;table class="SEO_toolcontrols"&gt;&lt;tr&gt;&lt;td colspan="2"&gt;Rss feed url &lt;input type="text" id="rssfeedurl" size="75" value="http://www.tidytutorials.com/atom.xml?redirect=false&amp;max-results=100"/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;select id="typeselect_control" onChange="SelectSitemapType(this.options[this.selectedIndex].value)"&gt;&lt;br /&gt;
&lt;option value="xml"&gt;XML (Sitemap Protocol)&lt;/option&gt;&lt;br /&gt;
&lt;option value="html"&gt;HTML&lt;/option&gt;&lt;br /&gt;
&lt;option value="human"&gt;Human readable HTML&lt;/option&gt;&lt;br /&gt;
&lt;/select&gt;&lt;/td&gt;&lt;td&gt;&lt;div id="xmlcontrols_container"&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;Include publish date &lt;a href="javascript:alert('(optional) Set the create date as last modified date')"&gt;?&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="checkbox" id="xmlcontrols_date"&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Assign Incremental Priority &lt;a href="javascript:alert('(optional) Assign latest page with priority of 1 and oldest page with 0.1')"&gt;?&lt;/a&gt;&lt;/td&gt;&lt;td&gt; &lt;input type="checkbox" id="xmlcontrols_priority"&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Update frequency &lt;a href="javascript:alert('(optional) How frequently are the pages updated')"&gt;?&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;select id="xmlcontrols_freq"&gt;&lt;br /&gt;
&lt;option value="always"&gt;always&lt;/option&gt;&lt;br /&gt;
&lt;option value="hourly"&gt;hourly&lt;/option&gt;&lt;br /&gt;
&lt;option value="daily"&gt;daily&lt;/option&gt;&lt;br /&gt;
&lt;option value="weekly"&gt;weekly&lt;/option&gt;&lt;br /&gt;
&lt;option value="monthly"&gt;monthly&lt;/option&gt;&lt;br /&gt;
&lt;option value="yearly"&gt;yearly&lt;/option&gt;&lt;br /&gt;
&lt;option value="never"&gt;never&lt;/option&gt;&lt;br /&gt;
&lt;option selected value=""&gt;&lt;/option&gt;&lt;br /&gt;
&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;input type="submit" value="Create Sitemap"/&gt;&lt;/td&gt;&lt;td&gt;&lt;div id="sitemap_progress"&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/form&gt;&lt;textarea onClick="this.select()" cols="75" rows="10" id="sitemap_container"&gt;&lt;/textarea&gt;&lt;br /&gt;
&lt;div id="html_panel"&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-2985921403055685358?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/AfKHwN_LTqFkhgO1xAArCC4hiKY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/AfKHwN_LTqFkhgO1xAArCC4hiKY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/AfKHwN_LTqFkhgO1xAArCC4hiKY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/AfKHwN_LTqFkhgO1xAArCC4hiKY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/c8SBjMEnJJU" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2011/07/automatically-create-sitemaps-for-blogs.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-6397909454089637010</guid><pubDate>Thu, 14 Jul 2011 04:14:00 +0000</pubDate><atom:updated>2011-07-14T04:13:39.705-07:00</atom:updated><title>Compare Page Ranks of Multiple Sites</title><description>Use this tool to compare the page ranks of multiple sites. No messy capthas. Quickly and easily find out the PR of multiple sites.&lt;br /&gt;
&lt;script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAnaimGdQdwepBgDz4IoiW8RSghK-DezSI2aqoaWIe1mHkPbfVshSXFvDV0C3UE-W0jWMOBNop7NItAQ"&gt;&lt;/script&gt;&lt;br /&gt;
&lt;script type="text/javascript" src="http://codediaries.com/jediseo/seolinkpr.js"&gt;&lt;/script&gt;&lt;br /&gt;
&lt;form onSubmit="GetSingleShotPR('singlhrf','singleshotpr_container'); return false"&gt;&lt;table border="0" class="SEO_toolcontrols"&gt;&lt;tr&gt;&lt;td&gt;URL 1 &lt;input type="text" id="singlhrf1" size="50" value="codediaries.blogspot.com"/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;URL 2 &lt;input type="text" id="singlhrf2" size="50" value="www.tidytutorials.com"/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;URL 3 &lt;input type="text" id="singlhrf3" size="50" value="codediaries.com"/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;URL 4 &lt;input type="text" id="singlhrf4" size="50" value="dirtcheapproxies.com"/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;URL 5 &lt;input type="text" id="singlhrf5" size="50" value=""/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;input type="submit" value="Check PR"/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;br /&gt;
&lt;/form&gt;&lt;br /&gt;
&lt;div id="singleshotpr_container"&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-6397909454089637010?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/HTKipUqEdRl4w4ldMCKwGRM6z7E/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/HTKipUqEdRl4w4ldMCKwGRM6z7E/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/HTKipUqEdRl4w4ldMCKwGRM6z7E/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/HTKipUqEdRl4w4ldMCKwGRM6z7E/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/cj7H2AlVzBs" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2011/07/compare-page-ranks-of-multiple-sites.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-2738447342215638602</guid><pubDate>Tue, 29 Mar 2011 04:05:00 +0000</pubDate><atom:updated>2011-11-20T16:58:31.891-08:00</atom:updated><title>C/C++ Forking and Using Pipes to Exchange Data Between Parent and Child</title><description>&lt;b&gt;Aim&lt;/b&gt;&lt;br /&gt;
Pipes are form of inter-process communication. They are very useful to synchronize access to resources, notification and exchanging data. In this example we will use fork to spawn a process and then communicate between the child process and parent process using pipes.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Assumptions&lt;/b&gt;&lt;br /&gt;
You have a C++ compiler (g++ in this example) and access to a linux environment or cygwin.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Application&lt;/b&gt;&lt;br /&gt;
In this simple example we use pipe to create the pipes. We then fork and use write and read on the pipes to exchange information. Also note that we call waitpid in the parent to stop it exiting before the child has quit.&lt;br /&gt;
&lt;br /&gt;
Let us have a look at the code below. This file will be saved as forking_pipes.cpp&lt;br /&gt;
&lt;table class="CD_Code_Table" border="1"&gt;&lt;tr&gt;&lt;td&gt;&lt;pre id="xxpre" class="CD_Code_Words"&gt;&lt;span name="xxhid" class="CD_Code_Num"&gt; 1. &lt;/span&gt;#include &amp;lt;stdlib.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 2. &lt;/span&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 3. &lt;/span&gt;#include &amp;lt;string.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 4. &lt;/span&gt;#include &amp;lt;unistd.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 5. &lt;/span&gt;#include &amp;lt;errno.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 6. &lt;/span&gt;#include &amp;lt;sys/types.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 7. &lt;/span&gt;#include &amp;lt;sys/wait.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 8. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 9. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;10. &lt;/span&gt;int main(int argv, char** argc){
&lt;span name="xxhid" class="CD_Code_Num"&gt;11. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;12. &lt;/span&gt;    int pipes[2];
&lt;span name="xxhid" class="CD_Code_Num"&gt;13. &lt;/span&gt;    char readbuf[64]={0};
&lt;span name="xxhid" class="CD_Code_Num"&gt;14. &lt;/span&gt;    char writebuf[64]={0};
&lt;span name="xxhid" class="CD_Code_Num"&gt;15. &lt;/span&gt;    int childpid;
&lt;span name="xxhid" class="CD_Code_Num"&gt;16. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;17. &lt;/span&gt;    if(pipe(pipes)&amp;lt;0){
&lt;span name="xxhid" class="CD_Code_Num"&gt;18. &lt;/span&gt;        perror("Pipe allocation failed");
&lt;span name="xxhid" class="CD_Code_Num"&gt;19. &lt;/span&gt;        exit(0);
&lt;span name="xxhid" class="CD_Code_Num"&gt;20. &lt;/span&gt;    }
&lt;span name="xxhid" class="CD_Code_Num"&gt;21. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;22. &lt;/span&gt;    switch(childpid=fork()){
&lt;span name="xxhid" class="CD_Code_Num"&gt;23. &lt;/span&gt;        case -1:
&lt;span name="xxhid" class="CD_Code_Num"&gt;24. &lt;/span&gt;            perror("Problem with fork\n");
&lt;span name="xxhid" class="CD_Code_Num"&gt;25. &lt;/span&gt;            break;
&lt;span name="xxhid" class="CD_Code_Num"&gt;26. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;27. &lt;/span&gt;        case 0:  //Child
&lt;span name="xxhid" class="CD_Code_Num"&gt;28. &lt;/span&gt;            //Writing
&lt;span name="xxhid" class="CD_Code_Num"&gt;29. &lt;/span&gt;            sprintf(writebuf, "Hello from %d", getpid());
&lt;span name="xxhid" class="CD_Code_Num"&gt;30. &lt;/span&gt;            if(write(pipes[1], writebuf, strlen(writebuf))&amp;lt;0){
&lt;span name="xxhid" class="CD_Code_Num"&gt;31. &lt;/span&gt;                perror("Child write failed\n");
&lt;span name="xxhid" class="CD_Code_Num"&gt;32. &lt;/span&gt;            }
&lt;span name="xxhid" class="CD_Code_Num"&gt;33. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;34. &lt;/span&gt;            //Reading
&lt;span name="xxhid" class="CD_Code_Num"&gt;35. &lt;/span&gt;            if(read(pipes[0], readbuf, 64)&amp;gt;0){
&lt;span name="xxhid" class="CD_Code_Num"&gt;36. &lt;/span&gt;                printf("Child read - %s\n", readbuf);
&lt;span name="xxhid" class="CD_Code_Num"&gt;37. &lt;/span&gt;            }
&lt;span name="xxhid" class="CD_Code_Num"&gt;38. &lt;/span&gt;            else{
&lt;span name="xxhid" class="CD_Code_Num"&gt;39. &lt;/span&gt;                perror("child read failed\n");
&lt;span name="xxhid" class="CD_Code_Num"&gt;40. &lt;/span&gt;            }
&lt;span name="xxhid" class="CD_Code_Num"&gt;41. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;42. &lt;/span&gt;            break;
&lt;span name="xxhid" class="CD_Code_Num"&gt;43. &lt;/span&gt;        
&lt;span name="xxhid" class="CD_Code_Num"&gt;44. &lt;/span&gt;        default: //Parent
&lt;span name="xxhid" class="CD_Code_Num"&gt;45. &lt;/span&gt;            //Reading
&lt;span name="xxhid" class="CD_Code_Num"&gt;46. &lt;/span&gt;            if(read(pipes[0], readbuf, 64)&amp;gt;0){
&lt;span name="xxhid" class="CD_Code_Num"&gt;47. &lt;/span&gt;                printf("Parent read - %s\n", readbuf);
&lt;span name="xxhid" class="CD_Code_Num"&gt;48. &lt;/span&gt;            }
&lt;span name="xxhid" class="CD_Code_Num"&gt;49. &lt;/span&gt;            else{
&lt;span name="xxhid" class="CD_Code_Num"&gt;50. &lt;/span&gt;                perror("Parent read failed\n");
&lt;span name="xxhid" class="CD_Code_Num"&gt;51. &lt;/span&gt;            }
&lt;span name="xxhid" class="CD_Code_Num"&gt;52. &lt;/span&gt;            
&lt;span name="xxhid" class="CD_Code_Num"&gt;53. &lt;/span&gt;            //Writing
&lt;span name="xxhid" class="CD_Code_Num"&gt;54. &lt;/span&gt;            sprintf(writebuf, "Hello from parent");
&lt;span name="xxhid" class="CD_Code_Num"&gt;55. &lt;/span&gt;            if(write(pipes[1], writebuf, strlen(writebuf))&amp;lt;0){
&lt;span name="xxhid" class="CD_Code_Num"&gt;56. &lt;/span&gt;                perror("Parent write failed\n");
&lt;span name="xxhid" class="CD_Code_Num"&gt;57. &lt;/span&gt;            }
&lt;span name="xxhid" class="CD_Code_Num"&gt;58. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;59. &lt;/span&gt;            waitpid(childpid,0,0);
&lt;span name="xxhid" class="CD_Code_Num"&gt;60. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;61. &lt;/span&gt;            break;
&lt;span name="xxhid" class="CD_Code_Num"&gt;62. &lt;/span&gt;    }
&lt;span name="xxhid" class="CD_Code_Num"&gt;63. &lt;/span&gt;}&lt;/pre&gt;&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input type="checkbox" onclick="ToggleLineNumbers(this.checked, this.parentNode)" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;br /&gt;
Compile the code&lt;br /&gt;
&lt;table class="CD_Console_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;br /&gt;
..workspace\Forkingpipes&amp;gt;g++  forking_pipes.cpp &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
pipes[0] is the reading pipe and pipe[1] is the writing pipe. In the parent whatever you write to pipe[1] can be read at the child using pipe[0]. In the child whatever you write to pipe[1] can be read at the Parent using pipe[0].&lt;br /&gt;
&lt;br /&gt;
If you wish to spawn multiple children then you have to have multiple pipe pairs. This means you will have to use select in the parent to find out which pipes are ready for writing and reading.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-2738447342215638602?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/y4eF7WhJ8epS4HZZaNOJhhOEBLQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/y4eF7WhJ8epS4HZZaNOJhhOEBLQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/y4eF7WhJ8epS4HZZaNOJhhOEBLQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/y4eF7WhJ8epS4HZZaNOJhhOEBLQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/K5yGIURyiMM" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2011/03/forking-and-using-pipes-to-exchange.html</link><author>noreply@blogger.com (righteous)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-7437987505661561155</guid><pubDate>Fri, 18 Mar 2011 03:29:00 +0000</pubDate><atom:updated>2012-02-03T03:11:31.760-08:00</atom:updated><title>Bandwidth Throttling and Connection Limiting for Web, Proxy and Email Servers</title><description>&lt;br /&gt;
&lt;b&gt;Aim&lt;/b&gt;&lt;br /&gt;
The aim of this program is to create a self regulating tcp/ip connection forwarding program that can limit connections and do per IP bandwidth throttling and connection limiting. This can be used to give bandwidth throttling and connection limiting to proxy, email, web and ftp servers. It will work with any server such as tomcat, apache, glassfish, squid, weblogic etc as it runs as a separate standalone application.  This software is now called SpeedBump. This can be easily used to throttle Apache and Squid very easily.&lt;br /&gt;
&lt;br /&gt;
Resources&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt; &lt;a href="http://code.google.com/p/kimbula/" rel="nofollow"&gt;Project Kimbula&lt;/a&gt;. - Project hosted on google's code hosting&lt;/li&gt;
&lt;li&gt;&lt;a href="http://code.google.com/p/kimbula/downloads/list" rel="nofollow"&gt;Download Source Now&lt;/a&gt; - Download source code.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://p/speedbump.html"&gt;Installation&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;The key features are&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Set the maximum number of con-current connections. Similar to apache's MaxClients.&lt;/li&gt;
&lt;li&gt;Set maximum con-current connections per ip. Similar to conn-limit in iptables.&lt;/li&gt;
&lt;li&gt;Set the new connection burst and average connections per second per ip. Similar to limit-burst in iptables.&lt;/li&gt;
&lt;li&gt;Set the uplink and downlink throughput per ip. The throughput will be divided amongst the concurrent connections per ip.&lt;/li&gt;
&lt;li&gt;Once a certain amount of bytes has been uploaded/downloaded drop the available throughput for a time period.&lt;/li&gt;
&lt;li&gt;Written in simple, easy to understand C.&lt;/li&gt;
&lt;li&gt;Add and remove headers for upstream and downstream connections&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
Let us look at a typical scenario below. One user has opened up a lot of connections to the server and is downloading or uploading a lot of data. &lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://lh3.googleusercontent.com/--n-NM7VuXzc/TYK1ovQugCI/AAAAAAAABHY/I7X-C2aRWSc/s1600/before.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="223" src="https://lh3.googleusercontent.com/--n-NM7VuXzc/TYK1ovQugCI/AAAAAAAABHY/I7X-C2aRWSc/s400/before.jpg" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&amp;nbsp;The server now has less connections available for the other users and the most of the bandwidth is being used by the one greedy user. This usually results in a poor experience for other users.&lt;br /&gt;
&lt;br /&gt;
Now we bring in the SpeedBump. This can be located on a seperate server or co-located with the existing server. For purposes of illustration let us show them on separate servers. Now all the connections have to first go through SpeedBump before they can get to the server.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://lh3.googleusercontent.com/-PBL6j0qy01o/TYK2kRV3tXI/AAAAAAAABHc/BZpOh28jPA0/s1600/after.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="145" src="https://lh3.googleusercontent.com/-PBL6j0qy01o/TYK2kRV3tXI/AAAAAAAABHc/BZpOh28jPA0/s400/after.jpg" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
SpeedBump will now reject all connections above the connection limit and throttle the bandwidth used by the greedy user. This reduces the load on the web/proxy server and gives a consistent experience to all the users.&lt;br /&gt;
&lt;ul&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-7437987505661561155?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/dIX0VfrN__PxZP4gNe-9LxZ-NtA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dIX0VfrN__PxZP4gNe-9LxZ-NtA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/dIX0VfrN__PxZP4gNe-9LxZ-NtA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dIX0VfrN__PxZP4gNe-9LxZ-NtA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/-VJQSq0_dTk" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2011/03/bandwidth-throttling-and-connection.html</link><author>noreply@blogger.com (righteous)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://lh3.googleusercontent.com/--n-NM7VuXzc/TYK1ovQugCI/AAAAAAAABHY/I7X-C2aRWSc/s72-c/before.jpg" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-1298084494285524173</guid><pubDate>Fri, 18 Mar 2011 03:11:00 +0000</pubDate><atom:updated>2011-04-04T04:30:30.069-07:00</atom:updated><title>Software Maintained by TidyTutorials</title><description>Here is a list of the software currently maintained by TidyTutorials.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://2.bp.blogspot.com/-lx6j1-84Ck0/TVYlIi-kqNI/AAAAAAAABG8/c0lcaf-W9-Y/s1600/mugatiya.jpg" imageanchor="1" rel="nofollow" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-lx6j1-84Ck0/TVYlIi-kqNI/AAAAAAAABG8/c0lcaf-W9-Y/s1600/mugatiya.jpg" /&gt;&lt;/a&gt;&lt;b&gt;Free IP:Port Proxy Emulator&lt;/b&gt;&lt;br /&gt;
This emulates the behaviour of an ip:port or elite proxy, but is written in php and runs inside an Apache web server. The only ip:port proxy emulator able to push rated advertisements! Contains Turing Test. The source code and installation instruction can be found at &lt;a href="http://www.tidytutorials.com/2010/11/apachephp-ipport-proxy-emulator.html"&gt;Project Mugatiya&lt;/a&gt;.&lt;br /&gt;
Basic requirement is apache + php + should be able to configure .htaccess.&lt;br /&gt;
Hosting : VPS/VDS&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://3.bp.blogspot.com/-v2nvx-Gr4W8/TVaDf-TCuXI/AAAAAAAABHA/Aijxp2-9kJQ/s1600/uguduwa.jpg" imageanchor="1" rel="nofollow" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-v2nvx-Gr4W8/TVaDf-TCuXI/AAAAAAAABHA/Aijxp2-9kJQ/s1600/uguduwa.jpg" /&gt;&lt;/a&gt;&lt;b&gt;Free Web Proxy&lt;/b&gt;&lt;br /&gt;
Yet another web proxy such as Glype or PHProxy. Display advertisements and has the ability do display rated advertisements. Has unique ability to be rented out! The source code and installation instruction can be found at  &lt;a href="http://www.tidytutorials.com/2010/12/apachephp-web-proxy.html"&gt;Project Uguduwa&lt;/a&gt;.&lt;br /&gt;
Basic requirement is apache + php + shoulb be able to configure .htaccess.&lt;br /&gt;
Hosting: Shared or VPS/VDS&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://3.bp.blogspot.com/-D4tlS4vRUdo/TV31PY8hedI/AAAAAAAABHM/pYnRYUwYn3Q/s1600/kimbula.jpg" imageanchor="1" rel="nofollow" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-D4tlS4vRUdo/TV31PY8hedI/AAAAAAAABHM/pYnRYUwYn3Q/s1600/kimbula.jpg" /&gt;&lt;/a&gt;&lt;b&gt;Free SpeedBump Software&lt;/b&gt;&lt;br /&gt;
SpeedBump runs in front of a proxy server or web server and regulates connections and throttles bandwitdh. Don't let a single IP hog all your connections and bandwith. Set max concurrent connections per ip, max burst and average connections per ip, througput per ip and many more. Can be modified to be used as a proxy. The source code and installation instruction can be found at  &lt;a href="http://www.tidytutorials.com/2011/03/bandwidth-throttling-and-connection.html"&gt;Project Kimbula&lt;/a&gt;.&lt;br /&gt;
Basic requriement gcc/g++&lt;br /&gt;
Hosing: VPS/VDS&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Common Database Pool Template&lt;/b&gt;&lt;br /&gt;
A common database pool using a template that will support Oracle, MySQL and SQLServer and use as common a process and methodology as possible to access connections. More information can be found &lt;a href="http://www.tidytutorials.com/2010/01/common-database-pool-that-supports.html"&gt;here&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-1298084494285524173?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/eTOm2bfIai7uujtU-GODHvnoUXo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eTOm2bfIai7uujtU-GODHvnoUXo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/eTOm2bfIai7uujtU-GODHvnoUXo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eTOm2bfIai7uujtU-GODHvnoUXo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/tAUtM_P0Bj4" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2011/03/software-maintained-by-tidytutorials.html</link><author>noreply@blogger.com (righteous)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-lx6j1-84Ck0/TVYlIi-kqNI/AAAAAAAABG8/c0lcaf-W9-Y/s72-c/mugatiya.jpg" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-4383728393435456352</guid><pubDate>Fri, 04 Mar 2011 00:08:00 +0000</pubDate><atom:updated>2012-02-23T16:31:37.148-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mtrace</category><category domain="http://www.blogger.com/atom/ns#">memory leak</category><category domain="http://www.blogger.com/atom/ns#">muntrace</category><category domain="http://www.blogger.com/atom/ns#">C++</category><title>Tracing Memory Leak in C/C++ Using Mtrace</title><description>&lt;b&gt;Aim&lt;/b&gt;&lt;br /&gt;
The aim of this tutorial is to show how you how to trace a memory leak in a C/C++ program using mcheck. This allows you to match your &lt;i&gt;&lt;b&gt;malloc&lt;/b&gt;&lt;/i&gt;, &lt;i&gt;&lt;b&gt;realloc&lt;/b&gt;&lt;/i&gt;, &lt;i&gt;&lt;b&gt;calloc&lt;/b&gt;&lt;/i&gt; etc to your &lt;b&gt;&lt;i&gt;free&lt;/i&gt;&lt;/b&gt;s. Mtrace functionality is available with the gnu C/C++ dev packages.&lt;br /&gt;
&lt;br /&gt;
Let us take a look at the sample program below. Let's call this file leaker.c&lt;br /&gt;
&lt;table border="1" class="CD_Code_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre class="CD_Code_Words" id="xxpre"&gt;&lt;span class="CD_Code_Num" name="xxhid"&gt; 1. &lt;/span&gt;#include &amp;lt;stdlib.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 2. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 3. &lt;/span&gt;#include &amp;lt;mcheck.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 4. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 5. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 6. &lt;/span&gt;int main(int argv, char** argc){
&lt;span class="CD_Code_Num" name="xxhid"&gt; 7. &lt;/span&gt;        mtrace();
&lt;span class="CD_Code_Num" name="xxhid"&gt; 8. &lt;/span&gt;        char * c1 = malloc(10*sizeof(char));
&lt;span class="CD_Code_Num" name="xxhid"&gt; 9. &lt;/span&gt;        char * c2 = malloc(10*sizeof(char));
&lt;span class="CD_Code_Num" name="xxhid"&gt;10. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;11. &lt;/span&gt;        c1[2]='a';
&lt;span class="CD_Code_Num" name="xxhid"&gt;12. &lt;/span&gt;        c2[2]='b';
&lt;span class="CD_Code_Num" name="xxhid"&gt;13. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;14. &lt;/span&gt;        free(c2);
&lt;span class="CD_Code_Num" name="xxhid"&gt;15. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;16. &lt;/span&gt;        muntrace();
&lt;span class="CD_Code_Num" name="xxhid"&gt;17. &lt;/span&gt;}&lt;/pre&gt;
&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input onclick="ToggleLineNumbers(this.checked, this.parentNode)" type="checkbox" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;b&gt;Line 3&lt;/b&gt; You need to include the mcheck.h header file&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Line 7&lt;/b&gt; call mtrace() to start the tracing.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Line 16&lt;/b&gt; call muntrace to stop the tracing. &lt;br /&gt;
&lt;br /&gt;
As you can see this program has a blatant memory leak, in that we are not freeing pointer C2. Next we have to compile the program with the -g switch so that we can find out at which line the leak is occurring.&lt;br /&gt;
&lt;br /&gt;
&lt;table class="CD_Console_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;br /&gt;
gcc -g leaker.c&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
This will create the default a.out executable. Before we run the program we need to set an environment variable called MALLOC_TRACE. This will contain the location of the tracefile to be created by mtrace/mcheck. The example below is for the bash shell.&lt;br /&gt;
&lt;br /&gt;
&lt;table class="CD_Console_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;br /&gt;
&amp;gt;MALLOC_TRACE=/home/righteous/mt.txt&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Now we run the program. Once the program has run it's course you can take a look at the trace file that it has created.&lt;br /&gt;
&lt;table class="CD_Console_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;br /&gt;
&amp;gt;./a.out&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;cat mt.txt&lt;br /&gt;
= Start&lt;br /&gt;
@ ./a.out:[0x8048456] + 0x8968378 0xa&lt;br /&gt;
@ ./a.out:[0x8048465] + 0x8968388 0xa&lt;br /&gt;
@ ./a.out:[0x8048485] - 0x8968388&lt;br /&gt;
= End&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
To make sense of this file we need another command called mtrace. This is a perl script. This may or may not be available in your environment. You can search for the script and download it.&lt;br /&gt;
Now we run the script and you will see details of the memory leak below.&lt;br /&gt;
&lt;table class="CD_Console_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;br /&gt;
&amp;gt;mtrace a.out mt.txt&lt;br /&gt;
&lt;br /&gt;
Memory not freed:&lt;br /&gt;
-----------------&lt;br /&gt;
Address     Size     Caller&lt;br /&gt;
0x08968378      0xa  at /home/test.c:8  ??:0&lt;br /&gt;
&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Things get a lot trickier in an environment where you are forking process. For instance if you allocate memory, fork and then delete the memory in the child process, this will be flagged as an error as the child never actually allocated the memory. Also if you start mtrace after you have allocated memory or call muntrace before you have freed all your memory, mtrace will highlight these as errors.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-4383728393435456352?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/fbe2Vxd-gBt51lCdJqwLnes-hfc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/fbe2Vxd-gBt51lCdJqwLnes-hfc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/fbe2Vxd-gBt51lCdJqwLnes-hfc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/fbe2Vxd-gBt51lCdJqwLnes-hfc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/zUAkzhXK_5s" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2011/03/tracing-memory-leak-in-cc-using-mtrace.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-2869654729535782923</guid><pubDate>Fri, 17 Dec 2010 04:46:00 +0000</pubDate><atom:updated>2011-03-17T19:55:42.083-07:00</atom:updated><title>Apache/Php Web Proxy</title><description>&lt;b&gt;Aim&lt;/b&gt;&lt;br /&gt;
The aim of this exercise is to create yet another web proxy. This is a url rewriting proxy so the user experience is not the very best. It is written as a Php script, it will run on shared hosting. There is no database requirement.&lt;br /&gt;
&lt;br /&gt;
As this is a fairly substantial project, it is hosted on google at &lt;a href="http://code.google.com/p/uguduwa/" rel="nofollow"&gt;Project uguduwa&lt;/a&gt;. There is also more information at the Wordpress blog &lt;a href="http://projectuguduwa.blogspot.com/" rel="nofollow"&gt;projectuguduwa.blogspot.com&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;The advantages&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Run in shared hosting or Virtual Private or Dedicated server&lt;/li&gt;
&lt;li&gt;Modify content to display adds&lt;/li&gt;
&lt;li&gt;Has a content filter&lt;/li&gt;
&lt;li&gt;Remote Ip, requested url and requested hostname based black and white lists&lt;/li&gt;
&lt;li&gt;Written in simple, easy to understant Php.&lt;/li&gt;
&lt;/ol&gt;Resources&lt;br /&gt;
&lt;ul&gt;&lt;li&gt; &lt;a href="http://code.google.com/p/uguduwa/" rel="nofollow"&gt;Project Uguduwa&lt;/a&gt;. - Project hosted on google's code hosting&lt;/li&gt;
&lt;li&gt;&lt;a href="http://code.google.com/p/uguduwa/downloads/list" rel="nofollow"&gt;Download Source Now&lt;/a&gt; - Download source code and zip file &lt;/li&gt;
&lt;li&gt;&lt;a href="http://projectuguduwa.blogspot.com/" rel="nofollow"&gt;projectuguduwa.blogspot.com&lt;/a&gt;. For installation, configuration and other relevant information&lt;/li&gt;
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-2869654729535782923?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/6QRvK_aS-V3i4MYvHA8zlDlQgr4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/6QRvK_aS-V3i4MYvHA8zlDlQgr4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/6QRvK_aS-V3i4MYvHA8zlDlQgr4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/6QRvK_aS-V3i4MYvHA8zlDlQgr4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/5pSzRme1pEc" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2010/12/apachephp-web-proxy.html</link><author>noreply@blogger.com (righteous)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-5799179619415883321</guid><pubDate>Wed, 17 Nov 2010 01:53:00 +0000</pubDate><atom:updated>2012-03-08T22:11:20.772-08:00</atom:updated><title>Apache/Php IP:Port Proxy Emulator</title><description>&lt;b&gt;Aim&lt;/b&gt;&lt;br /&gt;
The aim of this exercise is to create an IP:Port proxy emulator in Php that can be deployed to a web server such as Apache. Once deployed it will run almost exactly like an IP:Port proxy -also referred to&amp;nbsp; as Elite or Open proxies- requring you to change your browser settings. Because it's written as a Php script, it will run inside apace. There is no database requirement.&lt;br /&gt;
&lt;br /&gt;
As this is a fairly substantial project, it is hosted on google at &lt;a href="http://code.google.com/p/mugatiya/" rel="nofollow"&gt;Project Mugatiya&lt;/a&gt;. There is also more information at the the 'Project Mugatiya blog &lt;a href="http://www.tidytutorials.com/p/mugatiya.html" rel="nofollow"&gt;Project Mugatiya on TidyTutorials&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;The advantages&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Almost exactly like a proxy such as squid or mod_proxy. &lt;/li&gt;
&lt;li&gt;Works with flash, streaming, torrents, pretty much everything. &lt;/li&gt;
&lt;li&gt;Modify content to display adds&lt;/li&gt;
&lt;li&gt;Has inbuilt Turing Test (reCaptcha) to keep away bots&lt;/li&gt;
&lt;li&gt;Has a content filter&lt;/li&gt;
&lt;li&gt;Remote Ip, requested url and requested hostname based black and white lists&lt;/li&gt;
&lt;li&gt;Written in simple, easy to understand Php.&lt;/li&gt;
&lt;/ol&gt;
Resources&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt; &lt;a href="http://code.google.com/p/mugatiya/" rel="nofollow"&gt;Project Mugatiya&lt;/a&gt;. - Project hosted on google's code hosting &lt;/li&gt;
&lt;li&gt;&lt;a href="http://code.google.com/p/mugatiya/downloads/list" rel="nofollow"&gt;Download Source Now&lt;/a&gt; - Download source code and zip file&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.tidytutorials.com/p/mugatiya.html" rel="nofollow"&gt;projectmugatiya&lt;/a&gt;. For installation, configuration and other relevant information&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
You should note that the proxy does rewrite HTTPS requests as plain HTTP requests and this sometimes doesn't work with complex sites. Please feel free to contribute to the project.&lt;br /&gt;
&lt;br /&gt;
Also, remember that as this is an IP:Port proxy, if left unchecked could land you with exorbitant overages on bandwidth. This demolishes web proxies hands down.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-5799179619415883321?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/c43z4wfEtI1zXfRY-8PXP6OmmwM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/c43z4wfEtI1zXfRY-8PXP6OmmwM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/c43z4wfEtI1zXfRY-8PXP6OmmwM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/c43z4wfEtI1zXfRY-8PXP6OmmwM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/kISSBQB5cPA" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2010/11/apachephp-ipport-proxy-emulator.html</link><author>noreply@blogger.com (righteous)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-8106913915183039813</guid><pubDate>Tue, 26 Oct 2010 02:15:00 +0000</pubDate><atom:updated>2012-05-28T16:39:02.451-07:00</atom:updated><title>Using Tools such as AWK, GREP, NAWK, XARGs on the Command Line</title><description>&lt;h3&gt;




Extract the requests from an access_log that match the IPs from a list.&lt;/h3&gt;
We have your common log formatted access_log. We wish to get all the requests that match the IPs from a list of IPs in a text file called ip_list.txt.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;# awk '/.*/ {print "cat access_log | grep "$1" &amp;gt;&amp;gt; extract.txt "}' ip_list.txt | sh&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;



Find out the Files That Contain Certain Text&lt;/h3&gt;
&lt;br /&gt;
It is common to want all the names of the files that contain a certain text. For instance, say inside a script directory you want to find all the scripts that contain a certain database name or a piece of text.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&amp;nbsp;find . -name '*.sh' | xargs grep -il '_KingKongDB'&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;




Find out required libraries for C/C++ functions&lt;/h3&gt;
Here we want to use the function sem_init, but we are not too sure of what libraries to link with. We can use 'nm' and 'grep' on the libraries (/usr/lib on linux). &lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;# nm --print-file-name /usr/lib/* 2&amp;gt; /dev/null |grep sem_init &lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
This command will return results with strings containing '/usr/lib/libpthread.a'. From this you know that you have to link with the option -pthread.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;




Replace a value in multiple files and write the output files to a new directory&lt;/h3&gt;
In this example we have xml files such as Nokia-220s.xml, Nokia-2330.xml etc. We want to replace the value 'Spec-Handler' in these files with a new value called 'Nokia-WAP' and then write these files into a new directory called 'new' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;# ls -l Nokia* |awk  '{print "sed","'\''s/Spec-Handset/Nokia-WAP/g'\'' " $9 " &amp;gt;new/"$9}' |sh&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;




Replace the newline ('\n') character at the end of a line with something else (a comma in this case)&lt;/h3&gt;
&lt;i&gt;cat addresses.txt | tr '\n' ','&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Compare two files that are similar and list values in one file that do not appear in the other&lt;/h3&gt;
&lt;i&gt;cat file1 | awk '{print"echo " $2 " `grep -c " &amp;nbsp;$2" file2`"}' | sh | grep '0$'&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Misc&lt;/h3&gt;
&lt;i&gt;find . -type f | grep -v '\.svn' | xargs perl -pi -e 's/\r\n/\n/g'&amp;nbsp;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;cat access_log | grep 217.124.181.30 |sed -n 's/.*\("[a-zA-Z][^"]*"\)$/\1/pg' &lt;/i&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-8106913915183039813?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/zaw87xJGOI85_GrIJ0kxLWux7IY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zaw87xJGOI85_GrIJ0kxLWux7IY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/zaw87xJGOI85_GrIJ0kxLWux7IY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zaw87xJGOI85_GrIJ0kxLWux7IY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/HS7cyy2zAfY" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2010/10/using-tools-such-as-awk-grep-nawk-xargs.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-8528192520302582229</guid><pubDate>Mon, 11 Oct 2010 01:44:00 +0000</pubDate><atom:updated>2011-02-14T03:04:38.617-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">break web proxy</category><title>Break Out of Web Proxy Frames</title><description>&lt;b&gt;Aim&lt;/b&gt;&lt;br /&gt;
To break out of Web Proxy Frames or any frames for that matter all you have to do is add the javascript shown below to your web page and replace the &lt;i&gt;codediaries.com/break.html&lt;/i&gt; link with your own link. Web Proxies can sometimes be a nuisance to webmasters as these proxies sometimes stop adds and mangle content. With this code snippet, you can easily break out of 98% of Web Proxies, provided they have javascript turned on. Let's bust all those web proxies!&lt;br /&gt;
&lt;br /&gt;
&lt;table border="1" class="CD_Code_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre class="CD_Code_Words" id="xxpre"&gt;&lt;span class="CD_Code_Num" name="xxhid"&gt; 1. &lt;/span&gt;&amp;lt;script&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 2. &lt;/span&gt;if (top.location!=("http://codediaries.com/break.html")) {
&lt;span class="CD_Code_Num" name="xxhid"&gt; 3. &lt;/span&gt;            top.location = "http://codediaries.com/break.html";
&lt;span class="CD_Code_Num" name="xxhid"&gt; 4. &lt;/span&gt;}
&lt;span class="CD_Code_Num" name="xxhid"&gt; 5. &lt;/span&gt;&amp;lt;/script&amp;gt;&lt;/pre&gt;&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input onclick="ToggleLineNumbers(this.checked, this.parentNode)" type="checkbox" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
To see this in action visit the link &lt;a href="http://codediaries.com/break.html"&gt;http://codediaries.com/break.html&lt;/a&gt; using a web proxy.&lt;br /&gt;
&lt;br /&gt;
Unfortunately Web Proxies are a little smarter than your average Frame and they tend to rewrite and insert a lot of javascript code. You get around this by obfuscating the javascript code using any online obfuscater. Once done, the code will look like this.&lt;br /&gt;
&lt;br /&gt;
&lt;table border="1" class="CD_Code_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre class="CD_Code_Words" id="xxpre"&gt;&lt;span class="CD_Code_Num" name="xxhid"&gt; 1. &lt;/span&gt;&amp;lt;script&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 2. &lt;/span&gt;var _0x5135=["\x6C\x6F\x63\x61\x74\x69\x6F\x6E","\x68\x74\x74\x70\x3A\x2F\x2F\x63\x6F\x64\x65\x64\x69\x61\x72\x69\x65\x73\x2E\x63\x6F\x6D\x2F\x62\x72\x65\x61\x6B\x2E\x68\x74\x6D\x6C"];if(top[_0x5135[0]]!=(_0x5135[1])){top[_0x5135[0]]=_0x5135[1];} ;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 3. &lt;/span&gt;&amp;lt;/script&amp;gt;&lt;/pre&gt;&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input onclick="ToggleLineNumbers(this.checked, this.parentNode)" type="checkbox" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
Javascript obfuscators are readily available and most of them are online. Just search for "javascript obfuscator" in your favourite search engine.&lt;br /&gt;
&lt;br /&gt;
Once you learn this simple trick you can now make changes and experiment on breaking these Web Proxies and Frames. Use this as a starting point for your web proxy demolition.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-8528192520302582229?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/PQzbB7P9wor_TG28F5VdVBlSKjg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/PQzbB7P9wor_TG28F5VdVBlSKjg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/PQzbB7P9wor_TG28F5VdVBlSKjg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/PQzbB7P9wor_TG28F5VdVBlSKjg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/42mnVeeHraY" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2010/10/break-out-of-web-proxy-frames.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-2296023521897082722</guid><pubDate>Mon, 27 Sep 2010 03:04:00 +0000</pubDate><atom:updated>2010-09-26T20:13:44.746-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">securtiy</category><category domain="http://www.blogger.com/atom/ns#">policy</category><category domain="http://www.blogger.com/atom/ns#">tomcat</category><title>Tomcat Configuration - Home, Base and catalina.policy</title><description>Here is some generic information regarding Tomcat 5.5 configuration and giving read write execute permissions to a servlet. Java's security manager can be quite complex and usually leads to a lot of pain when configuring a servlet to do something that is above and beyond the standard "read", "write", "execute" such as delete. Debugging such Security Manager permission error can be fairly daunting.&lt;br /&gt;
&lt;br /&gt;
Tomcat configuration has two variables &lt;i&gt;catalina.home&lt;/i&gt; and &lt;i&gt;catalina.base&lt;/i&gt;. The purpose of this is to allow multiple instances of tomcat run from a single installation.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;catalina.home&lt;/i&gt; is the installation directory and contains the directories &lt;b&gt;bin, common, server&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;catalina.base&lt;/i&gt; is your instance directory and contains the instance directories such as &lt;b&gt;work, shared, conf, webapps&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Security&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
In your conf directory there is another directory called policy.d. Inside the policy.d directory there are a few files. All of these files are merged into one and written to the catalina.policy file in the conf directory. DO NOT edit the catalina.policy file as this will have no effect. You have to edit the files inside the policy.d directory.&lt;br /&gt;
&lt;br /&gt;
To give your servlet extra permissions edit the file 50users.policy and add your servlet&lt;br /&gt;
&lt;table class="CD_Code_Table" border="1"&gt;&lt;tr&gt;&lt;td&gt;&lt;pre id="xxpre" class="CD_Code_Words"&gt;&lt;span name="xxhid" class="CD_Code_Num"&gt; 1. &lt;/span&gt;grant codeBase "file:${catalina.base}/webapps/Android/WEB-INF/-" {
&lt;span name="xxhid" class="CD_Code_Num"&gt; 2. &lt;/span&gt; permission java.io.FilePermission "${catalina.base}/webapps/Android/-", "read,write,execute,delete";
&lt;span name="xxhid" class="CD_Code_Num"&gt; 3. &lt;/span&gt;};&lt;/pre&gt;&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input type="checkbox" onclick="ToggleLineNumbers(this.checked, this.parentNode)" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;br /&gt;
You must be careful here. If, for example you are using another package to modify some files such as the DeleteQuietly function in the io-commons.jar package, then you must give this jar file the permissions too, if it is in another directory. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Debugging Security&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
To debug you must start tomcat with the option &lt;i&gt;-Djava.security.manager -Djava.security.debug=all&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
If you are using the startup script in /etc/init.d/tomcat5.5 then you should edit the tomcat5.5 file and add the setting as shown below.&lt;br /&gt;
&lt;br /&gt;
&lt;table class="CD_Code_Table" border="1"&gt;&lt;tr&gt;&lt;td&gt;&lt;pre id="xxpre" class="CD_Code_Words"&gt;&lt;span name="xxhid" class="CD_Code_Num"&gt; 1. &lt;/span&gt;if [ "$TOMCAT5_SECURITY" = "yes" ]; then
&lt;span name="xxhid" class="CD_Code_Num"&gt; 2. &lt;/span&gt;        JAVA_OPTS="$JAVA_OPTS  -Djava.security.manager -Djava.security.debug=all  -Djava.security.policy=$CATALINA_BASE/conf/catalina.policy"
&lt;span name="xxhid" class="CD_Code_Num"&gt; 3. &lt;/span&gt;fi&lt;/pre&gt;&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input type="checkbox" onclick="ToggleLineNumbers(this.checked, this.parentNode)" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;br /&gt;
This will give you a huge amount of logging info, but you can easily search the output for something like '&lt;i&gt;access denied&lt;/i&gt;' to find out what is causing the problem&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-2296023521897082722?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/m5UODlVIK5v2YEM-SYASKjM6LaU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/m5UODlVIK5v2YEM-SYASKjM6LaU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/m5UODlVIK5v2YEM-SYASKjM6LaU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/m5UODlVIK5v2YEM-SYASKjM6LaU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/uHL0o6kmL9c" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2010/09/tomcat-configuration-home-base-and.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-3031387623008451079</guid><pubDate>Thu, 23 Sep 2010 02:07:00 +0000</pubDate><atom:updated>2010-09-22T19:09:58.574-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mount NTFS</category><category domain="http://www.blogger.com/atom/ns#">live CD</category><category domain="http://www.blogger.com/atom/ns#">ubuntu</category><title>Mount Windows NT partition From Ubuntu Live CD</title><description>What ususally happens is a virus attacks your PC and you cannot boot up in wondows. The easiest way to recover this is with a Unix/Linux live CD. In this example we will use an Ubuntu Live CD to boot up and mount your NTFS partitions so you can copy data from them to a USB Drive.&lt;br /&gt;
&lt;br /&gt;
The only problem is that the partitions mount with "Read Permissions" only so you need to access them as root. But the live Ubuntu CD doesn't really have a root password. But using sudo we can set the root password to give us the required permission.&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Boot using Ubuntu LiveCD &lt;/li&gt;
&lt;li&gt;In the Ubuntu desktop click on the applications menu and System Settings -&amp;gt; Advanced tab -&amp;gt; Disk -&amp;gt; Filesystems&lt;/li&gt;
&lt;li&gt;Click on "Administrator Mode" at the bottom.&lt;/li&gt;
&lt;li&gt;You should see the windows partitions. Double click the partition you want to mount.&lt;/li&gt;
&lt;li&gt;In the 'type' drop down list box select "NTFS - NT File System".&lt;/li&gt;
&lt;li&gt;Select Mount point. It this case we're going to browse to /home/ubuntu/Desktop and create a new folder WinD and select this folder.&lt;/li&gt;
&lt;li&gt;"Security &amp;amp; Safety - make sure you check the "Writeable" box.&lt;/li&gt;
&lt;li&gt;Select OK.&lt;/li&gt;
&lt;li&gt;Now Enable the partition by clicking the "Enable" button at the bottom.&lt;/li&gt;
&lt;li&gt;Now open a console and and go to the mount point /home/ubuntu/Desktop in this example.&lt;/li&gt;
&lt;li&gt;In the console, set the root pasword using "&lt;i&gt;sudo passwd root&lt;/i&gt;".&lt;/li&gt;
&lt;li&gt;Now su&lt;/li&gt;
&lt;li&gt;Voila, now you can access the mount (/home/ubuntu/desktop/WinD in this example) and you can set more liberal permissions so you can access them with a file browser. &lt;/li&gt;
&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-3031387623008451079?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/4Zlw9974DUSqlcwGJV9ZCYiykAY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4Zlw9974DUSqlcwGJV9ZCYiykAY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/4Zlw9974DUSqlcwGJV9ZCYiykAY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4Zlw9974DUSqlcwGJV9ZCYiykAY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/q9EmUAfHpjY" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2010/09/mount-windows-nt-partition-from-ubuntu.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-8072140697280770734</guid><pubDate>Fri, 10 Sep 2010 05:39:00 +0000</pubDate><atom:updated>2010-09-11T05:25:37.773-07:00</atom:updated><title>Sending Mail Via Google's SMTP Server Using PHP</title><description>&lt;span style="font-weight: bold;"&gt;Aim&lt;/span&gt;&lt;br /&gt;
We will create a simple php application that uses PHPMailer to connect to Google's smtp server to send out an email. You can use this for any email provider who allows SMTP access. This is useful from an e-commerce point of view, to send out emails that will not go straight into the recipients junk mail inbox. This method is preferred over sendmail which uses and SMTP relay. If you are hosting your application on a shared host, using PhpMailer will get around email limits set for sendmail by your hosting provider.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Assumptions&lt;/span&gt;&lt;br /&gt;
This article assumes that you have a compatible version of PHP installed and configured and you have downloaded and unzipped the PHPMailer&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Versions used in this example&lt;/span&gt;&lt;br /&gt;
&lt;table border="1" class="CD_System_Info"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Sofware/Component&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Image&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Windows XP SP2&lt;/td&gt;&lt;td&gt;N/A&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Php 5.2.12&lt;/td&gt;&lt;td&gt;N/A&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;PhpMailer 5.1&lt;/td&gt;&lt;td&gt;N/A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;a href="http://codediaries.blogspot.com/2009/03/mega-software-download-page.html"&gt;Links to these files can be found here.&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
For the purposes of this example PhpMailer has been unzipped in the diretory where the example script resides&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Application&lt;/span&gt;&lt;br /&gt;
As you can see we're connecting to google's SMTP server via the url &lt;i&gt;ssl://smtp.gmail.com:465&lt;/i&gt;. You have to login with a valid gmail username and password.&lt;br /&gt;
&lt;br /&gt;
&lt;table border="1" class="CD_Code_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre class="CD_Code_Words" id="xxpre"&gt;&lt;span class="CD_Code_Num" name="xxhid"&gt; 1. &lt;/span&gt;require("PHPMailer_v5.1/class.phpmailer.php");
&lt;span class="CD_Code_Num" name="xxhid"&gt; 2. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 3. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 4. &lt;/span&gt;$mailer = new PHPMailer();
&lt;span class="CD_Code_Num" name="xxhid"&gt; 5. &lt;/span&gt;$mailer-&amp;gt;IsSMTP();
&lt;span class="CD_Code_Num" name="xxhid"&gt; 6. &lt;/span&gt;$mailer-&amp;gt;Host = 'ssl://smtp.gmail.com:465';
&lt;span class="CD_Code_Num" name="xxhid"&gt; 7. &lt;/span&gt;$mailer-&amp;gt;SMTPAuth = TRUE;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 8. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 9. &lt;/span&gt;$mailer-&amp;gt;Username = 'harrypotter@gmail.com';
&lt;span class="CD_Code_Num" name="xxhid"&gt;10. &lt;/span&gt;$mailer-&amp;gt;Password = 'alhamora';
&lt;span class="CD_Code_Num" name="xxhid"&gt;11. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;12. &lt;/span&gt;$mailer-&amp;gt;From = 'harrpotter@gmail.com';
&lt;span class="CD_Code_Num" name="xxhid"&gt;13. &lt;/span&gt;$mailer-&amp;gt;FromName = 'Harry Potter';
&lt;span class="CD_Code_Num" name="xxhid"&gt;14. &lt;/span&gt;$mailer-&amp;gt;Body = "Hello there all\nHow is everyone today?";
&lt;span class="CD_Code_Num" name="xxhid"&gt;15. &lt;/span&gt;$mailer-&amp;gt;Subject = "Hello from Harry";
&lt;span class="CD_Code_Num" name="xxhid"&gt;16. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;17. &lt;/span&gt;$mailer-&amp;gt;AddAddress(hermionegranger@hotmail.com);
&lt;span class="CD_Code_Num" name="xxhid"&gt;18. &lt;/span&gt;$mailer-&amp;gt;AddAddress(ronweasely@yahoo.com);
&lt;span class="CD_Code_Num" name="xxhid"&gt;19. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;20. &lt;/span&gt;if(!$mailer-&amp;gt;Send()) {
&lt;span class="CD_Code_Num" name="xxhid"&gt;21. &lt;/span&gt;    error_log("Mailer :  error ".$mailer-&amp;gt;ErrorInfo)." : $to";
&lt;span class="CD_Code_Num" name="xxhid"&gt;22. &lt;/span&gt;    echo "fail";
&lt;span class="CD_Code_Num" name="xxhid"&gt;23. &lt;/span&gt;}
&lt;span class="CD_Code_Num" name="xxhid"&gt;24. &lt;/span&gt;else {
&lt;span class="CD_Code_Num" name="xxhid"&gt;25. &lt;/span&gt;    echo "sent";
&lt;span class="CD_Code_Num" name="xxhid"&gt;26. &lt;/span&gt;}&lt;/pre&gt;&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input onclick="ToggleLineNumbers(this.checked, this.parentNode)" type="checkbox" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-8072140697280770734?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/gSJqj2xg1N8mQtNVJrGedPX9FTg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gSJqj2xg1N8mQtNVJrGedPX9FTg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/gSJqj2xg1N8mQtNVJrGedPX9FTg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gSJqj2xg1N8mQtNVJrGedPX9FTg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/WNUlanc_vn8" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2010/09/sending-mail-via-googles-smtp-server.html</link><author>noreply@blogger.com (righteous)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-1695153882416018370</guid><pubDate>Mon, 06 Sep 2010 12:58:00 +0000</pubDate><atom:updated>2010-09-06T06:03:27.172-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">java</category><category domain="http://www.blogger.com/atom/ns#">daylight saving time</category><category domain="http://www.blogger.com/atom/ns#">tzupdater</category><title>Java Check Daylight Saving Time Utility</title><description>&lt;span style="font-weight: bold;"&gt;Aim&lt;/span&gt;&lt;br /&gt;
This simple example is to create a small java application that you can use to view timezones and see when the JDK/JRE changes to daylight savings time or changes back from daylight savings time. It's important to know when DST occurs, especially for systems that have time sensitive information.&lt;br /&gt;
&lt;br /&gt;
You can use the java/oracle tzupdater.jar utility to update your jre/jdk for all the latest timezones and daylight saving time configurations.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Application&lt;/span&gt;&lt;br /&gt;
This application enumerates the timezones available on a system. You can then select a timezone and set a date and then increment that date to see when DST occurs. This utility uses the 'inDaylightTime()' function to test if the time is in daylight savings time or not.&lt;br /&gt;
&lt;br /&gt;
&lt;table class="CD_Code_Table" border="1"&gt;&lt;tr&gt;&lt;td&gt;&lt;pre id="xxpre" class="CD_Code_Words"&gt;&lt;span name="xxhid" class="CD_Code_Num"&gt; 1. &lt;/span&gt;import java.util.*;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 2. &lt;/span&gt;import java.text.*;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 3. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 4. &lt;/span&gt;public class tzutility{
&lt;span name="xxhid" class="CD_Code_Num"&gt; 5. &lt;/span&gt;  public static void main(String[] args){
&lt;span name="xxhid" class="CD_Code_Num"&gt; 6. &lt;/span&gt;    Date date = new Date();
&lt;span name="xxhid" class="CD_Code_Num"&gt; 7. &lt;/span&gt;    String TimeZoneIds[] = TimeZone.getAvailableIDs();
&lt;span name="xxhid" class="CD_Code_Num"&gt; 8. &lt;/span&gt;    for(int i = 0; i &amp;lt; TimeZoneIds.length; i++){
&lt;span name="xxhid" class="CD_Code_Num"&gt; 9. &lt;/span&gt;      TimeZone tz = TimeZone.getTimeZone(TimeZoneIds[i]);
&lt;span name="xxhid" class="CD_Code_Num"&gt;10. &lt;/span&gt;      String tzName = tz.getDisplayName(tz.inDaylightTime(date), TimeZone.LONG);
&lt;span name="xxhid" class="CD_Code_Num"&gt;11. &lt;/span&gt;      System.out.println(TimeZoneIds[i] + ":   ");
&lt;span name="xxhid" class="CD_Code_Num"&gt;12. &lt;/span&gt;    }
&lt;span name="xxhid" class="CD_Code_Num"&gt;13. &lt;/span&gt;    try{
&lt;span name="xxhid" class="CD_Code_Num"&gt;14. &lt;/span&gt;      DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
&lt;span name="xxhid" class="CD_Code_Num"&gt;15. &lt;/span&gt;      TimeZone nswtz = TimeZone.getTimeZone("Australia/Queensland");
&lt;span name="xxhid" class="CD_Code_Num"&gt;16. &lt;/span&gt;      Date d = df.parse("2010-09-01");
&lt;span name="xxhid" class="CD_Code_Num"&gt;17. &lt;/span&gt;      Calendar cal = Calendar.getInstance();
&lt;span name="xxhid" class="CD_Code_Num"&gt;18. &lt;/span&gt;      cal.setTime(d);
&lt;span name="xxhid" class="CD_Code_Num"&gt;19. &lt;/span&gt;      for(int i = 0 ; i &amp;lt; 100 ; i++ ){
&lt;span name="xxhid" class="CD_Code_Num"&gt;20. &lt;/span&gt;        System.out.println(cal.getTime()+" "+nswtz.inDaylightTime(cal.getTime()));
&lt;span name="xxhid" class="CD_Code_Num"&gt;21. &lt;/span&gt;        cal.add(Calendar.DATE, 1);
&lt;span name="xxhid" class="CD_Code_Num"&gt;22. &lt;/span&gt;      }
&lt;span name="xxhid" class="CD_Code_Num"&gt;23. &lt;/span&gt;    }catch(Exception e){
&lt;span name="xxhid" class="CD_Code_Num"&gt;24. &lt;/span&gt;      e.printStackTrace();
&lt;span name="xxhid" class="CD_Code_Num"&gt;25. &lt;/span&gt;    }
&lt;span name="xxhid" class="CD_Code_Num"&gt;26. &lt;/span&gt;  }
&lt;span name="xxhid" class="CD_Code_Num"&gt;27. &lt;/span&gt;}&lt;/pre&gt;&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input type="checkbox" onclick="ToggleLineNumbers(this.checked, this.parentNode)" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-1695153882416018370?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/XPpBNBa2gyjUWllCvfI5trRmBl4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XPpBNBa2gyjUWllCvfI5trRmBl4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/XPpBNBa2gyjUWllCvfI5trRmBl4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XPpBNBa2gyjUWllCvfI5trRmBl4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/KnIvxNW6pW0" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2010/09/java-check-daylight-saving-time-utility.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-6254760981887932228</guid><pubDate>Mon, 07 Jun 2010 09:11:00 +0000</pubDate><atom:updated>2010-06-10T04:29:52.424-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">array</category><category domain="http://www.blogger.com/atom/ns#">multidimentional</category><category domain="http://www.blogger.com/atom/ns#">C++</category><title>C++ Allocating Dynamic Multidimentional Arrays</title><description>Aim&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
To demonstrate dynamic array allocation in C++. Dynamic array allocation in C++ can take many forms. Regardless of whether you use malloc or new, you have to be carefull when freeing up memory using free or delete. In these examples we will allocate three dimentional 3D arrays as it give a better insight than two dimentional 2D arrays. The examples are given below.&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;int***p&lt;/em&gt; Dynamic using malloc&lt;br /&gt;
&lt;em&gt;int***p&lt;/em&gt; Dynamic using new&lt;br /&gt;
&lt;em&gt;int (*p)[10][10]&lt;/em&gt; Sem-Dynamic using new.&lt;br /&gt;
&lt;br /&gt;
Now let us look at these in more detail below.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Dynamic Malloc&lt;/strong&gt; &lt;br /&gt;
The first way to allocate an array is using malloc and a pointer to a pointer to a pointer. You have to free all the arrays at the end Malloc doesn't call constuctors and free doesn't call destructors.&lt;br /&gt;
&lt;table border="1" class="CD_Code_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre class="CD_Code_Words" id="xxpre"&gt;&lt;span class="CD_Code_Num" name="xxhid"&gt; 1. &lt;/span&gt;#include &amp;lt;stdlib.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 2. &lt;/span&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 3. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 4. &lt;/span&gt;int main(){
&lt;span class="CD_Code_Num" name="xxhid"&gt; 5. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 6. &lt;/span&gt;    int*** p;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 7. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 8. &lt;/span&gt;    int x    = 10;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 9. &lt;/span&gt;    int y    = 10;
&lt;span class="CD_Code_Num" name="xxhid"&gt;10. &lt;/span&gt;    int z     = 10;
&lt;span class="CD_Code_Num" name="xxhid"&gt;11. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;12. &lt;/span&gt;    int i, j;
&lt;span class="CD_Code_Num" name="xxhid"&gt;13. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;14. &lt;/span&gt;    p = (int***)malloc(x*sizeof(int**));
&lt;span class="CD_Code_Num" name="xxhid"&gt;15. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;16. &lt;/span&gt;    for(i=0; i &amp;lt; x; i++){
&lt;span class="CD_Code_Num" name="xxhid"&gt;17. &lt;/span&gt;        p[i] = (int**)malloc(y*sizeof(int*));
&lt;span class="CD_Code_Num" name="xxhid"&gt;18. &lt;/span&gt;        for(j=0; j &amp;lt; y; j++){
&lt;span class="CD_Code_Num" name="xxhid"&gt;19. &lt;/span&gt;            p[i][j] = (int*)malloc(z*sizeof(int));
&lt;span class="CD_Code_Num" name="xxhid"&gt;20. &lt;/span&gt;        }
&lt;span class="CD_Code_Num" name="xxhid"&gt;21. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;22. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;23. &lt;/span&gt;    p[0][2][3]=100;
&lt;span class="CD_Code_Num" name="xxhid"&gt;24. &lt;/span&gt;    p[4][5][4]=101;
&lt;span class="CD_Code_Num" name="xxhid"&gt;25. &lt;/span&gt;    printf("values %d\n", p[0][2][3]);
&lt;span class="CD_Code_Num" name="xxhid"&gt;26. &lt;/span&gt;    
&lt;span class="CD_Code_Num" name="xxhid"&gt;27. &lt;/span&gt;    for(i=0; i &amp;lt; x ; i++){
&lt;span class="CD_Code_Num" name="xxhid"&gt;28. &lt;/span&gt;        for(i=0; i &amp;lt; y ; i++){
&lt;span class="CD_Code_Num" name="xxhid"&gt;29. &lt;/span&gt;            free(p[i][j]);
&lt;span class="CD_Code_Num" name="xxhid"&gt;30. &lt;/span&gt;        }
&lt;span class="CD_Code_Num" name="xxhid"&gt;31. &lt;/span&gt;        free (p[i]);
&lt;span class="CD_Code_Num" name="xxhid"&gt;32. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;33. &lt;/span&gt;    free (p);
&lt;span class="CD_Code_Num" name="xxhid"&gt;34. &lt;/span&gt;}&lt;/pre&gt;&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input onclick="ToggleLineNumbers(this.checked, this.parentNode)" type="checkbox" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
&lt;strong&gt;Dynamic New&lt;/strong&gt;&lt;br /&gt;
Now let us look at dynamically allocating an array using the new operator. You have to delete all the arrays after you have finished.&lt;br /&gt;
&lt;table border="1" class="CD_Code_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre class="CD_Code_Words" id="xxpre"&gt;&lt;span class="CD_Code_Num" name="xxhid"&gt; 1. &lt;/span&gt;#include &amp;lt;stdlib.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 2. &lt;/span&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 3. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 4. &lt;/span&gt;int main(){
&lt;span class="CD_Code_Num" name="xxhid"&gt; 5. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 6. &lt;/span&gt;    int x = 10;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 7. &lt;/span&gt;    int y = 10;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 8. &lt;/span&gt;    int z = 10;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 9. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;10. &lt;/span&gt;    int i, j;
&lt;span class="CD_Code_Num" name="xxhid"&gt;11. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;12. &lt;/span&gt;    int ***p; 
&lt;span class="CD_Code_Num" name="xxhid"&gt;13. &lt;/span&gt;    
&lt;span class="CD_Code_Num" name="xxhid"&gt;14. &lt;/span&gt;    p =  new int**[x];
&lt;span class="CD_Code_Num" name="xxhid"&gt;15. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;16. &lt;/span&gt;    for(i=0; i&amp;lt;x; i++){
&lt;span class="CD_Code_Num" name="xxhid"&gt;17. &lt;/span&gt;        p[i] = new int*[y];
&lt;span class="CD_Code_Num" name="xxhid"&gt;18. &lt;/span&gt;        for(j=0; j&amp;lt;x; j++){
&lt;span class="CD_Code_Num" name="xxhid"&gt;19. &lt;/span&gt;            p[i][j] = new int[z];
&lt;span class="CD_Code_Num" name="xxhid"&gt;20. &lt;/span&gt;        }
&lt;span class="CD_Code_Num" name="xxhid"&gt;21. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;22. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;23. &lt;/span&gt;    p[1][2][3]=100;
&lt;span class="CD_Code_Num" name="xxhid"&gt;24. &lt;/span&gt;    p[4][5][6]=100;
&lt;span class="CD_Code_Num" name="xxhid"&gt;25. &lt;/span&gt;    printf("values %d\n", p[1][2][3]);
&lt;span class="CD_Code_Num" name="xxhid"&gt;26. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;27. &lt;/span&gt;    for(i=0; i&amp;lt;x; i++){
&lt;span class="CD_Code_Num" name="xxhid"&gt;28. &lt;/span&gt;        for(j=0; j&amp;lt;x; j++){
&lt;span class="CD_Code_Num" name="xxhid"&gt;29. &lt;/span&gt;            delete [] p[i][j];
&lt;span class="CD_Code_Num" name="xxhid"&gt;30. &lt;/span&gt;        }
&lt;span class="CD_Code_Num" name="xxhid"&gt;31. &lt;/span&gt;        delete  [] p[i] ;
&lt;span class="CD_Code_Num" name="xxhid"&gt;32. &lt;/span&gt;    }
&lt;span class="CD_Code_Num" name="xxhid"&gt;33. &lt;/span&gt;    delete [] p;
&lt;span class="CD_Code_Num" name="xxhid"&gt;34. &lt;/span&gt;}&lt;/pre&gt;&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input onclick="ToggleLineNumbers(this.checked, this.parentNode)" type="checkbox" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
&lt;strong&gt;Semi-Dynamic with 'new'&lt;/strong&gt;&lt;br /&gt;
Next let us have a look at semi-dynamically allocating an array using 'new'. It's semi dynamic because you can vary only one array length and the others are fixed. &lt;i&gt;p&lt;/i&gt; is basicall a pointer to a multidimentional array of int.&lt;br /&gt;
&lt;table border="1" class="CD_Code_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre class="CD_Code_Words" id="xxpre"&gt;&lt;span class="CD_Code_Num" name="xxhid"&gt; 1. &lt;/span&gt;#include &amp;lt;stdlib.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 2. &lt;/span&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 3. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 4. &lt;/span&gt;int main(){
&lt;span class="CD_Code_Num" name="xxhid"&gt; 5. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 6. &lt;/span&gt;    int x = 10;
&lt;span class="CD_Code_Num" name="xxhid"&gt; 7. &lt;/span&gt;    const int y(10);
&lt;span class="CD_Code_Num" name="xxhid"&gt; 8. &lt;/span&gt;    const int z(10);
&lt;span class="CD_Code_Num" name="xxhid"&gt; 9. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;10. &lt;/span&gt;    int i, j;
&lt;span class="CD_Code_Num" name="xxhid"&gt;11. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;12. &lt;/span&gt;    int (*p)[y][z] = new int[x][y][z];
&lt;span class="CD_Code_Num" name="xxhid"&gt;13. &lt;/span&gt;    
&lt;span class="CD_Code_Num" name="xxhid"&gt;14. &lt;/span&gt;    p[0][2][3]=100;
&lt;span class="CD_Code_Num" name="xxhid"&gt;15. &lt;/span&gt;    p[4][5][4]=101;
&lt;span class="CD_Code_Num" name="xxhid"&gt;16. &lt;/span&gt;    printf("values %d\n", p[0][2][3]);
&lt;span class="CD_Code_Num" name="xxhid"&gt;17. &lt;/span&gt;
&lt;span class="CD_Code_Num" name="xxhid"&gt;18. &lt;/span&gt;    delete [] p    ;
&lt;span class="CD_Code_Num" name="xxhid"&gt;19. &lt;/span&gt;}&lt;/pre&gt;&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input onclick="ToggleLineNumbers(this.checked, this.parentNode)" type="checkbox" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-6254760981887932228?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/SNSgskQQUf-f9YiIhcAfY6jcESo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SNSgskQQUf-f9YiIhcAfY6jcESo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/SNSgskQQUf-f9YiIhcAfY6jcESo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SNSgskQQUf-f9YiIhcAfY6jcESo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/pa3o6CGdMtE" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2010/06/c-allocating-dynamic-multidimentional.html</link><author>noreply@blogger.com (righteous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4928424574574982882.post-7493421762192327841</guid><pubDate>Thu, 03 Jun 2010 11:56:00 +0000</pubDate><atom:updated>2010-11-02T01:25:13.430-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">linux</category><category domain="http://www.blogger.com/atom/ns#">socket</category><category domain="http://www.blogger.com/atom/ns#">fork</category><category domain="http://www.blogger.com/atom/ns#">C++</category><title>Linux C++ Forking Server and Client Socket Example</title><description>&lt;span style="font-weight: bold;"&gt;Aim&lt;/span&gt;&lt;br /&gt;
The goal of this example is to create simple Linux C++ server that accepts socket connections and forks-off to handle the connection. This is the simplest Linux C++ forking server socket and client example you can find. This covers all the steps required to open a C++ clinet/server socket and forking a process. Please leave any comments or questions at the end of this tutorial and I will endeavour to answer them. This tutorial shares the client code with &lt;a href="http://codediaries.blogspot.com/2010/06/linux-c-socket-example-with-client.html"&gt;Linux C++ Socket Example with Client Server and Mulit-Threading &lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Assumptions&lt;/span&gt;&lt;br /&gt;
This article assumes that you have a Linux based C++ compiler installed and configured. You can use cygwin if you do not have a Linux environment.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Versions used in this example&lt;/span&gt;&lt;br /&gt;
&lt;table border="1" class="CD_System_Info"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Sofware/Component&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Image&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Fedora Core 5&lt;/td&gt;&lt;td&gt;N/A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;a href="http://codediaries.blogspot.com/2009/03/mega-software-download-page.html"&gt;Links to these files can be found here&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://codediaries.com/code/examples/LinSocketFork.zip"&gt;&lt;img src="http://codediaries.com/code/ico_download.gif" style="height: 40px; vertical-align: middle;" /&gt;You can download the zipped example here.&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Steps required for a server socket&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Initialize the socket using socket().&lt;/li&gt;
&lt;li&gt;Set any options such as blocking etc using setsockopt().&lt;/li&gt;
&lt;li&gt;Bind to the local address and port using bind(). For a server use INADDR_ANY as the address.&lt;/li&gt;
&lt;li&gt;listen() for connections.&lt;/li&gt;
&lt;li&gt;Go into a loop and accept connections usin accept().&lt;/li&gt;
&lt;li&gt;fork! handle the connection in the child process and go back to listening for another socket in the parent process&lt;/li&gt;
Read and write to the socket. within the thread. &lt;/li&gt;
&lt;/li&gt;
&lt;li&gt;close()&lt;/li&gt;
&lt;li&gt;When you are done, close the server socket using close()&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;b&gt;Steps required for a client socket&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;Please see the &lt;a href="http://codediaries.blogspot.com/2010/06/linux-c-socket-example-with-client.html"&gt;Linux C++ Socket Example with Client Server and Mulit-Threading &lt;/a&gt;tutorial for the client. &lt;/ol&gt;&lt;br /&gt;
&lt;b&gt;Write and compile the Server&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;The server listens on port 1101 for a connection. When it receives a connection it creates a thread via pthread_create to handle it. This thread then reads from the socket, appends " SERVER ECHO" to it and sends it back to the client. As the server doesn't require an IP address, we assign INADDR_ANY to the sockaddr_in struct. Save this code as LinSever.cpp. &lt;br /&gt;
&lt;table class="CD_Code_Table" border="1"&gt;&lt;tr&gt;&lt;td&gt;&lt;pre id="xxpre" class="CD_Code_Words"&gt;&lt;span name="xxhid" class="CD_Code_Num"&gt; 1. &lt;/span&gt;#include &amp;lt;fcntl.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 2. &lt;/span&gt;#include &amp;lt;string.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 3. &lt;/span&gt;#include &amp;lt;stdlib.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 4. &lt;/span&gt;#include &amp;lt;errno.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 5. &lt;/span&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 6. &lt;/span&gt;#include &amp;lt;netinet/in.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 7. &lt;/span&gt;#include &amp;lt;resolv.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 8. &lt;/span&gt;#include &amp;lt;sys/socket.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt; 9. &lt;/span&gt;#include &amp;lt;arpa/inet.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;10. &lt;/span&gt;#include &amp;lt;unistd.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;11. &lt;/span&gt;#include &amp;lt;pthread.h&amp;gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;12. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;13. &lt;/span&gt;void* SocketHandler(void*);
&lt;span name="xxhid" class="CD_Code_Num"&gt;14. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;15. &lt;/span&gt;int main(int argv, char** argc){
&lt;span name="xxhid" class="CD_Code_Num"&gt;16. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;17. &lt;/span&gt;    int host_port= 1101;
&lt;span name="xxhid" class="CD_Code_Num"&gt;18. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;19. &lt;/span&gt;    struct sockaddr_in my_addr;
&lt;span name="xxhid" class="CD_Code_Num"&gt;20. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;21. &lt;/span&gt;    int hsock;
&lt;span name="xxhid" class="CD_Code_Num"&gt;22. &lt;/span&gt;    int * p_int ;
&lt;span name="xxhid" class="CD_Code_Num"&gt;23. &lt;/span&gt;    int err;
&lt;span name="xxhid" class="CD_Code_Num"&gt;24. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;25. &lt;/span&gt;    socklen_t addr_size = 0;
&lt;span name="xxhid" class="CD_Code_Num"&gt;26. &lt;/span&gt;    int* csock;
&lt;span name="xxhid" class="CD_Code_Num"&gt;27. &lt;/span&gt;    sockaddr_in sadr;
&lt;span name="xxhid" class="CD_Code_Num"&gt;28. &lt;/span&gt;    pthread_t thread_id=0;
&lt;span name="xxhid" class="CD_Code_Num"&gt;29. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;30. &lt;/span&gt;    int childpid;
&lt;span name="xxhid" class="CD_Code_Num"&gt;31. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;32. &lt;/span&gt;    signal(SIGCHLD, SIG_IGN);
&lt;span name="xxhid" class="CD_Code_Num"&gt;33. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;34. &lt;/span&gt;    hsock = socket(AF_INET, SOCK_STREAM, 0);
&lt;span name="xxhid" class="CD_Code_Num"&gt;35. &lt;/span&gt;    if(hsock == -1){
&lt;span name="xxhid" class="CD_Code_Num"&gt;36. &lt;/span&gt;        printf("Error initializing socket %d\n", errno);
&lt;span name="xxhid" class="CD_Code_Num"&gt;37. &lt;/span&gt;        goto FINISH;
&lt;span name="xxhid" class="CD_Code_Num"&gt;38. &lt;/span&gt;    }
&lt;span name="xxhid" class="CD_Code_Num"&gt;39. &lt;/span&gt;    
&lt;span name="xxhid" class="CD_Code_Num"&gt;40. &lt;/span&gt;    p_int = (int*)malloc(sizeof(int));
&lt;span name="xxhid" class="CD_Code_Num"&gt;41. &lt;/span&gt;    *p_int = 1;
&lt;span name="xxhid" class="CD_Code_Num"&gt;42. &lt;/span&gt;        
&lt;span name="xxhid" class="CD_Code_Num"&gt;43. &lt;/span&gt;    if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
&lt;span name="xxhid" class="CD_Code_Num"&gt;44. &lt;/span&gt;        (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
&lt;span name="xxhid" class="CD_Code_Num"&gt;45. &lt;/span&gt;        printf("Error setting options %d\n", errno);
&lt;span name="xxhid" class="CD_Code_Num"&gt;46. &lt;/span&gt;        free(p_int);
&lt;span name="xxhid" class="CD_Code_Num"&gt;47. &lt;/span&gt;        goto FINISH;
&lt;span name="xxhid" class="CD_Code_Num"&gt;48. &lt;/span&gt;    }
&lt;span name="xxhid" class="CD_Code_Num"&gt;49. &lt;/span&gt;    free(p_int);
&lt;span name="xxhid" class="CD_Code_Num"&gt;50. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;51. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;52. &lt;/span&gt;    my_addr.sin_family = AF_INET ;
&lt;span name="xxhid" class="CD_Code_Num"&gt;53. &lt;/span&gt;    my_addr.sin_port = htons(host_port);
&lt;span name="xxhid" class="CD_Code_Num"&gt;54. &lt;/span&gt;    
&lt;span name="xxhid" class="CD_Code_Num"&gt;55. &lt;/span&gt;    memset(&amp;(my_addr.sin_zero), 0, 8);
&lt;span name="xxhid" class="CD_Code_Num"&gt;56. &lt;/span&gt;    my_addr.sin_addr.s_addr = INADDR_ANY ;
&lt;span name="xxhid" class="CD_Code_Num"&gt;57. &lt;/span&gt;    
&lt;span name="xxhid" class="CD_Code_Num"&gt;58. &lt;/span&gt;    if( bind( hsock, (sockaddr*)&amp;my_addr, sizeof(my_addr)) == -1 ){
&lt;span name="xxhid" class="CD_Code_Num"&gt;59. &lt;/span&gt;        fprintf(stderr,"Error binding to socket, make sure nothing else is listening on this port %d\n",errno);
&lt;span name="xxhid" class="CD_Code_Num"&gt;60. &lt;/span&gt;        goto FINISH;
&lt;span name="xxhid" class="CD_Code_Num"&gt;61. &lt;/span&gt;    }
&lt;span name="xxhid" class="CD_Code_Num"&gt;62. &lt;/span&gt;    if(listen( hsock, 10) == -1 ){
&lt;span name="xxhid" class="CD_Code_Num"&gt;63. &lt;/span&gt;        fprintf(stderr, "Error listening %d\n",errno);
&lt;span name="xxhid" class="CD_Code_Num"&gt;64. &lt;/span&gt;        goto FINISH;
&lt;span name="xxhid" class="CD_Code_Num"&gt;65. &lt;/span&gt;    }
&lt;span name="xxhid" class="CD_Code_Num"&gt;66. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;67. &lt;/span&gt;    //Now lets do the server stuff
&lt;span name="xxhid" class="CD_Code_Num"&gt;68. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;69. &lt;/span&gt;    addr_size = sizeof(sockaddr_in);
&lt;span name="xxhid" class="CD_Code_Num"&gt;70. &lt;/span&gt;    
&lt;span name="xxhid" class="CD_Code_Num"&gt;71. &lt;/span&gt;    while(true){
&lt;span name="xxhid" class="CD_Code_Num"&gt;72. &lt;/span&gt;        printf("waiting for a connection\n");
&lt;span name="xxhid" class="CD_Code_Num"&gt;73. &lt;/span&gt;        csock = (int*)malloc(sizeof(int));
&lt;span name="xxhid" class="CD_Code_Num"&gt;74. &lt;/span&gt;        if((*csock = accept( hsock, (sockaddr*)&amp;sadr, &amp;addr_size))!= -1){
&lt;span name="xxhid" class="CD_Code_Num"&gt;75. &lt;/span&gt;            printf("---------------------\nReceived connection from %s\n",inet_ntoa(sadr.sin_addr));
&lt;span name="xxhid" class="CD_Code_Num"&gt;76. &lt;/span&gt;            switch( childpid=fork()){
&lt;span name="xxhid" class="CD_Code_Num"&gt;77. &lt;/span&gt;                case -1://error
&lt;span name="xxhid" class="CD_Code_Num"&gt;78. &lt;/span&gt;                    fprintf(stderr, "Error spawning the child %d\n",errno);
&lt;span name="xxhid" class="CD_Code_Num"&gt;79. &lt;/span&gt;                    exit(0);
&lt;span name="xxhid" class="CD_Code_Num"&gt;80. &lt;/span&gt;                    break;
&lt;span name="xxhid" class="CD_Code_Num"&gt;81. &lt;/span&gt;                case 0://in the child
&lt;span name="xxhid" class="CD_Code_Num"&gt;82. &lt;/span&gt;                    SocketHandler(csock);
&lt;span name="xxhid" class="CD_Code_Num"&gt;83. &lt;/span&gt;                    exit(0);
&lt;span name="xxhid" class="CD_Code_Num"&gt;84. &lt;/span&gt;                default://in the server
&lt;span name="xxhid" class="CD_Code_Num"&gt;85. &lt;/span&gt;                    close(*csock);
&lt;span name="xxhid" class="CD_Code_Num"&gt;86. &lt;/span&gt;                    free(csock);
&lt;span name="xxhid" class="CD_Code_Num"&gt;87. &lt;/span&gt;                    break;
&lt;span name="xxhid" class="CD_Code_Num"&gt;88. &lt;/span&gt;            }
&lt;span name="xxhid" class="CD_Code_Num"&gt;89. &lt;/span&gt;        }
&lt;span name="xxhid" class="CD_Code_Num"&gt;90. &lt;/span&gt;        else{
&lt;span name="xxhid" class="CD_Code_Num"&gt;91. &lt;/span&gt;            fprintf(stderr, "Error accepting %d\n", errno);
&lt;span name="xxhid" class="CD_Code_Num"&gt;92. &lt;/span&gt;        }
&lt;span name="xxhid" class="CD_Code_Num"&gt;93. &lt;/span&gt;    }
&lt;span name="xxhid" class="CD_Code_Num"&gt;94. &lt;/span&gt;    
&lt;span name="xxhid" class="CD_Code_Num"&gt;95. &lt;/span&gt;FINISH:
&lt;span name="xxhid" class="CD_Code_Num"&gt;96. &lt;/span&gt;;
&lt;span name="xxhid" class="CD_Code_Num"&gt;97. &lt;/span&gt;}
&lt;span name="xxhid" class="CD_Code_Num"&gt;98. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;99. &lt;/span&gt;void* SocketHandler(void* lp){
&lt;span name="xxhid" class="CD_Code_Num"&gt;100. &lt;/span&gt;    int *csock = (int*)lp;
&lt;span name="xxhid" class="CD_Code_Num"&gt;101. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;102. &lt;/span&gt;    char buffer[1024];
&lt;span name="xxhid" class="CD_Code_Num"&gt;103. &lt;/span&gt;    int buffer_len = 1024;
&lt;span name="xxhid" class="CD_Code_Num"&gt;104. &lt;/span&gt;    int bytecount;
&lt;span name="xxhid" class="CD_Code_Num"&gt;105. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;106. &lt;/span&gt;    memset(buffer, 0, buffer_len);
&lt;span name="xxhid" class="CD_Code_Num"&gt;107. &lt;/span&gt;    if((bytecount = recv(*csock, buffer, buffer_len, 0))== -1){
&lt;span name="xxhid" class="CD_Code_Num"&gt;108. &lt;/span&gt;        fprintf(stderr, "Error receiving data %d\n", errno);
&lt;span name="xxhid" class="CD_Code_Num"&gt;109. &lt;/span&gt;        goto FINISH;
&lt;span name="xxhid" class="CD_Code_Num"&gt;110. &lt;/span&gt;    }
&lt;span name="xxhid" class="CD_Code_Num"&gt;111. &lt;/span&gt;    printf("Received bytes %d\nReceived string \"%s\"\n", bytecount, buffer);
&lt;span name="xxhid" class="CD_Code_Num"&gt;112. &lt;/span&gt;    strcat(buffer, " SERVER ECHO");
&lt;span name="xxhid" class="CD_Code_Num"&gt;113. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;114. &lt;/span&gt;    if((bytecount = send(*csock, buffer, strlen(buffer), 0))== -1){
&lt;span name="xxhid" class="CD_Code_Num"&gt;115. &lt;/span&gt;        fprintf(stderr, "Error sending data %d\n", errno);
&lt;span name="xxhid" class="CD_Code_Num"&gt;116. &lt;/span&gt;        goto FINISH;
&lt;span name="xxhid" class="CD_Code_Num"&gt;117. &lt;/span&gt;    }
&lt;span name="xxhid" class="CD_Code_Num"&gt;118. &lt;/span&gt;    
&lt;span name="xxhid" class="CD_Code_Num"&gt;119. &lt;/span&gt;    printf("Sent bytes %d\n", bytecount);
&lt;span name="xxhid" class="CD_Code_Num"&gt;120. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;121. &lt;/span&gt;
&lt;span name="xxhid" class="CD_Code_Num"&gt;122. &lt;/span&gt;FINISH:
&lt;span name="xxhid" class="CD_Code_Num"&gt;123. &lt;/span&gt;    free(csock);
&lt;span name="xxhid" class="CD_Code_Num"&gt;124. &lt;/span&gt;    return 0;
&lt;span name="xxhid" class="CD_Code_Num"&gt;125. &lt;/span&gt;}&lt;/pre&gt;&lt;span class="CD_Code_Table_Footer"&gt;Hide line numbers &lt;/span&gt;&lt;input type="checkbox" onclick="ToggleLineNumbers(this.checked, this.parentNode)" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;br /&gt;
&lt;strong&gt;Line 32:&lt;/strong&gt; When a child process exits without a call to 'wait()' from the parent, it turns into a 'zombie.' You can prevent this by setting signal(SIGCHLD, SIG_IGN);&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Line 76:&lt;/strong&gt; Fork returns -1 in the parent on failure. It return the child pid in the parent and 0 in the child.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Line 85:&lt;/strong&gt; When you fork, you must close the client connection in the parent(server) process, otherwise you will hit the "open socket descriptor limit" in Linux.&lt;br /&gt;
&lt;/li&gt;
`
&lt;li&gt;Open a prompt to the working directory and compile the code using your C++ compiler &lt;br /&gt;
&lt;table class="CD_Console_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;br /&gt;
..workspace\SocketExample&amp;gt;g++ -o server LinServer.cpp -lpthread &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;b&gt;Write and compile the Client&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;The client reads a line from the console and sends this to the server. It then reads from the server and displays it on the console. You can view the client in this tutorial &lt;a href="http://codediaries.blogspot.com/2010/06/linux-c-socket-example-with-client.html"&gt;Linux C++ Socket Example with Client Server and Mulit-Threading &lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Run the example&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Open a prompt to the working directory and run the server. &lt;br /&gt;
&lt;table class="CD_Console_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;br /&gt;
..workspace\SocketExample&amp;gt;server &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Open another prompt to the working directory and run the Client. &lt;br /&gt;
&lt;table class="CD_Console_Table"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;br /&gt;
..workspace\SocketExample&amp;gt;client &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Now type something into the client prompt and press ENTER.&lt;/li&gt;
&lt;li&gt;You will see the exchange between client and server. You can open many client prompts at the same time and test.&lt;/li&gt;
&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4928424574574982882-7493421762192327841?l=www.tidytutorials.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/aPg9NjBrJQEfFWQ5bXgtpQm8cl0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/aPg9NjBrJQEfFWQ5bXgtpQm8cl0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/aPg9NjBrJQEfFWQ5bXgtpQm8cl0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/aPg9NjBrJQEfFWQ5bXgtpQm8cl0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeDiaries/~4/AlHAkMUNVWo" height="1" width="1"/&gt;</description><link>http://www.tidytutorials.com/2010/06/linux-c-forking-server-and-client.html</link><author>noreply@blogger.com (righteous)</author><thr:total>4</thr:total></item></channel></rss>

