Just a recent run-in with productivity afforded by Qt.

New code (cross-platform):

 int filecmp(const QString & filename1, const QString & filename2)
{
    QFileInfo fi1(filename1);
    QFileInfo fi2(filename2);
    if (fi1.absoluteFilePath() == fi2.absoluteFilePath()) return 0;
    return 1;
}

Old code (Posix only):

int filecmp(char *filename1, char *filename2)
{
   char *root1, *root2, *path1, *path2, *end1, *end2;
   int rval;
   struct stat statbuf;
   ino_t inode1;
   const char *cwdname = ".";

   if (filename1 == NULL || filename2 == NULL) return 1;

   if (!strcmp(filename1, filename2)) return 0; /* exact match */

   root1 = strrchr(filename1, PATHSEP);
   root2 = strrchr(filename2, PATHSEP);

   if (root1 == NULL) {
      path1 = (char *)cwdname;
      end1 = NULL;
      root1 = filename1;
   }
   else {
      path1 = filename1;
      end1 = root1;
      root1++;
   }

   if (root2 == NULL) {
      path2 = (char *)cwdname;
      end2 = NULL;
      root2 = filename2;
   }
   else {
      path2 = filename2;
      end2 = root2;
      root2++;
   }
  
   if (strcmp(root1, root2)) return 1;  /* root names don't match */

   /* If we got here, one or both filenames specify a directory */
   /* path, and the directory paths are different strings.      */
   /* However, one may be an absolute path and the other a      */
   /* relative path, so we check the inodes of the paths for    */
   /* equivalence.  Note that the file itself is not assumed to */
   /* exist.                                                    */

   rval = 1;
   if (end1 != NULL) *end1 = '\0';
   if (stat(path1, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
      inode1 = statbuf.st_ino;
      if (end2 != NULL) *end2 = '\0';
      if (stat(path2, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
         if (inode1 == statbuf.st_ino)
            rval = 0;
      }
      if (end2 != NULL) *end2 = PATHSEP;
   }
   if (end1 != NULL) *end1 = PATHSEP;
   return rval;
}

There are plenty of places where I do just that: reduce code size by an order
of magnitude. I like the feeling.

Cheers, Kuba
_______________________________________________
Xcircuit-dev mailing list
[email protected]
http://www.opencircuitdesign.com/mailman/listinfo/xcircuit-dev

Reply via email to