1 
2 static char rcsid[] = "@(#)$Id: a_sort.c,v 1.3 1996/03/14 17:27:49 wfp5p Exp $";
3 
4 /*******************************************************************************
5  *  The Elm Mail System  -  $Revision: 1.3 $   $State: Exp $
6  *
7  *                      Copyright (c) 1988-1995 USENET Community Trust
8  * 			Copyright (c) 1986,1987 Dave Taylor
9  *******************************************************************************
10  * Bug reports, patches, comments, suggestions should be sent to:
11  *
12  *      Bill Pemberton, Elm Coordinator
13  *      flash@virginia.edu
14  *
15  *******************************************************************************
16  * $Log: a_sort.c,v $
17  * Revision 1.3  1996/03/14  17:27:49  wfp5p
18  * Alpha 9
19  *
20  * Revision 1.2  1995/09/29  17:41:56  wfp5p
21  * Alpha 8 (Chip's big changes)
22  *
23  * Revision 1.1.1.1  1995/04/19  20:38:34  wfp5p
24  * Initial import of elm 2.4 PL0 as base for elm 2.5.
25  *
26  ******************************************************************************/
27 
28 /** Sort alias table by the field specified in the global
29     variable "alias_sortby"...
30 
31 **/
32 
33 #include "elm_defs.h"
34 #include "elm_globals.h"
35 #include "s_aliases.h"
36 
37 char *alias_sort_name(int longname);
38 static void alias_old_current(long iindex);
39 
sort_aliases(entries,visible,are_in_aliases)40 sort_aliases(entries, visible, are_in_aliases)
41 int entries, visible, are_in_aliases;
42 {
43 	/** Sort the header_table definitions... If 'visible', then
44 	    put the status lines etc **/
45 
46 	long last_index = -1;
47 	int compare_aliases();	/* for sorting */
48 
49 	dprint(2, (debugfile, "\n** sorting aliases by %s **\n\n",
50 		alias_sort_name(TRUE)));
51 
52 	/* Don't get last_index if no entries or no current. */
53 	/* There would be no current if we are sorting for the first time. */
54 	if (entries > 0 && curr_alias > 0)
55 	  last_index = aliases[curr_alias-1]->length;
56 
57 	if ((entries > 30) && visible && are_in_aliases) {
58 	    error1(catgets(elm_msg_cat, AliasesSet, AliasesSort,
59 		    "Sorting aliases by %s..."), alias_sort_name(TRUE));
60 	}
61 
62 	if (entries > 1)
63 	  qsort((char *) aliases, (unsigned) entries,
64 	        sizeof (struct alias_rec *), compare_aliases);
65 
66 	if (last_index > -1)
67 	  alias_old_current(last_index);
68 
69 	if (are_in_aliases) {
70 	    clear_error();
71 	}
72 }
73 
74 int
compare_aliases(p1,p2)75 compare_aliases(p1, p2)
76 struct alias_rec **p1, **p2;
77 {
78 	/** compare two aliases according to the sortby value.
79 
80 	    Both are simple strcmp()'s on the alias or last_name
81 	    components of the alias.
82 	 **/
83 
84 	register struct alias_rec *first, *second;
85 	register int ret;
86 	register long diff;
87 
88 	first = *p1;
89 	second = *p2;
90 
91 	/* If (only) one of the compares is a duplicate we want it
92 	 * to go to the end of the list regardless of the sorting
93 	 * method.
94 	 */
95 	if ((first->type ^ second->type) & DUPLICATE) {
96 	    if (first->type & DUPLICATE)
97 	        ret = 1;
98 	    else			/* It must be second... */
99 	        ret = -1;
100 	    return ret;
101 	}
102 
103 	switch (abs(alias_sortby)) {
104 	case ALIAS_SORT:
105 		ret = strcmp(first->alias, second->alias);
106 		break;
107 
108 	case NAME_SORT:
109 		ret = strcmp(first->last_name, second->last_name);
110 	     /*
111 	      * If equal on last name then compare on first name
112 	      * which is the first part of 'name'.
113 	      */
114 		if (ret == 0) {
115 		    ret = strcmp(first->name, second->name);
116 		}
117 		break;
118 
119 	case TEXT_SORT:
120 		diff = (first->length - second->length);
121  		if ( diff < 0 )	ret = -1;
122  		else if ( diff > 0 ) ret = 1;
123  		else ret = 0;
124 		break;
125 
126 	default:
127 		/* never get this! */
128 		ret = 0;
129 		break;
130 	}
131 
132 	if (alias_sortby < 0)
133 	  ret = -ret;
134 
135 	return ret;
136 }
137 
alias_sort_name(longname)138 char *alias_sort_name(longname)
139 int longname;
140 {
141 
142     if (alias_sortby < 0) {
143 	switch (-alias_sortby) {
144 	case ALIAS_SORT: return (longname
145 		? catgets(elm_msg_cat, AliasesSet, AliasesRevAliasName,
146 			"Reverse Alias Name")
147 		: catgets(elm_msg_cat, AliasesSet, AliasesRevAliasAbr,
148 			"Reverse-Alias"));
149 	case NAME_SORT: return (longname
150 		? catgets(elm_msg_cat, AliasesSet, AliasesRevFullName,
151 			"Reverse Full (Real) Name")
152 		: catgets(elm_msg_cat, AliasesSet, AliasesRevFullAbr,
153 			"Reverse-Name"));
154 	case TEXT_SORT: return (longname
155 		? catgets(elm_msg_cat, AliasesSet, AliasesRevTextFile,
156 			"Reverse Text File")
157 		: catgets(elm_msg_cat, AliasesSet, AliasesRevTextAbr,
158 			"Reverse-Text"));
159 	}
160     } else {
161 	switch (alias_sortby) {
162 	case ALIAS_SORT: return (longname
163 		? catgets(elm_msg_cat, AliasesSet, AliasesAliasName,
164 			"Alias Name")
165 		: catgets(elm_msg_cat, AliasesSet, AliasesAliasAbr,
166 			"Alias"));
167 	case NAME_SORT: return (longname
168 		? catgets(elm_msg_cat, AliasesSet, AliasesFullName,
169 			"Full (Real) Name")
170 		: catgets(elm_msg_cat, AliasesSet, AliasesFullAbr,
171 			"Name"));
172 	case TEXT_SORT: return (longname
173 		? catgets(elm_msg_cat, AliasesSet, AliasesTextFile,
174 			"Text File")
175 		: catgets(elm_msg_cat, AliasesSet, AliasesTextAbr,
176 			"Text"));
177 	}
178     }
179 
180     return("*UNKNOWN-SORT-PARAMETER*");
181 }
182 
183 static void
alias_old_current(iindex)184 alias_old_current(iindex)
185 long iindex;
186 {
187 	/** Set current to the message that has "index" as it's
188 	    index number.  This is to track the current message
189 	    when we resync... **/
190 
191 	register int i;
192 
193 	dprint(4, (debugfile, "alias-old-current(%d)\n", iindex));
194 
195 	for (i = 0; i < num_aliases; i++)
196 	  if (aliases[i]->length == iindex) {
197 	    curr_alias = i+1;
198 	    dprint(4, (debugfile, "\tset curr_alias to %d!\n", curr_alias));
199 	    return;
200 	  }
201 
202 	dprint(4, (debugfile,
203 		"\tcouldn't find current index.  curr_alias left as %d\n",
204 		curr_alias));
205 	return;		/* can't be found.  Leave it alone, then */
206 }
207