2018-02-26 11:33:24 +01:00
|
|
|
/*
|
|
|
|
This file is part of actube.
|
2018-02-26 11:32:22 +01:00
|
|
|
|
2018-02-26 11:33:24 +01:00
|
|
|
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.
|
2018-02-26 11:32:22 +01:00
|
|
|
|
2018-02-26 11:33:24 +01:00
|
|
|
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.
|
2018-02-26 11:32:22 +01:00
|
|
|
|
2018-02-26 11:33:24 +01:00
|
|
|
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.
|
|
|
|
*/
|
2018-03-31 07:43:30 +02:00
|
|
|
char *cw_filename(const char *path, const char *name, const char *ext)
|
|
|
|
{
|
2018-03-25 10:07:39 +02:00
|
|
|
int name_len;
|
2018-03-31 07:43:30 +02:00
|
|
|
int ext_len;
|
|
|
|
int size = 0;
|
|
|
|
|
2018-02-26 11:33:24 +01:00
|
|
|
int path_len;
|
|
|
|
char c;
|
2018-03-31 07:43:30 +02:00
|
|
|
char *result;
|
|
|
|
char *slash = "";
|
|
|
|
|
2018-02-26 11:33:24 +01:00
|
|
|
path_len = strlen(path);
|
2018-03-31 07:43:30 +02:00
|
|
|
if (path_len > 0) {
|
|
|
|
c = path[path_len - 1];
|
|
|
|
if (!strchr(CW_FILE_PATH_DELIMITERS, c)) {
|
2018-02-26 11:33:24 +01:00
|
|
|
path_len++;
|
|
|
|
slash = "/";
|
|
|
|
}
|
|
|
|
}
|
2018-03-25 10:07:39 +02:00
|
|
|
|
2018-03-31 07:43:30 +02:00
|
|
|
name_len = strlen(name);
|
|
|
|
|
|
|
|
if (ext != NULL) {
|
|
|
|
if (ext[0] == '.') {
|
2018-02-26 11:33:24 +01:00
|
|
|
ext++;
|
|
|
|
}
|
|
|
|
ext_len = strlen(ext);
|
2018-03-31 07:43:30 +02:00
|
|
|
} else
|
|
|
|
ext_len = -1;
|
2018-02-26 11:33:24 +01:00
|
|
|
|
2018-03-31 07:43:30 +02:00
|
|
|
size = (path_len) + name_len + (ext_len + 1) + 1;
|
2018-03-25 10:07:39 +02:00
|
|
|
result = malloc(size);
|
2018-02-26 11:33:24 +01:00
|
|
|
if (!result)
|
|
|
|
return NULL;
|
2018-03-31 07:43:30 +02:00
|
|
|
|
|
|
|
strncpy(result, path, path_len);
|
|
|
|
result[path_len] = 0;
|
|
|
|
strcat(result, slash);
|
|
|
|
strcat(result, name);
|
|
|
|
if (ext) {
|
|
|
|
strcat(result, ".");
|
|
|
|
strcat(result, ext);
|
2018-02-26 11:33:24 +01:00
|
|
|
}
|
2018-03-31 07:43:30 +02:00
|
|
|
|
2018-02-26 11:33:24 +01:00
|
|
|
return result;
|
|
|
|
}
|