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.
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.
C++, Java, Javascript, Perl, PHP we have all the examples. Easy to follow, easy to understand step by step tutorials.
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.
CodeDiaries Proxy Network - For free web and ip:port proxies using the Code Diaries proxy core.
Dirt Cheap Proxies - For fast and reliable managed proxies for $5/month
Tidy Tutorials
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,
C/C++ Regex Replace Function
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.
int regexreplace(char *pattern, char* replacement, char* instring, char** outstring, int maxlength);
It accespts 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.
Download the source code from here (.c file and .h file) When you compile make sure you include the regexreplace.c
int regexreplace(char *pattern, char* replacement, char* instring, char** outstring, int maxlength);
It accespts 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.
Download the source code from here (.c file and .h file) When you compile make sure you include the regexreplace.c
1. #include <stdlib.h> 2. #include <stdio.h> 3. #include <string.h> 4. 5. #include "regexreplace.h" 6. 7. int main(int vv, char** cc){ 8. 9. char* result; 10. 11. if(regexreplace("shaped_\\([0-9]*\\)_\\([0-9]*\\)", 12. "quota_policer(\\1,\\2)", 13. "shaped_500_400 and shaped_900_100", 14. &result, 15. 256)==0){ 16. 17. printf("====>%s<===\n",result); 18. free(result); 19. } 20. }Hide line numbers |
C/C++ Regular Expressions - Regex/Regexec
Aim
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 C/C++ regexreplace).
One of the drawbacks of regexec is that it doesn't match multiple times. For example if my string was '
This is hello_100_200_ and 700_600' it would only match the 100_200.
As you can see \\0 matches 100_200
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 C/C++ regexreplace).
1. #include <stdio.h> 2. #include <string.h> 3. #include <regex.h> 4. 5. int main(int vv, char** c){ 6. 7. regex_t pregx; 8. regmatch_t pmatch[10]; 9. int i; 10. const char* pattern = "\\([0-9][0-9]*\\)_\\([0-9][0-9]*\\)"; 11. char tomatch[256] = "This is hello_100_200_"; 12. char tmpvalue[256]; 13. 14. if(regcomp(&pregx, pattern, 0)!=0){ 15. printf("Pattern failed to compile\n"); 16. return -1; 17. } 18. if(regexec(&pregx, tomatch, 10, pmatch,0)!=0){ 19. printf("Could not match pattern %s %s\n",pattern, tomatch); 20. return -1; 21. } 22. 23. for(i = 0 ; i < 10 && pmatch[i].rm_so!=-1 ; i++){ 24. memset(tmpvalue, 0, 256); 25. strncpy(tmpvalue, &tomatch[pmatch[i].rm_so], pmatch[i].rm_eo-pmatch[i].rm_so); 26. printf("matched pmatch[%d].rm_so: %d pmatch[%d].rm_eo: %d - \"%s\"\n", 27. i, pmatch[i].rm_so, i, pmatch[i].rm_eo, tmpvalue); 28. } 29. return 0; 30. }Hide line numbers |
One of the drawbacks of regexec is that it doesn't match multiple times. For example if my string was '
This is hello_100_200_ and 700_600' it would only match the 100_200.
..workspace\>gcc -c reg.c ..workspace\>./a.out matched pmatch[0].rm_so: 14 pmatch[0].rm_eo: 21 - "100_200" matched pmatch[1].rm_so: 14 pmatch[1].rm_eo: 17 - "100" matched pmatch[2].rm_so: 18 pmatch[2].rm_eo: 21 - "200" |
Oracle TimesTen Getting Unixtime or Seconds Since the Epoch UTC
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 'An interval data type must be specified for a datetime arithmetic result.' The easiest work around is to cast the subtraction as a bigint and divide by 1000000
On TimesTen - sql
select cast((sysdate - to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')) as bigint)/1000000 from dual
On conventional oracle database - sql
select (sysdate - to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')) * 86400 from dual
On TimesTen - sql
select cast((sysdate - to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')) as bigint)/1000000 from dual
On conventional oracle database - sql
select (sysdate - to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')) * 86400 from dual
C/C++ Trimming whitespace - Trim
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.
1. int trim(char* c){ 2. char * i; 3. if(c==0 || !strlen(c)) 4. return 0; 5. for(i=c ; isspace(*i)!=0 && (i-c)<strlen(c); i++); 6. memmove(c, i, strlen(c)-(i-c)+1); 7. if(!strlen(c)) 8. return 0; 9. for(i=(c+strlen(c)-1) ; isspace(*i)!=0 && i>c; i--); 10. *(i+1)='\0'; 11. return strlen(c); 12. } 13. 14. int main(int vv, char** cc){ 15. char xman[] =" \t String that needs trimming \n "; 16. trim(xman); 17. printf ("--%s--\n", xman); 18. }Hide line numbers |
Subscribe to:
Posts (Atom)
