[geany/geany] f1dbf2: Grab ctags implementation of sort.c/h

Jiří Techet git-noreply at xxxxx
Mon Dec 17 21:05:39 UTC 2018


Branch:      refs/heads/master
Author:      Jiří Techet <techet at gmail.com>
Committer:   Jiří Techet <techet at gmail.com>
Date:        Sat, 08 Oct 2016 12:13:41 UTC
Commit:      f1dbf2cb7b4b5695a3f735c4819b35c3254f3493
             https://github.com/geany/geany/commit/f1dbf2cb7b4b5695a3f735c4819b35c3254f3493

Log Message:
-----------
Grab ctags implementation of sort.c/h

I haven't studies the changes much but this file is responsible for sorting
tag files which isn't interesing for us.


Modified Paths:
--------------
    ctags/main/options.h
    ctags/main/sort.c
    ctags/main/sort.h

Modified: ctags/main/options.h
5 lines changed, 5 insertions(+), 0 deletions(-)
===================================================================
@@ -31,6 +31,11 @@
 *   DATA DECLARATIONS
 */
 
+typedef enum sortType {
+	SO_UNSORTED,
+	SO_SORTED,
+	SO_FOLDSORTED
+} sortType;
 
 /*  This stores the command line options.
  */


Modified: ctags/main/sort.c
105 lines changed, 66 insertions(+), 39 deletions(-)
===================================================================
@@ -12,39 +12,38 @@
 */
 #include "general.h"  /* must always come first */
 
+#if defined (HAVE_IO_H)
+# include <io.h>
+#endif
 #if defined (HAVE_STDLIB_H)
 # include <stdlib.h>  /* to declare malloc () */
 #endif
+#if defined (HAVE_UNISTD_H)
+# include <unistd.h>
+#endif
 #include <string.h>
 #include <stdio.h>
 
 #include "debug.h"
 #include "entry.h"
-#include "routines.h"
 #include "options.h"
 #include "read.h"
+#include "routines.h"
 #include "sort.h"
 
-#ifdef TRAP_MEMORY_CALLS
-# include "safe_malloc.h"
-#endif
-
 /*
 *   FUNCTION DEFINITIONS
 */
 
-extern void catFile (const char *const name)
+extern void catFile (MIO *mio)
 {
-	FILE *const fp = fopen (name, "r");
-
-	if (fp != NULL)
+	if (mio != NULL)
 	{
 		int c;
-
-		while ((c = getc (fp)) != EOF)
+		mio_seek (mio, 0, SEEK_SET);
+		while ((c = mio_getc (mio)) != EOF)
 			putchar (c);
 		fflush (stdout);
-		fclose (fp);
 	}
 }
 
@@ -56,9 +55,12 @@ extern void catFile (const char *const name)
 # define PE_CONST const
 #endif
 
-extern void externalSortTags (const bool toStdout)
+extern void externalSortTags (const bool toStdout, MIO *tagFile)
 {
-	const char *const sortCommand = "sort -u -o";
+	const char *const sortNormalCommand = "sort -u";
+	const char *const sortFoldedCommand = "sort -u -f";
+	const char *sortCommand =
+		Option.sorted == SO_FOLDSORTED ? sortFoldedCommand : sortNormalCommand;
 	PE_CONST char *const sortOrder1 = "LC_COLLATE=C";
 	PE_CONST char *const sortOrder2 = "LC_ALL=C";
 	const size_t length = 4 + strlen (sortOrder1) + strlen (sortOrder2) +
@@ -70,29 +72,51 @@ extern void externalSortTags (const bool toStdout)
 	{
 		/*  Ensure ASCII value sort order.
 		 */
-#ifdef HAVE_SETENV
+#if defined (HAVE_SETENV) || defined (HAVE_PUTENV)
+# ifdef HAVE_SETENV
 		setenv ("LC_COLLATE", "C", 1);
 		setenv ("LC_ALL", "C", 1);
-		sprintf (cmd, "%s %s %s", sortCommand, tagFileName (), tagFileName ());
-#else
-# ifdef HAVE_PUTENV
+# else
 		putenv (sortOrder1);
 		putenv (sortOrder2);
-		sprintf (cmd, "%s %s %s", sortCommand, tagFileName (), tagFileName ());
-# else
-		sprintf (cmd, "%s %s %s %s %s", sortOrder1, sortOrder2, sortCommand,
-				tagFileName (), tagFileName ());
 # endif
+		if (toStdout)
+			sprintf (cmd, "%s", sortCommand);
+		else
+			sprintf (cmd, "%s -o %s %s", sortCommand,
+					tagFileName (), tagFileName ());
+#else
+		if (toStdout)
+			sprintf (cmd, "%s %s %s", sortOrder1, sortOrder2, sortCommand);
+		else
+			sprintf (cmd, "%s %s %s -o %s %s", sortOrder1, sortOrder2,
+					sortCommand, tagFileName (), tagFileName ());
 #endif
 		verbose ("system (\"%s\")\n", cmd);
-		ret = system (cmd);
+		if (toStdout)
+		{
+			const int fdstdin = 0;
+			int fdsave;
+
+			fdsave = dup (fdstdin);
+			if (fdsave < 0)
+				error (FATAL | PERROR, "cannot save stdin fd");
+			if (dup2 (fileno (mio_file_get_fp (tagFile)), fdstdin) < 0)
+				error (FATAL | PERROR, "cannot redirect stdin");
+			if (lseek (fdstdin, 0, SEEK_SET) != 0)
+				error (FATAL | PERROR, "cannot rewind tag file");
+			ret = system (cmd);
+			if (dup2 (fdsave, fdstdin) < 0)
+				error (FATAL | PERROR, "cannot restore stdin fd");
+			close (fdsave);
+		}
+		else
+			ret = system (cmd);
 		free (cmd);
 
 	}
 	if (ret != 0)
 		error (FATAL | PERROR, "cannot sort tag file");
-	else if (toStdout)
-		catFile (tagFileName ());
 }
 
 #else
@@ -103,7 +127,7 @@ extern void externalSortTags (const bool toStdout)
  *  so have lots of memory if you have large tag files.
  */
 
-static void failedSort (MIO *const mio, const char* msg)
+extern void failedSort (MIO *const mio, const char* msg)
 {
 	const char* const cannotSort = "cannot sort tag file";
 	if (mio != NULL)
@@ -114,6 +138,14 @@ static void failedSort (MIO *const mio, const char* msg)
 		error (FATAL, "%s: %s", msg, cannotSort);
 }
 
+static int compareTagsFolded(const void *const one, const void *const two)
+{
+	const char *const line1 = *(const char* const*) one;
+	const char *const line2 = *(const char* const*) two;
+
+	return struppercmp (line1, line2);
+}
+
 static int compareTags (const void *const one, const void *const two)
 {
 	const char *const line1 = *(const char* const*) one;
@@ -148,32 +180,28 @@ static void writeSortedTags (
 				failedSort (mio, NULL);
 	}
 	if (toStdout)
-		fflush (mio_file_get_fp (mio));
+		mio_flush (mio);
 	mio_free (mio);
 }
 
-extern void internalSortTags (const bool toStdout)
+extern void internalSortTags (const bool toStdout, MIO* mio, size_t numTags)
 {
 	vString *vLine = vStringNew ();
-	MIO *mio = NULL;
 	const char *line;
 	size_t i;
+	int (*cmpFunc)(const void *, const void *);
 
 	/*  Allocate a table of line pointers to be sorted.
 	 */
-	size_t numTags = TagFile.numTags.added + TagFile.numTags.prev;
 	const size_t tableSize = numTags * sizeof (char *);
-	char **const table = (char **) malloc (tableSize);    /* line pointers */
-	DebugStatement ( size_t mallocSize = tableSize; )   /* cumulative total */
+	char **const table = (char **) malloc (tableSize);  /* line pointers */
+	DebugStatement ( size_t mallocSize = tableSize; )  /* cumulative total */
 
+
+	cmpFunc = Option.sorted == SO_FOLDSORTED ? compareTagsFolded : compareTags;
 	if (table == NULL)
 		failedSort (mio, "out of memory");
 
-	/*  Open the tag file and place its lines into allocated buffers.
-	 */
-	mio = mio_new_file (tagFileName (), "r");
-	if (mio == NULL)
-		failedSort (mio, NULL);
 	for (i = 0  ;  i < numTags  &&  ! mio_eof (mio)  ;  )
 	{
 		line = readLineRaw (vLine, mio);
@@ -198,12 +226,11 @@ extern void internalSortTags (const bool toStdout)
 		}
 	}
 	numTags = i;
-	mio_free (mio);
 	vStringDelete (vLine);
 
 	/*  Sort the lines.
 	 */
-	qsort (table, numTags, sizeof (*table), compareTags);
+	qsort (table, numTags, sizeof (*table), cmpFunc);
 
 	writeSortedTags (table, numTags, toStdout);
 


Modified: ctags/main/sort.h
15 lines changed, 12 insertions(+), 3 deletions(-)
===================================================================
@@ -14,15 +14,24 @@
 */
 #include "general.h"  /* must always come first */
 
+#include <stdio.h>
+
+#include "mio.h"
+
 /*
 *   FUNCTION PROTOTYPES
 */
-extern void catFile (const char *const name);
+extern void catFile (MIO *mio);
 
 #ifdef EXTERNAL_SORT
-extern void externalSortTags (const bool toStdout);
+extern void externalSortTags (const bool toStdout, MIO *tagFile);
 #else
-extern void internalSortTags (const bool toStdout);
+extern void internalSortTags (const bool toStdout,
+			      MIO *mio,
+			      size_t numTags);
 #endif
 
+/* mio is closed in this function. */
+extern void failedSort (MIO *const mio, const char* msg);
+
 #endif  /* CTAGS_MAIN_SORT_H */



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list