/* C Program Example This program demonstrates the use of the STABLE option with 2 ascending text keys to sort a file of names. The names are sorted by the first 6 characters of the last name and the first 6 characters of the first name. The contents of the input file and resulting output file are listed below. The associated C program code listing follows. ................................................................... Input file: example.in JONES DAVID WARNER LIZZY SMITTS JAMES SMITH RANDY BROWN TONY GRANT JOSEPH BROWN JAMES JONES DAVID BAKER PAMELA SMART SHERYL RUSSO JOSEPH JONES DONALD BROWN GORDON ................................................................... Output file: example.out BAKER PAMELA BROWN GORDON BROWN JAMES BROWN TONY GRANT JOSEPH JONES DAVID JONES DAVID JONES DONALD RUSSO JOSEPH SMART SHERYL SMITH RANDY SMITTS JAMES WARNER LIZZY ................................................................... */ /* **================================================================= ** ** EXAMPLE.C code: ** ** Abstract: Example of using sort with the STABLE option and ** 2 text keys (both ascending). ** ** ** Input file: example.in ** Output file: example.out ** **================================================================= */ /* ---------------------------------------------------------------------------- ** Include files: */ # include # include # include # include # include # include /* ---------------------------------------------------------------------------- ** Local macro definitions: */ # define MAX_REC_LEN 150 # define MAX_NUM_KEYS 10 /* ---------------------------------------------------------------------------- ** Local structure definitions. */ /* Define the description for each key. */ typedef struct { unsigned short type; /* Data type of key */ unsigned short order; /* Order of key */ unsigned short offset; /* Offset of key */ unsigned short len; /* Length of key */ } key_info; struct { unsigned short num; /* number of keys */ key_info key[MAX_NUM_KEYS]; } key_buffer; /* ---------------------------------------------------------------------------- ** External literals. */ globalvalue int SOR$M_STABLE; /* ---------------------------------------------------------------------------- ** Main entry point. */ main (int argc, char *argv[]) { int i; unsigned int options; /* Sort options */ unsigned int num_records_in; unsigned int num_records_out; unsigned int lrl; /* longest record length */ unsigned short size; /* record size from return_rec */ unsigned int status; unsigned long int return_status; FILE *infile; /* input file */ FILE *outfile; /* output file */ char record [MAX_REC_LEN]; $DESCRIPTOR (record_desc, record); lrl = sizeof(record); key_buffer.num = 2; key_buffer.key[0].type = DSC$K_DTYPE_T; key_buffer.key[0].order = 0; /* ascending */ key_buffer.key[0].offset = 0; key_buffer.key[0].len = 6; key_buffer.key[1].type = DSC$K_DTYPE_T; key_buffer.key[1].order = 0; /* ascending */ key_buffer.key[1].offset = 7; key_buffer.key[1].len = 6; /* Open input and output files. */ if (argc != 3) { printf("Usage: example inputfile outputfile\n"); exit(-1); } infile = fopen(argv[1], "r"); if (infile == (FILE *) NULL) { printf("Can't open input file %s\n",argv[1]); exit(-1); } outfile = fopen(argv[2], "w"); if (outfile == (FILE *) NULL) { printf("Can't create output file %s\n",argv[2]); exit(-1); } /* Specify options. Initialize the sort and check for errors. */ options = SOR$M_STABLE; return_status = SOR$BEGIN_SORT(&key_buffer, &lrl, &options, 0,0,0,0,0,0); if (return_status != SS$_NORMAL) { printf ("Status from SOR$BEGIN_SORT: 0x%x\n", return_status); exit(return_status); } /* Within a loop, get all the records from the input file. */ /* Exit if an error occurs. */ num_records_in = 0; while (fgets( record, lrl, infile) != NULL) { record_desc.dsc$w_length = strlen(record)-1; num_records_in++; return_status = SOR$RELEASE_REC(&record_desc,0); if (return_status != SS$_NORMAL) { printf ("Status from SOR$RELEASE_REC: 0x%x\n", return_status); exit(return_status); } } /* Sort all of the input records. */ /* Exit if an error occurs. */ return_status = SOR$SORT_MERGE(0); if (return_status != SS$_NORMAL) { printf ("Status from SOR$SORT_MERGE: 0x%x\n", return_status); exit(return_status); } /* Within a loop, write the sorted records to the output file. */ /* Exit if an error occurs, other than end-of-file. */ record_desc.dsc$w_length = lrl; num_records_out = 0; do { return_status = SOR$RETURN_REC(&record_desc,&size,0); if (return_status == SS$_NORMAL) { num_records_out++; status = fprintf (outfile,"%.*s\n", size, record); if (status < 0 ) { printf ("Error writing to output file, status = %d\n", status); exit(status); } } else if (return_status != SS$_ENDOFFILE) { printf ("Status from SOR$RETURN_REC: 0x%x\n", return_status); exit(return_status); }; } while (return_status != SS$_ENDOFFILE); /* Sanity check - assure number of input and output records match. */ if (num_records_out != num_records_in) { printf("Number of records out is not correct. # in = %d, # out = %d\n", num_records_out, num_records_in); exit(status); } /* Successful completion. Close input and output files. End program. */ return_status = SOR$END_SORT(0); if (return_status != SS$_NORMAL) { printf ("Status from SOR$END_SORT: 0x%x\n", return_status); exit(return_status); } fclose (infile); fclose (outfile); }