Example #1
0
File: fileio.c Project: barak/lush
/* lush_basename returns a new, managed string */
const char *lush_basename(const char *fname, const char *suffix)
{
#ifdef UNIX
   if (strlen(fname) > STRING_BUFFER-4)
      RAISEF("filename is too long",NIL);
   char *s = strrchr(fname,'/');
   if (s)
      fname = s+1;
   /* Process suffix */
   if (suffix==0 || suffix[0]==0)
      return mm_strdup(fname);
   if (suffix[0]=='.')
      suffix += 1;
   if (suffix[0]==0)
      return mm_strdup(fname);
   strcpyif(string_buffer,fname);
   int sl = strlen(suffix);
   s = string_buffer + strlen(string_buffer);
   if (s > string_buffer + sl) {
      s =  s - (sl + 1);
      if (s[0]=='.' && strcmp(s+1,suffix)==0)
         *s = 0;
   }
   return mm_strdup(string_buffer);
#endif
  
#ifdef WIN32
   const char *p = fname;
   const char *s = fname;
   /* Special cases */
   if (fname[0] && fname[1]==':') {
      strcpyif(string_buffer,fname);
      if (fname[2]==0)
         return mm_strdup(string_buffer);
      string_buffer[2] = '\\'; 
      if (fname[3]==0 && (fname[2]=='/' || fname[2]=='\\'))
         return mm_strdup(string_buffer);
   }
   /* Position p after last slash */
   while (*s) {
      if (s[0]=='\\' || s[0]=='/')
         p = s + 1;
      s++;
   }
   /* Copy into buffer */
   if (strlen(p) > STRING_BUFFER-10)
      RAISEF("filename too long",NIL);
   s = string_buffer;
   while (*p && *p!='/' && *p!='\\')
      *s++ = *p++;
   *s = 0;
   /* Process suffix */
   if (suffix==0 || suffix[0]==0)
      return mm_strdup(string_buffer);
   if (suffix[0]=='.')
      suffix += 1;
   if (suffix[0]==0)
      return mm_strdup(string_buffer);    
   int sl = strlen(suffix);
   if (s > string_buffer + sl) {
      s = s - (sl + 1);
      if (s[0]=='.' && stricmp(s+1,suffix)==0)
         *s = 0;
   }
   return mm_strdup(string_buffer);
#endif
}
Example #2
0
const char *
basename(const char *fname, const char *suffix)
{
  /* UNIX implementation */
#ifdef UNIX
  int sl;
  char *s;
  if (strlen(fname) > STRING_BUFFER-4)
    error(NIL,"filename is too long",NIL);
  s = strrchr(fname,'/');
  if (s)
    fname = s+1;
  /* Process suffix */
  if (suffix==0 || suffix[0]==0)
    return fname;
  if (suffix[0]=='.')
    suffix += 1;
  if (suffix[0]==0)
    return fname;
  strcpyif(string_buffer,fname);
  sl = strlen(suffix);
  s = string_buffer + strlen(string_buffer);
  if (s > string_buffer + sl) {
    s =  s - (sl + 1);
    if (s[0]=='.' && strcmp(s+1,suffix)==0)
      *s = 0;
  }
  return string_buffer;
#endif
  
  /* WIN32 implementation */
#ifdef WIN32
  int sl;
  char *p = (char*)fname;
  char *s = (char*)fname;
  /* Special cases */
  if (fname[0] && fname[1]==':') {
    strcpyif(string_buffer,fname);
    if (fname[2]==0)
      return string_buffer;
    string_buffer[2] = '\\'; 
    if (fname[3]==0 && (fname[2]=='/' || fname[2]=='\\'))
      return string_buffer;
  }
  /* Position p after last slash */
  while (*s) {
    if (s[0]=='\\' || s[0]=='/')
        p = s + 1;
    s++;
  }
  /* Copy into buffer */
  if (strlen(p) > STRING_BUFFER-10)
    error(NIL,"Filename too long",NIL);
  s = string_buffer;
  while (*p && *p!='/' && *p!='\\')
    *s++ = *p++;
  *s = 0;
  /* Process suffix */
  if (suffix==0 || suffix[0]==0)
    return string_buffer;
  if (suffix[0]=='.')
    suffix += 1;
  if (suffix[0]==0)
    return string_buffer;    
  sl = strlen(suffix);
  if (s > string_buffer + sl) {
    s = s - (sl + 1);
    if (s[0]=='.' && stricmp(s+1,suffix)==0)
      *s = 0;
  }
  return string_buffer;
#endif
}