text
stringlengths
0
357
else {
return dc_strdup(pathNfilename);
}
}
void dc_split_filename(const char* pathNfilename, char** ret_basename, char** ret_all_suffixes_incl_dot)
{
/* splits a filename into basename and all suffixes, eg. "/path/foo.tar.gz" is split into "foo.tar" and ".gz",
(we use the _last_ dot which allows the usage inside the filename which are very usual;
maybe the detection could be more intelligent, however, for the moment, it is just file)
- if there is no suffix, the returned suffix string is empty, eg. "/path/foobar" is split into "foobar" and ""
- the case of the returned suffix is preserved; this is to allow reconstruction of (similar) names */
char* basename = dc_get_filename(pathNfilename);
char* suffix = NULL;
char* p1 = strrchr(basename, '.');
if (p1) {
suffix = dc_strdup(p1);
*p1 = 0;
}
else {
suffix = dc_strdup(NULL);
}
/* return the given values */
if (ret_basename ) { *ret_basename = basename; } else { free(basename); }
if (ret_all_suffixes_incl_dot) { *ret_all_suffixes_incl_dot = suffix; } else { free(suffix); }
}
char* dc_get_filesuffix_lc(const char* pathNfilename)
{
if (pathNfilename) {
const char* p = strrchr(pathNfilename, '.'); /* use the last point, we're interesting the "main" type */
if (p) {
p++;
return dc_strlower(p); /* in contrast to dc_split_filename() we return the lowercase suffix */
}
}
return NULL;
}
int dc_get_filemeta(const void* buf_start, size_t buf_bytes, uint32_t* ret_width, uint32_t *ret_height)
{
/* Strategy:
reading GIF dimensions requires the first 10 bytes of the file
reading PNG dimensions requires the first 24 bytes of the file
reading JPEG dimensions requires scanning through jpeg chunks
In all formats, the file is at least 24 bytes big, so we'll read that always
inspired by http://www.cplusplus.com/forum/beginner/45217/ */
const unsigned char* buf = buf_start;
if (buf_bytes<24) {
return 0;
}
/* For JPEGs, we need to check the first bytes of each DCT chunk. */
if (buf[0]==0xFF && buf[1]==0xD8 && buf[2]==0xFF)
{
long pos = 2;
while (buf[pos]==0xFF)
{
if (buf[pos+1]==0xC0 || buf[pos+1]==0xC1 || buf[pos+1]==0xC2 || buf[pos+1]==0xC3 || buf[pos+1]==0xC9 || buf[pos+1]==0xCA || buf[pos+1]==0xCB) {
*ret_height = (buf[pos+5]<<8) + buf[pos+6]; /* sic! height is first */
*ret_width = (buf[pos+7]<<8) + buf[pos+8];
return 1;
}
pos += 2+(buf[pos+2]<<8)+buf[pos+3];
if (pos+12>buf_bytes) { break; }
}
}
/* GIF: first three bytes say "GIF", next three give version number. Then dimensions */
if (buf[0]=='G' && buf[1]=='I' && buf[2]=='F')
{
*ret_width = buf[6] + (buf[7]<<8);
*ret_height = buf[8] + (buf[9]<<8);
return 1;
}
/* PNG: the first frame is by definition an IHDR frame, which gives dimensions */
if (buf[0]==0x89 && buf[1]=='P' && buf[2]=='N' && buf[3]=='G' && buf[4]==0x0D && buf[5]==0x0A && buf[6]==0x1A && buf[7]==0x0A
&& buf[12]=='I' && buf[13]=='H' && buf[14]=='D' && buf[15]=='R')
{
*ret_width = (buf[16]<<24) + (buf[17]<<16) + (buf[18]<<8) + (buf[19]<<0);
*ret_height = (buf[20]<<24) + (buf[21]<<16) + (buf[22]<<8) + (buf[23]<<0);
return 1;
}
return 0;
}
char* dc_get_abs_path(dc_context_t* context, const char* pathNfilename)
{
int success = 0;
char* pathNfilename_abs = NULL;
if (context==NULL || pathNfilename==NULL) {