cw_filename function added

FossilOrigin-Name: a3170b6e3e975c442df9656866b8e936d2417cad24f673f25fc71516c307da34
This commit is contained in:
7u83@mail.ru 2018-02-26 10:33:24 +00:00
parent d81661f639
commit 775aa8d133
3 changed files with 82 additions and 1 deletions

View File

@ -77,6 +77,7 @@ MAVLOBJS= \
mlist.o \ mlist.o \
cw_load_file.o \ cw_load_file.o \
cw_save_file.o \ cw_save_file.o \
cw_filename.o \
message_set.o message_set.o

View File

@ -1,4 +1,81 @@
/*
This file is part of actube.
actube is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
actube is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
char * cw_filename(const char * path You should have received a copy of the GNU General Public License
along with actube. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief implents cw_filename
**/
#include <string.h>
#include <stdio.h>
#include "file.h"
/**
* @brief Build full pathname from path, filename and extension
* @param path Path
* @param name Filename
* @param ext Extension
* @return a pointer to the create filename which has to be
* released using free.
*/
char * cw_filename(const char * path, const char * name, const char * ext){
int size=0;
int path_len;
char c;
char * slash="";
path_len = strlen(path);
if (path_len>0){
c = path[path_len-1];
if (!strchr(CW_FILE_PATH_DELIMITERS,c)){
path_len++;
slash = "/";
}
}
int name_len = strlen (name);
int ext_len;
if (ext != NULL){
if (ext[0]=='.'){
ext++;
}
ext_len = strlen(ext);
}
else
ext_len=-1;
size = (path_len) + name_len + (ext_len+1) + 1;
char * result = malloc(size);
if (!result)
return NULL;
strncpy(result,path,path_len);
result[path_len]=0;
strcat(result,slash);
strcat(result,name);
if (ext){
strcat(result,".");
strcat(result,ext);
}
return result;
}

View File

@ -10,6 +10,9 @@
char *cw_load_file(const char *filename,size_t *size); char *cw_load_file(const char *filename,size_t *size);
int cw_save_file(const char *filename, char *data,int len); int cw_save_file(const char *filename, char *data,int len);
char * cw_filename(const char * path, const char * name, const char * ext);
#define CW_FILE_PATH_DELIMITERS "/"
/**@}*/ /**@}*/