[772] | 1 | // --------------- // |
---|
| 2 | // -- str_ext.c -- // |
---|
| 3 | // --------------- // |
---|
| 4 | |
---|
| 5 | // string extensions |
---|
| 6 | // Copyright (c) 2014 Lionel Lacassagne, All Rights Reserved |
---|
| 7 | // LRI, Univ Paris-Sud XI, CNRS |
---|
| 8 | |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #include <stdlib.h> |
---|
[777] | 11 | #include <string.h> |
---|
[772] | 12 | |
---|
| 13 | #include "nrc_os_config.h" |
---|
| 14 | |
---|
[821] | 15 | // ---------------------------------------------- |
---|
| 16 | void str_remove_ext(const char * src, char * dst) |
---|
| 17 | // ---------------------------------------------- |
---|
[772] | 18 | { |
---|
| 19 | int len; |
---|
| 20 | char * ptr = NULL; // ptr to the position of the '.' |
---|
| 21 | |
---|
| 22 | if (src == NULL) { |
---|
| 23 | printf("########################################"); |
---|
| 24 | printf("### str_remove_ext ERROR: src = NULL ###"); |
---|
| 25 | printf("########################################"); |
---|
| 26 | giet_pthread_exit("exit(-1)"); |
---|
| 27 | } |
---|
| 28 | if (dst == NULL) { |
---|
| 29 | printf("########################################"); |
---|
| 30 | printf("### str_remove_ext ERROR: dst = NULL ###"); |
---|
| 31 | printf("########################################"); |
---|
| 32 | giet_pthread_exit("exit(-1)"); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | ptr = strchr(src, '.'); |
---|
| 36 | if (ptr != NULL) { |
---|
| 37 | len = ptr - src; |
---|
| 38 | } |
---|
| 39 | else { |
---|
| 40 | len = strlen(src); |
---|
| 41 | } |
---|
| 42 | // safe copy min(len, dst) |
---|
| 43 | if (len < strlen(dst)) { |
---|
| 44 | len = strlen(dst); |
---|
| 45 | } |
---|
| 46 | strncpy(dst, src, len); |
---|
| 47 | dst[len] = '\0'; // do not forget to add null char, otherwise |
---|
| 48 | } |
---|
[821] | 49 | |
---|
| 50 | |
---|
[772] | 51 | // ------------------- |
---|
| 52 | int str_len(char * str) |
---|
| 53 | // ------------------- |
---|
| 54 | { |
---|
| 55 | char * ptr = str; |
---|
| 56 | while (*ptr) { |
---|
| 57 | ptr++; |
---|
| 58 | } |
---|
| 59 | return ptr - str; |
---|
| 60 | } |
---|
| 61 | // ------------------------ |
---|
| 62 | void str_toupper(char * str) |
---|
| 63 | // ------------------------ |
---|
| 64 | { |
---|
| 65 | // convert -in situ- all alpha char toupper |
---|
| 66 | |
---|
| 67 | int i, len; |
---|
| 68 | //char c; |
---|
| 69 | len = strlen(str); |
---|
| 70 | |
---|
| 71 | for (i = 0; i < len; i++) { |
---|
| 72 | /*if(isalpha(str[i])) { |
---|
| 73 | str[i] = toupper(str[i]); |
---|
| 74 | }*/ |
---|
| 75 | /*c = str[i]; |
---|
| 76 | if(isalpha(c)) { |
---|
| 77 | str[i] = toupper(c); |
---|
| 78 | }*/ |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | // --------------------------- |
---|
| 82 | void test_str_remove_ext(void) |
---|
| 83 | // --------------------------- |
---|
| 84 | { |
---|
| 85 | char * str1 = "toto.txt"; |
---|
| 86 | char * str2 = "toto"; |
---|
| 87 | char * str3 = ""; |
---|
| 88 | char * str4 =".txt"; |
---|
| 89 | char * str5 ="."; |
---|
| 90 | char * str6 = NULL; |
---|
| 91 | |
---|
| 92 | char string[1024]; |
---|
| 93 | |
---|
| 94 | printf("---------------------------"); |
---|
| 95 | printf("-- test_str_remove_ext() --"); |
---|
| 96 | printf("---------------------------"); |
---|
| 97 | |
---|
| 98 | printf("display\n"); |
---|
| 99 | printf("str1 = %s\n", str1); |
---|
| 100 | printf("str2 = %s\n", str2); |
---|
| 101 | printf("str3 = %s\n", str3); |
---|
| 102 | printf("str4 = %s\n", str4); |
---|
| 103 | |
---|
| 104 | printf("\nstr_remove\n"); |
---|
| 105 | str_remove_ext(str1, string); printf("'%s' -> '%s'\n", str1, string); |
---|
| 106 | str_remove_ext(str2, string); printf("'%s' -> '%s'\n", str2, string); |
---|
| 107 | str_remove_ext(str3, string); printf("'%s' -> '%s'\n", str3, string); |
---|
| 108 | str_remove_ext(str4, string); printf("'%s' -> '%s'\n", str4, string); |
---|
| 109 | str_remove_ext(str5, string); printf("'%s' -> '%s'\n", str5, string); |
---|
| 110 | str_remove_ext(str6, string); printf("'%s' -> '%s'\n", str6, string); // plante |
---|
| 111 | } |
---|