From 775aa8d13315013e4c9a8e4883f41e3201dbb0e2 Mon Sep 17 00:00:00 2001 From: "7u83@mail.ru" <7u83@mail.ru@noemail.net> Date: Mon, 26 Feb 2018 10:33:24 +0000 Subject: [PATCH] cw_filename function added FossilOrigin-Name: a3170b6e3e975c442df9656866b8e936d2417cad24f673f25fc71516c307da34 --- src/cw/Makefile | 1 + src/cw/cw_filename.c | 79 +++++++++++++++++++++++++++++++++++++++++++- src/cw/file.h | 3 ++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/cw/Makefile b/src/cw/Makefile index 8984a7c5..7633f800 100644 --- a/src/cw/Makefile +++ b/src/cw/Makefile @@ -77,6 +77,7 @@ MAVLOBJS= \ mlist.o \ cw_load_file.o \ cw_save_file.o \ + cw_filename.o \ message_set.o diff --git a/src/cw/cw_filename.c b/src/cw/cw_filename.c index 9b113442..071a202c 100644 --- a/src/cw/cw_filename.c +++ b/src/cw/cw_filename.c @@ -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 . +*/ +/** + * @file + * @brief implents cw_filename + **/ + +#include +#include +#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; +} diff --git a/src/cw/file.h b/src/cw/file.h index 2585a8c7..7eaac611 100644 --- a/src/cw/file.h +++ b/src/cw/file.h @@ -10,6 +10,9 @@ char *cw_load_file(const char *filename,size_t *size); 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 "/" /**@}*/