Dependencies: EthernetNetIf NetServices mbed HTTPServer
fonctions.cpp@0:96cf274f19bc, 2011-05-23 (annotated)
- Committer:
- etudiant12
- Date:
- Mon May 23 05:40:11 2011 +0000
- Revision:
- 0:96cf274f19bc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
etudiant12 | 0:96cf274f19bc | 1 | #include "stdio.h" |
etudiant12 | 0:96cf274f19bc | 2 | #include "time.h" |
etudiant12 | 0:96cf274f19bc | 3 | #include "stdlib.h" |
etudiant12 | 0:96cf274f19bc | 4 | #include "string.h" |
etudiant12 | 0:96cf274f19bc | 5 | #include "fonctions.h" |
etudiant12 | 0:96cf274f19bc | 6 | |
etudiant12 | 0:96cf274f19bc | 7 | // Fonctions permettant de travailler sur les chaines de caractere (source: http://nicolasj.developpez.com/articles/libc/string/#LII-J) |
etudiant12 | 0:96cf274f19bc | 8 | |
etudiant12 | 0:96cf274f19bc | 9 | //Fournit l'index d'une sous chaine |
etudiant12 | 0:96cf274f19bc | 10 | int str_istr (const char *cs, const char *ct) { |
etudiant12 | 0:96cf274f19bc | 11 | int index = -1; |
etudiant12 | 0:96cf274f19bc | 12 | |
etudiant12 | 0:96cf274f19bc | 13 | if (cs != NULL && ct != NULL) { |
etudiant12 | 0:96cf274f19bc | 14 | char *ptr_pos = NULL; |
etudiant12 | 0:96cf274f19bc | 15 | |
etudiant12 | 0:96cf274f19bc | 16 | ptr_pos = (char*)strstr(cs, ct); |
etudiant12 | 0:96cf274f19bc | 17 | if (ptr_pos != NULL) { |
etudiant12 | 0:96cf274f19bc | 18 | index = ptr_pos - cs; |
etudiant12 | 0:96cf274f19bc | 19 | } |
etudiant12 | 0:96cf274f19bc | 20 | } |
etudiant12 | 0:96cf274f19bc | 21 | return index; |
etudiant12 | 0:96cf274f19bc | 22 | } |
etudiant12 | 0:96cf274f19bc | 23 | |
etudiant12 | 0:96cf274f19bc | 24 | //Extrait une sous chaine en fonction d'indices limites |
etudiant12 | 0:96cf274f19bc | 25 | char *str_sub (const char *s, unsigned int start, unsigned int end) { |
etudiant12 | 0:96cf274f19bc | 26 | char *new_s = NULL; |
etudiant12 | 0:96cf274f19bc | 27 | |
etudiant12 | 0:96cf274f19bc | 28 | if (s != NULL && start < end) { |
etudiant12 | 0:96cf274f19bc | 29 | |
etudiant12 | 0:96cf274f19bc | 30 | new_s =(char*) malloc (sizeof (*new_s) * (end - start + 2)); |
etudiant12 | 0:96cf274f19bc | 31 | if (new_s != NULL) { |
etudiant12 | 0:96cf274f19bc | 32 | int i; |
etudiant12 | 0:96cf274f19bc | 33 | |
etudiant12 | 0:96cf274f19bc | 34 | for (i = start; i <= end; i++) { |
etudiant12 | 0:96cf274f19bc | 35 | new_s[i-start] = s[i]; |
etudiant12 | 0:96cf274f19bc | 36 | } |
etudiant12 | 0:96cf274f19bc | 37 | new_s[i-start] = '\0'; |
etudiant12 | 0:96cf274f19bc | 38 | } else { |
etudiant12 | 0:96cf274f19bc | 39 | fprintf (stderr, "Memoire insuffisante\n"); |
etudiant12 | 0:96cf274f19bc | 40 | exit (EXIT_FAILURE); |
etudiant12 | 0:96cf274f19bc | 41 | } |
etudiant12 | 0:96cf274f19bc | 42 | } |
etudiant12 | 0:96cf274f19bc | 43 | return new_s; |
etudiant12 | 0:96cf274f19bc | 44 | } |
etudiant12 | 0:96cf274f19bc | 45 | |
etudiant12 | 0:96cf274f19bc | 46 | // Extrait en plusieurs sous chaines en fonction d'une chaine de s�paration |
etudiant12 | 0:96cf274f19bc | 47 | char **str_split (char *s, const char *ct) { |
etudiant12 | 0:96cf274f19bc | 48 | char **tab = NULL; |
etudiant12 | 0:96cf274f19bc | 49 | |
etudiant12 | 0:96cf274f19bc | 50 | if (s != NULL && ct != NULL) { |
etudiant12 | 0:96cf274f19bc | 51 | int i; |
etudiant12 | 0:96cf274f19bc | 52 | char *cs = NULL; |
etudiant12 | 0:96cf274f19bc | 53 | size_t size = 1; |
etudiant12 | 0:96cf274f19bc | 54 | |
etudiant12 | 0:96cf274f19bc | 55 | for (i=0;(cs =(char *) strtok (s, ct)); i++) { |
etudiant12 | 0:96cf274f19bc | 56 | if (size <= i + 1) { |
etudiant12 | 0:96cf274f19bc | 57 | void *tmp = NULL; |
etudiant12 | 0:96cf274f19bc | 58 | |
etudiant12 | 0:96cf274f19bc | 59 | |
etudiant12 | 0:96cf274f19bc | 60 | size <<= 1; |
etudiant12 | 0:96cf274f19bc | 61 | tmp = realloc (tab, sizeof (*tab) * size); |
etudiant12 | 0:96cf274f19bc | 62 | if (tmp != NULL) { |
etudiant12 | 0:96cf274f19bc | 63 | tab =(char**) tmp; |
etudiant12 | 0:96cf274f19bc | 64 | } else { |
etudiant12 | 0:96cf274f19bc | 65 | fprintf (stderr, "Memoire insuffisante\n"); |
etudiant12 | 0:96cf274f19bc | 66 | free (tab); |
etudiant12 | 0:96cf274f19bc | 67 | tab = NULL; |
etudiant12 | 0:96cf274f19bc | 68 | exit (EXIT_FAILURE); |
etudiant12 | 0:96cf274f19bc | 69 | } |
etudiant12 | 0:96cf274f19bc | 70 | } |
etudiant12 | 0:96cf274f19bc | 71 | |
etudiant12 | 0:96cf274f19bc | 72 | tab[i] = cs; |
etudiant12 | 0:96cf274f19bc | 73 | s = NULL; |
etudiant12 | 0:96cf274f19bc | 74 | } |
etudiant12 | 0:96cf274f19bc | 75 | tab[i] = NULL; |
etudiant12 | 0:96cf274f19bc | 76 | } |
etudiant12 | 0:96cf274f19bc | 77 | return tab; |
etudiant12 | 0:96cf274f19bc | 78 | } |