guess_short_name.c [plain text]
#ifndef RLD
#include <string.h>
#include "stuff/bool.h"
#include "stuff/errors.h"
#include "stuff/allocate.h"
#include "stuff/guess_short_name.h"
static char *look_back_for_slash(
char *name,
char *p);
__private_extern__
char *
guess_short_name(
char *name,
enum bool *is_framework,
char **return_suffix)
{
char *foo, *a, *b, *c, *d, *suffix, *guess;
unsigned long l, s;
*is_framework = FALSE;
*return_suffix = NULL;
a = strrchr(name, '/');
if(a == NULL)
goto guess_library;
if(a == name)
goto guess_library;
foo = a + 1;
l = strlen(foo);
suffix = strrchr(foo, '_');
if(suffix != NULL){
s = strlen(suffix);
if(suffix == foo || s < 2)
suffix = NULL;
else{
l -= s;
*return_suffix = allocate(s + 1);
strncpy(*return_suffix, suffix, s);
(*return_suffix)[s] = '\0';
}
}
b = look_back_for_slash(name, a);
if(b == NULL){
if(strncmp(name, foo, l) == 0 &&
strncmp(name + l, ".framework/", sizeof(".framework/")-1 ) == 0){
guess = allocate(l + 1);
strncpy(guess, name, l);
guess[l] = '\0';
*is_framework = TRUE;
return(guess);
}
else
goto guess_library;
}
else{
if(strncmp(b+1, foo, l) == 0 &&
strncmp(b+1 + l, ".framework/", sizeof(".framework/")-1 ) == 0){
guess = allocate(l + 1);
strncpy(guess, b+1, l);
guess[l] = '\0';
*is_framework = TRUE;
return(guess);
}
}
if(b == name)
goto guess_library;
c = look_back_for_slash(name, b);
if(c == NULL ||
c == name ||
strncmp(c+1, "Versions/", sizeof("Versions/")-1) != 0)
goto guess_library;
d = look_back_for_slash(name, c);
if(d == NULL){
if(strncmp(name, foo, l) == 0 &&
strncmp(name + l, ".framework/", sizeof(".framework/")-1 ) == 0){
guess = allocate(l + 1);
strncpy(guess, name, l);
guess[l] = '\0';
*is_framework = TRUE;
return(guess);
}
else
goto guess_library;
}
else{
if(strncmp(d+1, foo, l) == 0 &&
strncmp(d+1 + l, ".framework/", sizeof(".framework/")-1 ) == 0){
guess = allocate(l + 1);
strncpy(guess, d+1, l);
guess[l] = '\0';
*is_framework = TRUE;
return(guess);
}
else
goto guess_library;
}
guess_library:
a = strrchr(name, '.');
if(a == NULL)
return(NULL);
if(a == name)
return(NULL);
if(strcmp(a, ".dylib") != 0)
goto guess_qtx;
if(a - name >= 3 && a[-2] == '.')
a = a - 2;
b = look_back_for_slash(name, a);
if(b == name)
return(NULL);
if(b == NULL){
c = strchr(name, '_');
if(c != NULL && c != name){
l = c - name;
suffix = c;
for(s = 0; suffix[s] != '.'; s++)
;
*return_suffix = allocate(s + 1);
strncpy(*return_suffix, suffix, s);
(*return_suffix)[s] = '\0';
}
else
l = a - name;
if(l >= 3 && name[l-2] == '.')
l = l - 2;
guess = allocate(l + 1);
strncpy(guess, name, l);
guess[l] = '\0';
return(guess);
}
else{
c = strchr(b+1, '_');
if(c != NULL && c != b+1){
l = c - (b+1);
suffix = c;
for(s = 0; suffix[s] != '.'; s++)
;
*return_suffix = allocate(s + 1);
strncpy(*return_suffix, suffix, s);
(*return_suffix)[s] = '\0';
}
else
l = a - (b+1);
if(l >= 3 && b[1+l-2] == '.')
l = l - 2;
guess = allocate(l + 1);
strncpy(guess, b+1, l);
guess[l] = '\0';
return(guess);
}
guess_qtx:
a = strrchr(name, '.');
if(strcmp(a, ".qtx") != 0)
return(NULL);
b = look_back_for_slash(name, a);
if(b == name)
return(NULL);
if(b == NULL){
l = a - name;
if(l >= 3 && name[l-2] == '.')
l = l - 2;
guess = allocate(l + 1);
strncpy(guess, name, l);
guess[l + 1] = '\0';
return(guess);
}
else{
l = a - (b+1);
if(l >= 3 && b[1+l-2] == '.')
l = l - 2;
guess = allocate(l + 1);
strncpy(guess, b+1, l);
guess[l + 1] = '\0';
return(guess);
}
}
static
char *
look_back_for_slash(
char *name,
char *p)
{
for(p = p - 1; p >= name; p--){
if(*p == '/')
return(p);
}
return(NULL);
}
#endif