Category : Word Processors
Archive   : HP22D5.ZIP
Filename : CSORT.C

 
Output of file : CSORT.C contained in archive : HP22D5.ZIP
/*
** CSORT.C - container sorting.
**
** csort() is a function that sorts the lines of a container
** alphabetically in acsending order.
**
*/
#include "extern.h" /* Extensions need these! */

/*
** This routine sorts lines of text.
**
** To call this function from HPAD:
**
** put csort(page field 1) into page field 1;
**
** There is an optional second argument that determines the end
** of a sort item (a carriage-return by default).
**
** put csort(page field 1,",") into page field 2;
*/
csort(int NumArgs,HANDLE hText,HANDLE hDelimiter)

{
HANDLE newhdl;
HANDLE *ha;
PTR ptr,p;
int i,j;
int count;
int delimiter = '\r';

/* check for bad number of args */
if (NumArgs < 1) return(STOP);

if (NumArgs == 2) delimiter = **hDelimiter;

p = deref(hText);

/* count the number of lines in the source, put end-of-line markers */
for (count=0;*p;p++) {
if (*p == delimiter) {
*p++ = '\0';
count++;
}
}

/* return if there are no things to sort */
if (!count) {
int sz;

/* return a duplicate of the original string */
sz = HandleSize(hText);
newhdl = NewHandle(sz);
memcpy(deref(newhdl),deref(hText),sz);
goto end;
}

/* 1 based */
count++;

/* handler to array of pointers */
ha = (HANDLE *)NewHandle(count * sizeof(PTR));

/* handle to new memory */
newhdl = NewHandle((WORD)HandleSize(hText));

/* check for out of memory */
if (!ha || !newhdl) {
FreeHandle((HANDLE)ha);
FreeHandle(newhdl);
return(STOP);
}

/* get pointers to strings. */
for (i=0,p=deref(hText);i deref(ha)[i] = p;
p += strlen(p) + 1;
}

/* brute-force the sort of the pointers */
for (i=0;i for (j=i;j if (strcmpi(deref(ha)[i],deref(ha)[j]) > 0) {
p = deref(ha)[i];
deref(ha)[i] = deref(ha)[j];
deref(ha)[j] = p;
}
}
}

/* copy the strings back into original array */
p = deref(newhdl);
for (i=0;i strcpy(p,deref(ha)[i]);
p += strlen(p);
if (i + 1 < count) *p++ = delimiter;
}
*p = '\0';

/* free the temporary array buffer */
FreeHandle((HANDLE)ha);
end:
ReturnValue(newhdl);
return(STOP);
}

POOL pascal Pool[] = {
{ "csort",
csort,
0,
FUNCTION},

{ NULL, /* NULLs signify the end of the table */
NULL,
0,
0} };



  3 Responses to “Category : Word Processors
Archive   : HP22D5.ZIP
Filename : CSORT.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/