source: soft/giet_vm/applications/rosenfeld/src/str_ext.c @ 777

Last change on this file since 777 was 777, checked in by meunier, 8 years ago
  • Ajout de quelques fonction dans la lib math
  • Déplacement de certaines fonctions de stdlib vers string
File size: 2.9 KB
Line 
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>
11#include <string.h>
12
13#include "nrc_os_config.h"
14
15// --------------------------------------------
16void str_remove_ext(const char* src, char* dst)
17// --------------------------------------------
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}
49// -------------------
50int str_len(char * str)
51// -------------------
52{
53    char * ptr = str;
54    while (*ptr) {
55        ptr++;
56    }
57    return ptr - str;
58}
59// ------------------------
60void str_toupper(char * str)
61// ------------------------
62{
63    // convert -in situ- all alpha char toupper
64   
65    int i, len;
66    //char c;
67    len = strlen(str);
68   
69    for (i = 0; i < len; i++) {
70        /*if(isalpha(str[i])) {
71            str[i] = toupper(str[i]);
72        }*/
73        /*c = str[i];
74        if(isalpha(c)) {
75            str[i] = toupper(c);
76        }*/
77    }
78}
79// ---------------------------
80void test_str_remove_ext(void)
81// ---------------------------
82{
83    char * str1 = "toto.txt";
84    char * str2 = "toto";
85    char * str3 = "";
86    char * str4 =".txt";
87    char * str5 =".";
88    char * str6 = NULL;
89   
90    char string[1024];
91   
92    printf("---------------------------");
93    printf("-- test_str_remove_ext() --");
94    printf("---------------------------");
95   
96    printf("display\n");
97    printf("str1 = %s\n", str1);
98    printf("str2 = %s\n", str2);
99    printf("str3 = %s\n", str3);
100    printf("str4 = %s\n", str4);
101   
102    printf("\nstr_remove\n");
103    str_remove_ext(str1, string); printf("'%s' -> '%s'\n", str1, string);
104    str_remove_ext(str2, string); printf("'%s' -> '%s'\n", str2, string);
105    str_remove_ext(str3, string); printf("'%s' -> '%s'\n", str3, string);
106    str_remove_ext(str4, string); printf("'%s' -> '%s'\n", str4, string);
107    str_remove_ext(str5, string); printf("'%s' -> '%s'\n", str5, string);
108    str_remove_ext(str6, string); printf("'%s' -> '%s'\n", str6, string); // plante
109}
Note: See TracBrowser for help on using the repository browser.