Logo Search packages:      
Sourcecode: pan version File versions

char * pan::file::get_unique_fname ( const gchar *  path,
const gchar *  fname 
)

Makes a unique filename given an optional path and a starting file name. The filename is sanitized before looking for uniqueness.

Definition at line 240 of file file-util.cc.

References file_exists(), normalize_inplace(), and sanitize().

{
   /* sanity checks */
   pan_return_val_if_fail (is_nonempty_string (fname), NULL);

   /* sanitize filename */
   char * temp_fn = sanitize (fname);
   GString * filename = g_string_new (temp_fn);
   g_free(temp_fn);

   /* add the directory & look for uniqueness */
   const char * front = filename->str;
   const char * lastdot = strrchr (front, '.');
   char * lead;
   char * tail;
   if (lastdot == NULL) {
      lead = g_strdup (front);
      tail = g_strdup ("");
   } else {
      lead = g_strndup (front, lastdot-front);
      tail = g_strdup (lastdot);
   }

   for (int i=1;; ++i)
   {
      char * unique;

      if (i==1 && is_nonempty_string(path))
      {
         unique = g_strdup_printf ("%s%c%s%s",
                             path, G_DIR_SEPARATOR,
                             lead, tail);
      }
      else if (i==1)
      {
         unique = g_strdup_printf ("%s%s", lead, tail);
      }
      else if (is_nonempty_string(path))
      {
         unique = g_strdup_printf ("%s%c%s_copy_%d%s",
                             path, G_DIR_SEPARATOR,
                             lead, i, tail);
      }
      else
      {
         unique = g_strdup_printf ("%s_copy_%d%s", lead, i, tail);
      }

      if (!file_exists (unique)) {
         g_string_assign (filename, unique);
         g_free (unique);
         break;
      }

      g_free (unique);
   }

   /* cleanup */
   g_free (lead);
   g_free (tail);

   return normalize_inplace (g_string_free (filename, FALSE));
}


Generated by  Doxygen 1.6.0   Back to index