OSDN Git Service

Initial release for public
[lib1stclass/main.git] / remove_not_file_char.c
1 /*
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright: 2010- 1stclass.co.jp.  All rights reserved.
5 *
6 * Created by Hajime Kurita
7 */
8 #include<string.h>
9 #include<stdio.h>
10
11 #ifdef __cplusplus
12 #include<string>
13 #include "1stclass.hpp"
14 string firstclass::remove_not_file_char(const string *in_str){
15   char *str=NULL;
16   char *dst=NULL;
17   try {
18     str=new char[sizeof(char)*(in_str->length()+1)];
19     dst=new char[sizeof(char)*(in_str->length()+1)];
20     str[0]='\0';
21     dst[0]='\0';
22     safe_strcat(str, in_str->c_str());
23 #else
24 #include "lib1stclass.h"
25 int remove_not_file_char(const char *str, char *dst){
26 #endif
27   if(sizeof(dst)<sizeof(str)){
28     printf("Bad parameter for remove_not_file_character\n");
29 #ifdef __cplusplus
30     if(str!=NULL)
31       delete[] str;
32     if(dst!=NULL)
33       delete[] dst;
34     return "";
35 #else
36     return 1;
37 #endif
38   }
39   if((strrchr(str, '\0'))==NULL){
40     printf("\\0 is missing for the input parameter of remove_not_file_character\n");
41 #ifdef __cplusplus
42     if(str!=NULL)
43       delete[] str;
44     if(dst!=NULL)
45       delete[] dst;
46     return "";
47 #else
48     return 1;
49 #endif
50   }
51   unsigned int i=0;
52   for(i=0;i<=strlen(str);i++){
53     if(str[i]=='\\' || str[i]=='/' || str[i]==':' || str[i]=='*' || str[i]=='?' || str[i]=='"' || str[i]=='<' || str[i]=='>' || str[i]=='|'){
54       dst[i]='_';
55     }
56     else if(str[i] == '\0'){
57       dst[i]='\0';
58       break;
59     }
60     else{
61       dst[i]=str[i];
62     }
63   }
64     
65 #ifdef __cplusplus
66   
67     string result(dst);
68   if(str!=NULL)
69     delete[] str;
70   if(dst!=NULL)
71     delete[] dst;
72     return result;
73   }
74   catch(...){
75     if(str!=NULL)
76       delete[] str;
77     if(dst!=NULL)
78       delete[] dst;
79     return "";
80   }
81 #else
82   return 0;
83 #endif
84 }