User Tools

Site Tools


c:index

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
c:index [2009/08/13 09:50] – created tkbletscc:index [2011/03/03 11:28] (current) tkbletsc
Line 1: Line 1:
 +====== C/C++ code snippets ======
 +
 +
   * [[getopt]]   * [[getopt]]
   * [[key-value pairs]]   * [[key-value pairs]]
   * [[timing]]   * [[timing]]
 +  * [[memmem]] - find substrings of raw memory (as opposed to null terminated strings)
 +  * [[Getting mixed C and assembler output]]
  
 <code=c> <code=c>
Line 7: Line 12:
 #include <stdio.h> #include <stdio.h>
 #include <stdlib.h> #include <stdlib.h>
 +
 +#ifdef DEBUGMSG
 + #define DEBUG printf
 +#else
 + #define DEBUG(...) 
 +#endif
  
 #define die(...) { fprintf(stderr, __VA_ARGS__); exit(1); } #define die(...) { fprintf(stderr, __VA_ARGS__); exit(1); }
 #define pdie(s) { perror(s); exit(1); } #define pdie(s) { perror(s); exit(1); }
  
 +char parse_size(int64_t* v, char* s) {
 + char suffix;
 + if (sscanf(s,"%lld%c",v,&suffix)==2) {
 + if      (suffix=='k' || suffix=='K') (*v)*=1024;
 + else if (suffix=='m' || suffix=='M') (*v)*=1024*1024;
 + else if (suffix=='g' || suffix=='G') (*v)*=1024*1024*1024;
 + else if (suffix=='t' || suffix=='T') (*v)*=(int64_t)(1024*1024)*(int64_t)(1024*1024);
 + else return 0;
 + return 1;
 + } else if (sscanf(s,"%lld",v)==1) {
 + return 1;
 + }
 + return 0;
 +}
 +
 +char* tok;
 +for (tok = strtok(value,","); tok ; tok = strtok(NULL,",")) {
 +    ...
 +}
 </code> </code>
c/index.1250182208.txt.gz · Last modified: by tkbletsc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki