File: sort.c

package info (click to toggle)
xspread 3.1.1c-9
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,836 kB
  • ctags: 1,426
  • sloc: ansic: 19,342; yacc: 499; lisp: 231; makefile: 198; sh: 28; sed: 4
file content (542 lines) | stat: -rw-r--r-- 13,682 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
 * Copyright (C) 1992  Board of Regents of the University of Wisconsin
 * on behalf of the Department of Electrical Engineering and Computer
 * Science, University of Wisconsin-Milwaukee, Milwaukee, WI 53201.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * a copy of which is included here in file "GNU_GENERAL"
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * The programs in this directory were developed by software engineering
 * teams as part of the course "Introduction to Software Engineering"
 * under the supervision of Professor G. Davida.
 * This is a modification of a program written or modified by
 * others.  The original copyrights, as per GNU General Public License,
 * may still be applicable.  The UWM copyright is applicable only
 * the those parts generated at UWM.
 *
 * Please send all changes, enhancements, and other comments about this
 * software to
 *              soft-eng@cs.uwm.edu
 *
 * No Warranty, expressed or implied, comes with this software.
 * This software is intended to be used by not-for-profit
 * organizations or by individuals for personal HOME use.
 * This software, or any of its parts, may not be used by for-profit
 * organization, regardless of application or intended product or
 * customer, without the permission of the Board of Regents of the
 * University  of Wisconsin.
 *
 * Contact:     soft-eng@cs.uwm.edu
 *                      or
 *
 *              Software Engineering Coordinator
 *              Computer Science
 *              Department of EECS
 *              University of Wisconsin - Milwaukee
 *              Milwaukee, WI  53201
 *              414-229-4677
 *
 *              HISTORY,CLAIMS and CONTRIBUTIONS
 */
/*--------------author RAMA DEVI PUVVADA ------------------------------------*/
#include <config.h>

#include <curses.h>

#ifdef HAVE_X11_X_H
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "scXstuff.h"
#endif /* HAVE_X11_X_H */

#include "sc.h"
#include <signal.h>
#include <errno.h>

#define CLIM 3
#define RLIM 4

int sort_order = 1, sort_type = 1;
int sort_row, sort_col;

static void	get_col PROTO((void));
static void	get_row PROTO((void));
static void	sort_reset PROTO((void));
static void	sort_id PROTO((void));
static void	sort_rc PROTO((void));
static void	sort_col_label PROTO((void));
static void	sort_col_value PROTO((void));
static void	sort_row_label PROTO((void));
static void	sort_row_value PROTO((void));
static void	sort_vl PROTO((void));
static void	swap_col PROTO((int, int));
static void	swap_row PROTO((int, int));


void
Sort_Menu()
{
static char *mainmenu[] = {
                      "Column/Row",
                      "Dec/Inc",
                      "Label/Value",
                      "Reset"
                      };

static char *help[] = {
                   "Sort the table byrows/bycolumns",
                   "Set the Increasing/Decreasing order option",
                   "Set the value/label type option",
                   "Reset the sort options to Increasing & Value"
                             };
/* auto. */  unsigned int  choice=0;

sort_reset();
    do{ /* do until ESC is selected */
      choice = menu(4, mainmenu, help);
      switch(choice) {
                case 0: Main_Menu();
                        break;
                case 1: sort_rc();
                        break;
                case 2: sort_id();
                        break;
                case 3: sort_vl();
                        break;
                case 4: sort_reset();
                        break;
                     }
      if (choice == 1) break;
    } while (choice != 0);  

}

/*--------*/

static void
sort_reset()
{
sort_order = 1; /* increasing order */
sort_type = 1; /* by value */
}

/*------*/
static void
sort_id()
{
static char *item[] = {
                      "Decreasing",
                      "Increasing"
                      };

static char *help_prompt[] = {
                           "Sort in decreasing order",
                           "Sort in increasing order"
                             };

switch (menu(2, item, help_prompt)) {

        case 1: /* Decreasing */
                sort_order = 0;
                break;
        case 2: /* Increasing */
                sort_order = 1;
                break;
        }

}
/*--------*/

static void
sort_vl()
{
static char *item[] = {
                      "Label",
                      "Value"
                      };

static char *help_prompt[] = {
                           "Sort using label",
                           "Sort using value"
                             };

switch (menu(2, item, help_prompt)) {

        case 1: /* label */
                sort_type = 0;
                break;

        case 2: /* value */
                sort_type = 1;
                break;

        }

}

/*--------*/

static void
sort_rc()
{
int temprow,tempcol;

static char *item[] = {
                      "Row",
                      "Column"
                      };

static char *help_prompt[] = {
                           "Sort the table by rows",
                           "Sort the table by column"
                             };

temprow = currow;
tempcol = curcol;
switch (menu(2, item, help_prompt)) {

      case 1:
             get_row();
             switch (sort_type) {
                    case 0: sort_row_label();
                            break;
                    case 1: sort_row_value();
                            break;
                    }
             break;
      case 2:
             get_col();
             switch (sort_type) {
                    case 0: sort_col_label();
                            break;
                    case 1: sort_col_value();
                            break;
                    }
             break;
      }
currow = temprow;
curcol = tempcol;
}

/*----------------------*/
static void
sort_row_label()
{
int i,j;
int mm_index;
char* minmax;

for (i = 0; i <= maxcol; i++)
  {
  register struct ent *p = *ATBL(tbl,sort_row,i);
  if (!p || p->flags&is_valid || !(p->flags&is_label)) continue;

  minmax = p->label;
  mm_index = i;
  
  for (j = i; j <= maxcol; j++)
      {
      register struct ent *q = *ATBL(tbl,sort_row,j);
      if (!q || q->flags&is_valid || !(q->flags&is_label)) continue;
      switch (sort_order) {
          case 0: /* decreasing */
                  if (strcmp(q->label, minmax) > 0) 
                      {
                      mm_index = j;
                      minmax = q->label;
                      }
                  break;
          case 1: /* increasing */
                  if (strcmp(q->label, minmax) < 0)
                      {
                      mm_index = j;
                      minmax = q->label;
                      }

                  break;
          }
      }
      if (i != mm_index)
	swap_col(i, mm_index);
   }
FullUpdate++;
modflg++;
}

/*----------------------*/
static void
sort_row_value()
{
int i,j;
int mm_index;
double minmax;

for (i = 0; i <= maxcol; i++)
  {
  register struct ent *p = *ATBL(tbl,sort_row,i);
  if (!p || !(p->flags&is_valid) || p->flags&is_label) continue;

  minmax = p->v;
  mm_index = i;
  
  for (j = i; j <= maxcol; j++)
      {
      register struct ent *q = *ATBL(tbl,sort_row,j);
      if (!q || !(q->flags&is_valid) || q->flags&is_label) continue;
      switch (sort_order) {
          case 0: /* decreasing */
                  if (q->v > minmax) 
                      {
                      mm_index = j;
                      minmax = q->v;
                      }
                  break;
          case 1: /* increasing */
                  if (q->v < minmax)
                      {
                      mm_index = j;
                      minmax = q->v;
                      }
                  break;
          }
      }
   if (i != mm_index) swap_col(i, mm_index);
   }
FullUpdate++;
modflg++;
}
/*----------------------*/
static void
sort_col_label()
{
int i,j;
int mm_index;
char* minmax;

for (i = 0; i <= maxrow; i++)
  {
  register struct ent *p = *ATBL(tbl,i,sort_col);
  if (!p || p->flags&is_valid || !(p->flags&is_label)) continue;

  minmax = p->label;
  mm_index = i;
  
  for (j = i; j <= maxrow; j++)
      {
      register struct ent *q = *ATBL(tbl,j,sort_col);
      if (!q || q->flags&is_valid || !(q->flags&is_label)) continue;
      switch (sort_order) {
          case 0: /* decreasing */
                  if (strcmp(q->label, minmax) > 0) 
                      {
                      mm_index = j;
                      minmax = q->label;
                      }
                  break;
          case 1: /* increasing */
                  if (strcmp(q->label, minmax) < 0)
                      {
                      mm_index = j;
                      minmax = q->label;
                      }

                  break;
          }
      }
   if (i != mm_index) swap_row(i, mm_index);
   }
FullUpdate++;
modflg++;
}
/*----------------------*/
static void
sort_col_value()
{
int i,j;
int mm_index;
double minmax;

for (i = 0; i <= maxrow; i++)
  {
  register struct ent *p = *ATBL(tbl,i,sort_col);
  if (!p || !(p->flags&is_valid) || p->flags&is_label) continue;

  minmax = p->v;
  mm_index = i;
  
  for (j = i; j <= maxrow; j++)
      {
      register struct ent *q = *ATBL(tbl,j,sort_col);
      if (!q || !(q->flags&is_valid) || q->flags&is_label) continue;
      switch (sort_order) {
          case 0: /* decreasing */
                  if (q->v > minmax) 
                      {
                      mm_index = j;
                      minmax = q->v;
                      }
                  break;
          case 1: /* increasing */
                  if (q->v < minmax)
                      {
                      mm_index = j;
                      minmax = q->v;
                      }

                  break;
          }
      }
   if (i != mm_index) swap_row(i, mm_index);
   }
FullUpdate++;
modflg++;

}
/*----------------------*/

static void
swap_col(a,b)
int a, b;
{
int c;

curcol = a;
dupcol();
erase_area(0,a,maxrow,a);
for (c = 0; c <= maxrow; c++)
    {
    register struct ent *p = *ATBL(tbl, c, b+1);
    if (p) {
           register struct ent *n;
           n = lookat (c, a);
           (void) clearent(n);
           (void) copyent (n, p, 1, 0);
           }
    }

for (c = 0; c <= maxrow; c++)
    {
    register struct ent *p = *ATBL(tbl, c, a+1);
    if (p) {
           register struct ent *n;
           n = lookat (c, b+1);
           (void) clearent(n);
           (void) copyent (n, p, 1, 0);
           }
    }
curcol = a+1;
closecol(curcol,1);
}

/*----------------------*/
static void
swap_row(a,b)
int a, b;
{
int c;

currow = a;
duprow();
erase_area(a,0,a,maxcol);
for (c = 0; c <= maxcol; c++)
    {
    register struct ent *p = *ATBL(tbl, b+1, c);
    if (p) {
           register struct ent *n;
           n = lookat (a, c);
           (void) clearent(n);
           (void) copyent (n, p, 1, 0);
           }
    }

for (c = 0; c <= maxcol; c++)
    {
    register struct ent *p = *ATBL(tbl, a+1, c);
    if (p) {
           register struct ent *n;
           n = lookat (b+1, c);
           (void) clearent(n);
           (void) copyent (n, p, 1, 0);
           }
    }
currow = a+1;
deleterow(1);
}

/*----------------------*/
static void
get_col()
{
  char s[100];   /* stored number to be converted */
  char msg[100]; /* message for display */
  int  ierr;     /* 1 = No error on input */
  int  colm;     /* column number from label entered */
  int  i;        /* miscellaneous index */

do {
   ierr = 1;
   sprintf(s, "Input column label-- 2 character maximum:");
   get_str(s, CLIM);

  /* convert label in "base 26" to column number */
          /* set ierr=0 if an error occurs */
          for(i=colm=0; (i<CLIM) && (s[i] != '\0') && ierr ; i++){
            if ( !isalpha(s[i]) && !isspace(s[i])) {
              ierr = 0;
              continue;
            }
            if (isspace(s[i])) continue;
            if ( islower(s[i]))
              s[i] = toupper(s[i]);
            colm = (colm * 26) + (s[i] - 'A' + 1);
          }
          if (colm == 0)
            ierr = 0;  /* input was all spaces */
          else
            colm--;    /* adjust to zero-based system */

          if (ierr == 0 )
            {
             sprintf(msg,"Input must be character(s)");
             (void) message(msg);
            }
         } while ( ierr == 0 );

sort_col = colm;

}

/*----------------------*/
static void
get_row()
{
  char s[100];   /* stored number to be converted */
  char msg[100]; /* message for display */
  int  ierr;     /* 1 = No error on input */

      do {  
         clearlines(0,0);  /* clear from row 0 to row 0 */
         ierr = 0;
         sprintf(s,"Input row number:");
         sort_row = atoi( get_str(s,RLIM) );
         if ( !isdigit(s[0]) || (sort_row > 200) )
           {
            sprintf(msg,"Input error !");
            (void) message(msg);
            ierr = 1;
           } /* end if */
         } while ( ierr  == 1 );  /* end do */
}