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

Last change on this file since 772 was 772, checked in by meunier, 8 years ago
  • Ajout de l'application rosenfeld
  • Changement du nom du flag O_CREATE en O_CREAT
File size: 2.8 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
12#include "nrc_os_config.h"
13
14// --------------------------------------------
15void str_remove_ext(const char* src, char* dst)
16// --------------------------------------------
17{
18    int len;
19    char * ptr = NULL; // ptr to the position of the '.'
20   
21    if (src == NULL) {
22        printf("########################################");
23        printf("### str_remove_ext ERROR: src = NULL ###");
24        printf("########################################");
25        giet_pthread_exit("exit(-1)");
26    }
27    if (dst == NULL) {
28        printf("########################################");
29        printf("### str_remove_ext ERROR: dst = NULL ###");
30        printf("########################################");
31        giet_pthread_exit("exit(-1)");
32    }
33   
34    ptr = strchr(src, '.');
35    if (ptr != NULL) {
36        len = ptr - src;
37    }
38    else {
39        len = strlen(src);
40    }
41    // safe copy min(len, dst)
42    if (len < strlen(dst)) {
43        len = strlen(dst);
44    }
45    strncpy(dst, src, len);
46    dst[len] = '\0'; // do not forget to add null char, otherwise
47}
48// -------------------
49int str_len(char * str)
50// -------------------
51{
52    char * ptr = str;
53    while (*ptr) {
54        ptr++;
55    }
56    return ptr - str;
57}
58// ------------------------
59void str_toupper(char * str)
60// ------------------------
61{
62    // convert -in situ- all alpha char toupper
63   
64    int i, len;
65    //char c;
66    len = strlen(str);
67   
68    for (i = 0; i < len; i++) {
69        /*if(isalpha(str[i])) {
70            str[i] = toupper(str[i]);
71        }*/
72        /*c = str[i];
73        if(isalpha(c)) {
74            str[i] = toupper(c);
75        }*/
76    }
77}
78// ---------------------------
79void test_str_remove_ext(void)
80// ---------------------------
81{
82    char * str1 = "toto.txt";
83    char * str2 = "toto";
84    char * str3 = "";
85    char * str4 =".txt";
86    char * str5 =".";
87    char * str6 = NULL;
88   
89    char string[1024];
90   
91    printf("---------------------------");
92    printf("-- test_str_remove_ext() --");
93    printf("---------------------------");
94   
95    printf("display\n");
96    printf("str1 = %s\n", str1);
97    printf("str2 = %s\n", str2);
98    printf("str3 = %s\n", str3);
99    printf("str4 = %s\n", str4);
100   
101    printf("\nstr_remove\n");
102    str_remove_ext(str1, string); printf("'%s' -> '%s'\n", str1, string);
103    str_remove_ext(str2, string); printf("'%s' -> '%s'\n", str2, string);
104    str_remove_ext(str3, string); printf("'%s' -> '%s'\n", str3, string);
105    str_remove_ext(str4, string); printf("'%s' -> '%s'\n", str4, string);
106    str_remove_ext(str5, string); printf("'%s' -> '%s'\n", str5, string);
107    str_remove_ext(str6, string); printf("'%s' -> '%s'\n", str6, string); // plante
108}
Note: See TracBrowser for help on using the repository browser.