Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
programming:howtos-tips [2008/11/15 08:29]
cyril
programming:howtos-tips [2014/04/16 18:07] (current)
cyril [Conversions with string]
Line 1: Line 1:
 ====== Howtos and Tips ====== ====== Howtos and Tips ======
  
 +===== System =====
  
-===== Catching signals =====+For details on functions search in the man page (''man <function-name>'' or ''man -a <function-name>'' if there is also a unix command with same name), or search the web. 
 + 
 +==== Catching signals ====
  
 => C and C++. => C and C++.
Line 33: Line 36:
 You can also change the signals actions with the [[http://www.opengroup.org/onlinepubs/007908799/xsh/sigaction.html|sigaction function]]. You can also change the signals actions with the [[http://www.opengroup.org/onlinepubs/007908799/xsh/sigaction.html|sigaction function]].
  
 +==== Timer ====
 +
 +<code c>
 +#include <unistd.h>
 +unsigned int alarm(unsigned int seconds);
 +</code>
 +will trigger signal SIGALRM x seconds later that you can catch (see [[#catching_signals]]).
 +
 +If you need automatic repetition and/or sub-second precision, you can use:
 +<code c>
 +#include <sys/time.h>
 +int setitimer(int whichtimer, const struct itimerval *value, struct itimerval *ovalue);
 +</code>
 +
 +==== Waiting event ====
 +
 +Monitor multiple file descriptors.
 +
 +<code c>
 +#include <sys/select.h>
 +int select(int nfds, fd_set *readfds, fd_set *writefds,
 +           fd_set *exceptfds, struct timeval *timeout);
 +</code>
 +
 +==== Posix Threads ====
 +
 +<code c>
 +#include <pthread.h>
 +
 +void *threadFunction(void *threadid)
 +{
 + printf("%d: Hello World!\n", threadid);
 + pthread_exit(NULL);
 +}
 +
 +int main (int argc, char *argv[])
 +{
 + pthread_t threadId;
 + int threadData = 42;
 + if (pthread_create(&threadId, NULL, threadFunction, (void *)threadData) != 0)
 + {
 + printf("thread creation failed");
 + exit(1);
 + }
 + return 0;
 +}
 +</code>
 +
 +FIXME join sem etc
 +
 +==== Change program owner ====
 +
 +<code c>
 +pwd = getpwnam("cyril");
 +if (pwd) 
 +{
 + setgid(pwd->pw_gid);
 + setuid(pwd->pw_uid);
 +}
 +</code>
 +
 +But it doesn't really work to start shell commands, you should use ''sudo'' too:
 +<code c>
 +system("sudo -u cyril <command>);
 +system("sudo -u cyril /bin/sh -c '<command1>; <command2>');
 +</code>
  
 ===== Formatting IOs (iomanip) ===== ===== Formatting IOs (iomanip) =====
Line 44: Line 113:
 for everything in this section. for everything in this section.
  
-==== Color ====+=== Color ===
  
  
 <code cpp> <code cpp>
-std::cout << "normal text, "  << "\033[1;28m"  +std::cout << "normal text, "  << "\033[1;33m"  
-          << "colored text, " << "\033[1;33m"+          << "colored text, " << "\033[1;00m"
           << "normal text" << std::endl;           << "normal text" << std::endl;
 </code> </code>
Line 66: Line 135:
  
  
-==== Floating point precision ====+=== Floating point precision ===
  
 <code cpp> <code cpp>
Line 76: Line 145:
  
  
-==== Alignment ====+=== Alignment ===
  
  
Line 85: Line 154:
 </code> </code>
  
-==== White spaces ====+=== White spaces ===
  
 To get all characters while they are white spaces. To get all characters while they are white spaces.
Line 113: Line 182:
 <code c> <code c>
   #define ERROR(msg, ...) fprintf("ERROR:%s:%d# ", __FILE__, __LINE__, ## __VA_ARGS__);   #define ERROR(msg, ...) fprintf("ERROR:%s:%d# ", __FILE__, __LINE__, ## __VA_ARGS__);
 +</code>
 +You can also use the ''PRETTY_FUNCTION'' but it is not standard:
 +<code>
 +  #define ERROR(msg, ...) fprintf("ERROR:%s:%d %s# ", __FILE__, __LINE__, __PRETTY_FUNCTION__, ## __VA_ARGS__);
 </code> </code>
  
Line 122: Line 195:
  
 ===== Conversions with string ===== ===== Conversions with string =====
-==== From * to string ====+=== From * to string ===
 => C++ => C++
  
Line 137: Line 210:
 #include <stdio.h> #include <stdio.h>
 char s[128]; char s[128];
-sprintf("%d", 565);+sprintf(s, "%d", 565);
 </code> </code>
  
-==== From string to * ====+=== From string to * ===
 => C & C++ => C & C++
  
Line 146: Line 219:
 #include <stdlib.h> #include <stdlib.h>
 int i = atoi("562"); int i = atoi("562");
-float f atof("4.5"); +long l atol("4500294"); 
-double atol("4.546643");+double atof("4.546643");
 </code> </code>
  
  
-===== Define in makefile =====+===== #define in compiler command =====
  
 You can define names for the preprocessor from the command line (or the makefile) instead of the source file (''#define name''). It can be very useful. You can define names for the preprocessor from the command line (or the makefile) instead of the source file (''#define name''). It can be very useful.
Line 165: Line 238:
  
 ===== Function pointers ===== ===== Function pointers =====
 +
  
 The full test program is available [[http://www.rez-metz.supelec.fr/wiki/lib/exe/fetch.php/users:cyril:programming:function_pointers.cpp.txt|here]] The full test program is available [[http://www.rez-metz.supelec.fr/wiki/lib/exe/fetch.php/users:cyril:programming:function_pointers.cpp.txt|here]]
  
  
-==== Normal fn ====+=== Normal fn ===
  
 <code cpp> <code cpp>
Line 186: Line 260:
 </code> </code>
  
-==== Static member fn ====+=== Static member fn ===
  
 Static member functions behave the same way than normal functions. Static member functions behave the same way than normal functions.
Line 197: Line 271:
 </code> </code>
  
-==== Non virtual member fn ====+=== Non virtual member fn ===
  
 Member functions have a hidden parameter (a pointer to ''this''), so the pointer needs an object to call the function. If the member function is non virtual, the address of the member function is the same for all objects. Member functions have a hidden parameter (a pointer to ''this''), so the pointer needs an object to call the function. If the member function is non virtual, the address of the member function is the same for all objects.
Line 208: Line 282:
 </code> </code>
  
-==== Virtual member fn ====+=== Virtual member fn ===
  
 The syntax is the same as for non virtual member functions, and the pointer points to the place in the object where the address of the function is stored, so that it always points to the right function. The syntax is the same as for non virtual member functions, and the pointer points to the place in the object where the address of the function is stored, so that it always points to the right function.
Line 223: Line 297:
  
 ===== Cast overloading ===== ===== Cast overloading =====
-==== Cast from ====+=== Cast from ===
  
 This is done with constructors : This is done with constructors :
Line 242: Line 316:
 </code> </code>
  
-==== Cast to ====+=== Cast to ===
  
 The syntax is simple : The syntax is simple :
Line 251: Line 325:
  public:  public:
   ...   ...
-  double() { return data / (double)(1<<8); }+  operator double() { return data / (double)(1<<8); }
 }; };
  
Line 259: Line 333:
 </code> </code>
  
-==== Template cast ====+=== Template cast ===
  
 Imagine the following situation : Imagine the following situation :
programming/howtos-tips.1226737782.txt.gz · Last modified: 2013/09/19 16:43 (external edit)
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0