Home » Archimedes archive » Acorn User » AU 1998-09.adf » Regulars » StarInfo/Kingsley/TestLink_c

StarInfo/Kingsley/TestLink_c

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Acorn User » AU 1998-09.adf » Regulars
Filename: StarInfo/Kingsley/TestLink_c
Read OK:
File size: 089A bytes
Load address: 0000
Exec address: 0000
File contents
#include <stdio.h>
#include <roslib.h>
#include <kernel.h>
#include <Types.h>
#include <LinkList.h>
#include <swis.h>
#include <stdlib.h>
#include <string.h>

/*
A very simple test program for the LinkList module
*/

#ifdef __cplusplus
extern "C" {
#endif

extern int sortRoutine(LONG n1,LONG n2,LONG root);

#ifdef __cplusplus
} ;
#endif

int sortRoutine(LONG n1,LONG n2,LONG root)
{
char bufferA[128];
char bufferB[128];

    getUserData(root,n1,0,sizeof(bufferA),(char *) &bufferA,GET_DATA);
    getUserData(root,n2,0,sizeof(bufferB),(char *) &bufferB,GET_DATA);
    
    return ((bufferA[4]==bufferB[4] ? 0 : \
    	     bufferA[4]>bufferB[4] ? 1 : -1)); /* Swap around for reverse ordering */
    	     
}
  
int main(void)
{
LONG root=LNULL;
LONG node=LNULL;
char text[128];

    printf("\nSimple Link List Program");
    printf("\n------------------------\n\n");  
    
    printf("\nCreating Link List");
    
    if ((root=createList(DYNAMIC,sizeof(__ROOT)))==LNULL)
    {
      	if ((root=createList(RMA,sizeof(__ROOT)))==LNULL)
      	{
      	    printf("\nCant allocate memory for the Linked List!");
      	    printf("\nPlease increase the RMA or Dynamic Memory size!");
      	    exit(1);
      	}
    }
    
    printf("\n\nPlease keep entering text, until you type in '*' to exit");
    
    do {
      memset((char *) &text,(char) NULL,sizeof(text));
      printf("\nEnter your text : ");
      gets(&text);

      if (addNode(FIRST,root,(char *) &text,DYNAMIC,sizeof(text))==LNULL)
      {
      	if (addNode(FIRST,root,(char *) &text,RMA,sizeof(text))==LNULL)
      	{
      	    printf("\nCant add any more data to the list.  Type in * and press");
      	    printf("\nRETURN to continue");
      	}
      }      
    } while (text[0]!='*');
    
    printf("\nAnd now a status report :");
    getStatus(stdout,root);
    
    printf("\nAnd now we'll sort the complete list based on the 5th character");
    
    if (sortList(root,sortRoutine)==LNULL)
    {
      	printf("\nNothing was sorted!");
    }
    
    printf("\nNow another status report :");
    getStatus(stdout,root);
    
    printf("\nFinally, we'll delete the complete list");
    
    root=deleteList(root);
}
00000000  23 69 6e 63 6c 75 64 65  20 3c 73 74 64 69 6f 2e  |#include <stdio.|
00000010  68 3e 0a 23 69 6e 63 6c  75 64 65 20 3c 72 6f 73  |h>.#include <ros|
00000020  6c 69 62 2e 68 3e 0a 23  69 6e 63 6c 75 64 65 20  |lib.h>.#include |
00000030  3c 6b 65 72 6e 65 6c 2e  68 3e 0a 23 69 6e 63 6c  |<kernel.h>.#incl|
00000040  75 64 65 20 3c 54 79 70  65 73 2e 68 3e 0a 23 69  |ude <Types.h>.#i|
00000050  6e 63 6c 75 64 65 20 3c  4c 69 6e 6b 4c 69 73 74  |nclude <LinkList|
00000060  2e 68 3e 0a 23 69 6e 63  6c 75 64 65 20 3c 73 77  |.h>.#include <sw|
00000070  69 73 2e 68 3e 0a 23 69  6e 63 6c 75 64 65 20 3c  |is.h>.#include <|
00000080  73 74 64 6c 69 62 2e 68  3e 0a 23 69 6e 63 6c 75  |stdlib.h>.#inclu|
00000090  64 65 20 3c 73 74 72 69  6e 67 2e 68 3e 0a 0a 2f  |de <string.h>../|
000000a0  2a 0a 41 20 76 65 72 79  20 73 69 6d 70 6c 65 20  |*.A very simple |
000000b0  74 65 73 74 20 70 72 6f  67 72 61 6d 20 66 6f 72  |test program for|
000000c0  20 74 68 65 20 4c 69 6e  6b 4c 69 73 74 20 6d 6f  | the LinkList mo|
000000d0  64 75 6c 65 0a 2a 2f 0a  0a 23 69 66 64 65 66 20  |dule.*/..#ifdef |
000000e0  5f 5f 63 70 6c 75 73 70  6c 75 73 0a 65 78 74 65  |__cplusplus.exte|
000000f0  72 6e 20 22 43 22 20 7b  0a 23 65 6e 64 69 66 0a  |rn "C" {.#endif.|
00000100  0a 65 78 74 65 72 6e 20  69 6e 74 20 73 6f 72 74  |.extern int sort|
00000110  52 6f 75 74 69 6e 65 28  4c 4f 4e 47 20 6e 31 2c  |Routine(LONG n1,|
00000120  4c 4f 4e 47 20 6e 32 2c  4c 4f 4e 47 20 72 6f 6f  |LONG n2,LONG roo|
00000130  74 29 3b 0a 0a 23 69 66  64 65 66 20 5f 5f 63 70  |t);..#ifdef __cp|
00000140  6c 75 73 70 6c 75 73 0a  7d 20 3b 0a 23 65 6e 64  |lusplus.} ;.#end|
00000150  69 66 0a 0a 69 6e 74 20  73 6f 72 74 52 6f 75 74  |if..int sortRout|
00000160  69 6e 65 28 4c 4f 4e 47  20 6e 31 2c 4c 4f 4e 47  |ine(LONG n1,LONG|
00000170  20 6e 32 2c 4c 4f 4e 47  20 72 6f 6f 74 29 0a 7b  | n2,LONG root).{|
00000180  0a 63 68 61 72 20 62 75  66 66 65 72 41 5b 31 32  |.char bufferA[12|
00000190  38 5d 3b 0a 63 68 61 72  20 62 75 66 66 65 72 42  |8];.char bufferB|
000001a0  5b 31 32 38 5d 3b 0a 0a  20 20 20 20 67 65 74 55  |[128];..    getU|
000001b0  73 65 72 44 61 74 61 28  72 6f 6f 74 2c 6e 31 2c  |serData(root,n1,|
000001c0  30 2c 73 69 7a 65 6f 66  28 62 75 66 66 65 72 41  |0,sizeof(bufferA|
000001d0  29 2c 28 63 68 61 72 20  2a 29 20 26 62 75 66 66  |),(char *) &buff|
000001e0  65 72 41 2c 47 45 54 5f  44 41 54 41 29 3b 0a 20  |erA,GET_DATA);. |
000001f0  20 20 20 67 65 74 55 73  65 72 44 61 74 61 28 72  |   getUserData(r|
00000200  6f 6f 74 2c 6e 32 2c 30  2c 73 69 7a 65 6f 66 28  |oot,n2,0,sizeof(|
00000210  62 75 66 66 65 72 42 29  2c 28 63 68 61 72 20 2a  |bufferB),(char *|
00000220  29 20 26 62 75 66 66 65  72 42 2c 47 45 54 5f 44  |) &bufferB,GET_D|
00000230  41 54 41 29 3b 0a 20 20  20 20 0a 20 20 20 20 72  |ATA);.    .    r|
00000240  65 74 75 72 6e 20 28 28  62 75 66 66 65 72 41 5b  |eturn ((bufferA[|
00000250  34 5d 3d 3d 62 75 66 66  65 72 42 5b 34 5d 20 3f  |4]==bufferB[4] ?|
00000260  20 30 20 3a 20 5c 0a 20  20 20 20 09 20 20 20 20  | 0 : \.    .    |
00000270  20 62 75 66 66 65 72 41  5b 34 5d 3e 62 75 66 66  | bufferA[4]>buff|
00000280  65 72 42 5b 34 5d 20 3f  20 31 20 3a 20 2d 31 29  |erB[4] ? 1 : -1)|
00000290  29 3b 20 2f 2a 20 53 77  61 70 20 61 72 6f 75 6e  |); /* Swap aroun|
000002a0  64 20 66 6f 72 20 72 65  76 65 72 73 65 20 6f 72  |d for reverse or|
000002b0  64 65 72 69 6e 67 20 2a  2f 0a 20 20 20 20 09 20  |dering */.    . |
000002c0  20 20 20 20 0a 7d 0a 20  20 0a 69 6e 74 20 6d 61  |    .}.  .int ma|
000002d0  69 6e 28 76 6f 69 64 29  0a 7b 0a 4c 4f 4e 47 20  |in(void).{.LONG |
000002e0  72 6f 6f 74 3d 4c 4e 55  4c 4c 3b 0a 4c 4f 4e 47  |root=LNULL;.LONG|
000002f0  20 6e 6f 64 65 3d 4c 4e  55 4c 4c 3b 0a 63 68 61  | node=LNULL;.cha|
00000300  72 20 74 65 78 74 5b 31  32 38 5d 3b 0a 0a 20 20  |r text[128];..  |
00000310  20 20 70 72 69 6e 74 66  28 22 5c 6e 53 69 6d 70  |  printf("\nSimp|
00000320  6c 65 20 4c 69 6e 6b 20  4c 69 73 74 20 50 72 6f  |le Link List Pro|
00000330  67 72 61 6d 22 29 3b 0a  20 20 20 20 70 72 69 6e  |gram");.    prin|
00000340  74 66 28 22 5c 6e 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |tf("\n----------|
00000350  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 5c 6e  |--------------\n|
00000360  5c 6e 22 29 3b 20 20 0a  20 20 20 20 0a 20 20 20  |\n");  .    .   |
00000370  20 70 72 69 6e 74 66 28  22 5c 6e 43 72 65 61 74  | printf("\nCreat|
00000380  69 6e 67 20 4c 69 6e 6b  20 4c 69 73 74 22 29 3b  |ing Link List");|
00000390  0a 20 20 20 20 0a 20 20  20 20 69 66 20 28 28 72  |.    .    if ((r|
000003a0  6f 6f 74 3d 63 72 65 61  74 65 4c 69 73 74 28 44  |oot=createList(D|
000003b0  59 4e 41 4d 49 43 2c 73  69 7a 65 6f 66 28 5f 5f  |YNAMIC,sizeof(__|
000003c0  52 4f 4f 54 29 29 29 3d  3d 4c 4e 55 4c 4c 29 0a  |ROOT)))==LNULL).|
000003d0  20 20 20 20 7b 0a 20 20  20 20 20 20 09 69 66 20  |    {.      .if |
000003e0  28 28 72 6f 6f 74 3d 63  72 65 61 74 65 4c 69 73  |((root=createLis|
000003f0  74 28 52 4d 41 2c 73 69  7a 65 6f 66 28 5f 5f 52  |t(RMA,sizeof(__R|
00000400  4f 4f 54 29 29 29 3d 3d  4c 4e 55 4c 4c 29 0a 20  |OOT)))==LNULL). |
00000410  20 20 20 20 20 09 7b 0a  20 20 20 20 20 20 09 20  |     .{.      . |
00000420  20 20 20 70 72 69 6e 74  66 28 22 5c 6e 43 61 6e  |   printf("\nCan|
00000430  74 20 61 6c 6c 6f 63 61  74 65 20 6d 65 6d 6f 72  |t allocate memor|
00000440  79 20 66 6f 72 20 74 68  65 20 4c 69 6e 6b 65 64  |y for the Linked|
00000450  20 4c 69 73 74 21 22 29  3b 0a 20 20 20 20 20 20  | List!");.      |
00000460  09 20 20 20 20 70 72 69  6e 74 66 28 22 5c 6e 50  |.    printf("\nP|
00000470  6c 65 61 73 65 20 69 6e  63 72 65 61 73 65 20 74  |lease increase t|
00000480  68 65 20 52 4d 41 20 6f  72 20 44 79 6e 61 6d 69  |he RMA or Dynami|
00000490  63 20 4d 65 6d 6f 72 79  20 73 69 7a 65 21 22 29  |c Memory size!")|
000004a0  3b 0a 20 20 20 20 20 20  09 20 20 20 20 65 78 69  |;.      .    exi|
000004b0  74 28 31 29 3b 0a 20 20  20 20 20 20 09 7d 0a 20  |t(1);.      .}. |
000004c0  20 20 20 7d 0a 20 20 20  20 0a 20 20 20 20 70 72  |   }.    .    pr|
000004d0  69 6e 74 66 28 22 5c 6e  5c 6e 50 6c 65 61 73 65  |intf("\n\nPlease|
000004e0  20 6b 65 65 70 20 65 6e  74 65 72 69 6e 67 20 74  | keep entering t|
000004f0  65 78 74 2c 20 75 6e 74  69 6c 20 79 6f 75 20 74  |ext, until you t|
00000500  79 70 65 20 69 6e 20 27  2a 27 20 74 6f 20 65 78  |ype in '*' to ex|
00000510  69 74 22 29 3b 0a 20 20  20 20 0a 20 20 20 20 64  |it");.    .    d|
00000520  6f 20 7b 0a 20 20 20 20  20 20 6d 65 6d 73 65 74  |o {.      memset|
00000530  28 28 63 68 61 72 20 2a  29 20 26 74 65 78 74 2c  |((char *) &text,|
00000540  28 63 68 61 72 29 20 4e  55 4c 4c 2c 73 69 7a 65  |(char) NULL,size|
00000550  6f 66 28 74 65 78 74 29  29 3b 0a 20 20 20 20 20  |of(text));.     |
00000560  20 70 72 69 6e 74 66 28  22 5c 6e 45 6e 74 65 72  | printf("\nEnter|
00000570  20 79 6f 75 72 20 74 65  78 74 20 3a 20 22 29 3b  | your text : ");|
00000580  0a 20 20 20 20 20 20 67  65 74 73 28 26 74 65 78  |.      gets(&tex|
00000590  74 29 3b 0a 0a 20 20 20  20 20 20 69 66 20 28 61  |t);..      if (a|
000005a0  64 64 4e 6f 64 65 28 46  49 52 53 54 2c 72 6f 6f  |ddNode(FIRST,roo|
000005b0  74 2c 28 63 68 61 72 20  2a 29 20 26 74 65 78 74  |t,(char *) &text|
000005c0  2c 44 59 4e 41 4d 49 43  2c 73 69 7a 65 6f 66 28  |,DYNAMIC,sizeof(|
000005d0  74 65 78 74 29 29 3d 3d  4c 4e 55 4c 4c 29 0a 20  |text))==LNULL). |
000005e0  20 20 20 20 20 7b 0a 20  20 20 20 20 20 09 69 66  |     {.      .if|
000005f0  20 28 61 64 64 4e 6f 64  65 28 46 49 52 53 54 2c  | (addNode(FIRST,|
00000600  72 6f 6f 74 2c 28 63 68  61 72 20 2a 29 20 26 74  |root,(char *) &t|
00000610  65 78 74 2c 52 4d 41 2c  73 69 7a 65 6f 66 28 74  |ext,RMA,sizeof(t|
00000620  65 78 74 29 29 3d 3d 4c  4e 55 4c 4c 29 0a 20 20  |ext))==LNULL).  |
00000630  20 20 20 20 09 7b 0a 20  20 20 20 20 20 09 20 20  |    .{.      .  |
00000640  20 20 70 72 69 6e 74 66  28 22 5c 6e 43 61 6e 74  |  printf("\nCant|
00000650  20 61 64 64 20 61 6e 79  20 6d 6f 72 65 20 64 61  | add any more da|
00000660  74 61 20 74 6f 20 74 68  65 20 6c 69 73 74 2e 20  |ta to the list. |
00000670  20 54 79 70 65 20 69 6e  20 2a 20 61 6e 64 20 70  | Type in * and p|
00000680  72 65 73 73 22 29 3b 0a  20 20 20 20 20 20 09 20  |ress");.      . |
00000690  20 20 20 70 72 69 6e 74  66 28 22 5c 6e 52 45 54  |   printf("\nRET|
000006a0  55 52 4e 20 74 6f 20 63  6f 6e 74 69 6e 75 65 22  |URN to continue"|
000006b0  29 3b 0a 20 20 20 20 20  20 09 7d 0a 20 20 20 20  |);.      .}.    |
000006c0  20 20 7d 20 20 20 20 20  20 0a 20 20 20 20 7d 20  |  }      .    } |
000006d0  77 68 69 6c 65 20 28 74  65 78 74 5b 30 5d 21 3d  |while (text[0]!=|
000006e0  27 2a 27 29 3b 0a 20 20  20 20 0a 20 20 20 20 70  |'*');.    .    p|
000006f0  72 69 6e 74 66 28 22 5c  6e 41 6e 64 20 6e 6f 77  |rintf("\nAnd now|
00000700  20 61 20 73 74 61 74 75  73 20 72 65 70 6f 72 74  | a status report|
00000710  20 3a 22 29 3b 0a 20 20  20 20 67 65 74 53 74 61  | :");.    getSta|
00000720  74 75 73 28 73 74 64 6f  75 74 2c 72 6f 6f 74 29  |tus(stdout,root)|
00000730  3b 0a 20 20 20 20 0a 20  20 20 20 70 72 69 6e 74  |;.    .    print|
00000740  66 28 22 5c 6e 41 6e 64  20 6e 6f 77 20 77 65 27  |f("\nAnd now we'|
00000750  6c 6c 20 73 6f 72 74 20  74 68 65 20 63 6f 6d 70  |ll sort the comp|
00000760  6c 65 74 65 20 6c 69 73  74 20 62 61 73 65 64 20  |lete list based |
00000770  6f 6e 20 74 68 65 20 35  74 68 20 63 68 61 72 61  |on the 5th chara|
00000780  63 74 65 72 22 29 3b 0a  20 20 20 20 0a 20 20 20  |cter");.    .   |
00000790  20 69 66 20 28 73 6f 72  74 4c 69 73 74 28 72 6f  | if (sortList(ro|
000007a0  6f 74 2c 73 6f 72 74 52  6f 75 74 69 6e 65 29 3d  |ot,sortRoutine)=|
000007b0  3d 4c 4e 55 4c 4c 29 0a  20 20 20 20 7b 0a 20 20  |=LNULL).    {.  |
000007c0  20 20 20 20 09 70 72 69  6e 74 66 28 22 5c 6e 4e  |    .printf("\nN|
000007d0  6f 74 68 69 6e 67 20 77  61 73 20 73 6f 72 74 65  |othing was sorte|
000007e0  64 21 22 29 3b 0a 20 20  20 20 7d 0a 20 20 20 20  |d!");.    }.    |
000007f0  0a 20 20 20 20 70 72 69  6e 74 66 28 22 5c 6e 4e  |.    printf("\nN|
00000800  6f 77 20 61 6e 6f 74 68  65 72 20 73 74 61 74 75  |ow another statu|
00000810  73 20 72 65 70 6f 72 74  20 3a 22 29 3b 0a 20 20  |s report :");.  |
00000820  20 20 67 65 74 53 74 61  74 75 73 28 73 74 64 6f  |  getStatus(stdo|
00000830  75 74 2c 72 6f 6f 74 29  3b 0a 20 20 20 20 0a 20  |ut,root);.    . |
00000840  20 20 20 70 72 69 6e 74  66 28 22 5c 6e 46 69 6e  |   printf("\nFin|
00000850  61 6c 6c 79 2c 20 77 65  27 6c 6c 20 64 65 6c 65  |ally, we'll dele|
00000860  74 65 20 74 68 65 20 63  6f 6d 70 6c 65 74 65 20  |te the complete |
00000870  6c 69 73 74 22 29 3b 0a  20 20 20 20 0a 20 20 20  |list");.    .   |
00000880  20 72 6f 6f 74 3d 64 65  6c 65 74 65 4c 69 73 74  | root=deleteList|
00000890  28 72 6f 6f 74 29 3b 0a  7d 0a                    |(root);.}.|
0000089a