TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Paul Edwards
from: Joshua Marshall
date: 1996-12-09 17:56:14
subject: Sorting program

Here's a sorting program I had to write when I needed to sort rows of data
by information a few columns from the left edge.

You can stick it in OZPD if you like.  The sort routine is just a simple
one - if anyone wants to improve on it be my guest!

/*****************************************************************************/
/*                                                                         
 */ /* This program Written by Joshua Marshall                             
     */ /*                                                                 
         */
/*****************************************************************************/

/*****************************************************************************/
/*                                                                         
 */ /*                                                                     
     */ /* Sort - sort a text file on a certain column and datatype        
         */ /*                                                             
             */ /*                                                         
                 */ /* This program is released to the public domain.      
                     */ /*                                                 
                         */ /* Written December 1996.                      
                             */ /*                                         
                                 */
/*****************************************************************************/

/*****************************************************************************/
/* Local includes                                                          
 */
/*****************************************************************************/

#include 
#include 
#include 
#include 

/*****************************************************************************/
/* Local defines                                                           
 */
/*****************************************************************************/

#define ALPHA_TYPE   1
#define NUMBER_TYPE  2

#define UP_TYPE      0
#define DOWN_TYPE    1

/*****************************************************************************/
/* Local structures                                                        
 */
/*****************************************************************************/

typedef struct LINE
{
  char *text;
  struct LINE *nextline;
} line;

/*****************************************************************************/
/* Global variables                                                        
 */
/*****************************************************************************/

char *filein=NULL;
char *fileout=NULL;
int field=0;
int datatype=0;
int direction=0;
line firstline;
int numlines=0;

/*****************************************************************************/
/* Local variables                                                         
 */
/*****************************************************************************/

/*****************************************************************************/
/* Local functions                                                         
 */
/*****************************************************************************/

static int compare(char *s1, char *s2)
{
  char *tmp1;
  char *tmp2;
  char *tok1;
  char *tok2;
  int i;

  if (field>0)
  {
    tmp1=(char *)(malloc(strlen(s1)+1));
    tmp2=(char *)(malloc(strlen(s2)+1));

    strcpy(tmp1,s1);
    strcpy(tmp2,s2);
    tok1=strtok(tmp1," ");
    for (i=1;ii2)
      return 1;
    return 0;
  }

  return strcmp(tok1,tok2);
}

static void read_list(void)
{
  FILE *in;

  line *list;
  line *lastlist=&firstline;
  char s[500];

  in=fopen(filein,"r");

  if (in)
  {
    while (fgets(s,500,in))
    {
      list=(line *)(malloc(sizeof(line)));
      if (list)
      {
        lastlist->nextline=list;
        list->text=(char *)(malloc(strlen(s)+1));
        if (list->text)
        {
          numlines++;
          strcpy(list->text,s);
        }
        list->nextline=NULL;
        lastlist=list;
      }
    }
    fclose(in);
  }
}

static void sort_list(void)
{
  struct LINE *t1;
  struct LINE *t2;
  long max;
  long tmp;

  t1=&firstline;

  while(t1->nextline)
  {
    t2=t1->nextline;
    max=(long)(t1->nextline);

#ifdef DEBUG
    printf("."); fflush(stdout);
#endif

    while(t2->nextline)
    {
      if ( ((direction==DOWN_TYPE)&&(compare(((struct LINE
*)(max))->text, (t2->nextline)->text)<0))
         ||((direction==UP_TYPE  )&&(compare(((struct LINE
*)(max))->text, (t2->nextline)->text)>0)))
      {
        max=(long)(t2->nextline);
      }
      t2=t2->nextline;
    }

    tmp=(long)(((struct LINE *)(max))->text);
    ((struct LINE *)(max))->text=(t1->nextline)->text;
    (t1->nextline)->text=(char *)(tmp);

    t1=t1->nextline;
  }
}

static void write_list(void)
{
  FILE *out;
  line *list;
  line *tmp;

  out=fopen(fileout,"w");

  if (out)
  {
    list=firstline.nextline;
    while (list)
    {
      fprintf(out,"%s", list->text);
      free(list->text);
      tmp=list;
      list=list->nextline;
      free(tmp);
    }
    fclose(out);
  }
}

/*****************************************************************************/
/* Global functions                                                        
 */
/*****************************************************************************/

int main(int argc, char **argv)
{
  if (argc==1)
  {
    printf("Sort by Joshua Marshall, compiled"__DATE__"\n\n");
    printf("This program released to the public domain\n");
    printf("Usage: sort     \n");
    printf("Where field is the column number (default 0 - whole line)\n");
    printf("And datatype is\n");
    printf("  ALPHA (default)  - alphanumeric\n");
    printf("  NUMBER           - numeric\n");
    printf("And direction is\n");
    printf("  UP (default)     - smallest at the top\n");
    printf("  DOWN             - largest at the top\n");
  }

  if (argc>0)
    filein=argv[1];
  if (argc>1)
    fileout=argv[2];
  if (argc>2)
  {
    int i=2;
    
    while (i++MEMBER<-     Internet  joshua{at}st.net.au
  Team Amiga     Amiganet  41:400/869.14

... The die is cast.
--- Mail Manager 1.21x/p #1219
* Origin: Borderline Technologies (3:640/201.14{at}fidonet)
SEEN-BY: 633/267 270
@PATH: 640/201 201 820 711/409 808 50/99 635/728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.