home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / answers / Objective-C / sample < prev    next >
Text File  |  1993-12-20  |  29KB  |  1,222 lines

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!xlink.net!zib-berlin.de!netmbx.de!Germany.EU.net!EU.net!sun4nl!tuegate.tue.nl!krait.es.ele.tue.nl!tiggr
  2. From: tiggr@es.ele.tue.nl (Tiggr)
  3. Newsgroups: comp.lang.objective-c,comp.answers,news.answers
  4. Subject: comp.lang.objective-c FAQ, part 3/3: A Sample Program
  5. Supersedes: <sample_753551527@es.ele.tue.nl>
  6. Followup-To: comp.lang.objective-c
  7. Date: 20 Dec 1993 16:26:21 GMT
  8. Organization: Eindhoven University of Technology
  9. Lines: 1202
  10. Approved: news-answers-request@mit.edu
  11. Expires: 29 Jan 1994 16:26:13 GMT
  12. Message-ID: <sample_756404773@es.ele.tue.nl>
  13. Reply-To: tiggr@es.ele.tue.nl (Tiggr)
  14. NNTP-Posting-Host: krait.es.ele.tue.nl
  15. Summary: This third part of the comp.lang.objective-c FAQ postings
  16.         presents a simple sample program written in Objective-C.
  17. Originator: tiggr@krait.es.ele.tue.nl
  18. Xref: senator-bedfellow.mit.edu comp.lang.objective-c:1830 comp.answers:3103 news.answers:16033
  19.  
  20. Archive-name: Objective-C/sample
  21. Version: $Id: sample.preamble,v 2.2 1993/12/17 15:18:42 tiggr Exp $
  22.  
  23.  
  24.         A simple sample
  25.         Objective-C program
  26.  
  27.  
  28. This is the third part in a series of three informational postings
  29. concerning comp.lang.objective-c.  This article presents a simple program
  30. written in Objective-C.  The program consist of several files which are
  31. contained in a shar file (see instructions below on how to unpack).
  32.  
  33. The early version of this FAQ was compiled by Bill Shirley, with the aid of
  34. many people.  The current version is being maintained by Tiggr, aided by a
  35. lot of people, including Paul Sanchez and Bill Shirley.
  36.  
  37. Send your suggestions, additions, bug reports, comments and fixes to
  38. `tiggr@es.ele.tue.nl'.
  39.  
  40.  
  41. #---------------------------------- cut here ----------------------------------
  42. # This is a shell archive.  Remove anything before this line,
  43. # then unpack it by saving it in a file and typing "sh file".
  44. #
  45. # Wrapped by Pieter Schoenmakers <tiggr@viper> on Fri Dec 17 16:18:49 1993
  46. #
  47. # This archive contains:
  48. #    objc-sample    
  49. #
  50. # Existing files will not be overwritten.
  51. # Error checking via wc(1) will be performed.
  52.  
  53. LANG=""; export LANG
  54. PATH=/bin:/usr/bin:$PATH; export PATH
  55.  
  56. echo mkdir - objc-sample
  57. mkdir objc-sample
  58.  
  59. if test -f objc-sample/README
  60. then
  61.     echo Ok to overwrite existing file objc-sample/README\?
  62.     read answer
  63.     case "$answer" in
  64.     [yY]*)    echo Proceeding;;
  65.     *)    echo Aborting; exit 1;;
  66.     esac
  67.     rm -f objc-sample/README
  68.     if test -f objc-sample/README
  69.     then
  70.         echo Error: could not remove objc-sample/README, aborting
  71.         exit 1
  72.     fi
  73. fi
  74. echo x - objc-sample/README
  75. cat >objc-sample/README <<'@EOF'
  76. This directory contains the complete code for the "Simple Sample Objective-C
  77. program" described in the comp.lang.objective-c FAQ.  If you have a suitable
  78. compiler, use the supplied Makefile.  Otherwise, program output can be found
  79. in the file "output".
  80.  
  81. You should probably read "main.m" first.  It is very heavily annotated.
  82.  
  83. Also note and read the file COPYRIGHT.
  84. @EOF
  85. set `wc -lwc <objc-sample/README`
  86. if test $1$2$3 != 855366
  87. then
  88.     echo ERROR: wc results of objc-sample/README are $* should be 8 55 366
  89. fi
  90.  
  91. chmod 644 objc-sample/README
  92.  
  93. if test -f objc-sample/Char.h
  94. then
  95.     echo Ok to overwrite existing file objc-sample/Char.h\?
  96.     read answer
  97.     case "$answer" in
  98.     [yY]*)    echo Proceeding;;
  99.     *)    echo Aborting; exit 1;;
  100.     esac
  101.     rm -f objc-sample/Char.h
  102.     if test -f objc-sample/Char.h
  103.     then
  104.         echo Error: could not remove objc-sample/Char.h, aborting
  105.         exit 1
  106.     fi
  107. fi
  108. echo x - objc-sample/Char.h
  109. cat >objc-sample/Char.h <<'@EOF'
  110. #import <objc/Object.h>
  111.  
  112. @interface Char: Object
  113. {
  114.   int value;
  115. }
  116.  
  117. - init: (int) x;
  118. - report;
  119.  
  120. @end
  121. @EOF
  122. set `wc -lwc <objc-sample/Char.h`
  123. if test $1$2$3 != 1116100
  124. then
  125.     echo ERROR: wc results of objc-sample/Char.h are $* should be 11 16 100
  126. fi
  127.  
  128. chmod 644 objc-sample/Char.h
  129.  
  130. if test -f objc-sample/Node.h
  131. then
  132.     echo Ok to overwrite existing file objc-sample/Node.h\?
  133.     read answer
  134.     case "$answer" in
  135.     [yY]*)    echo Proceeding;;
  136.     *)    echo Aborting; exit 1;;
  137.     esac
  138.     rm -f objc-sample/Node.h
  139.     if test -f objc-sample/Node.h
  140.     then
  141.         echo Error: could not remove objc-sample/Node.h, aborting
  142.         exit 1
  143.     fi
  144. fi
  145. echo x - objc-sample/Node.h
  146. cat >objc-sample/Node.h <<'@EOF'
  147. // Node.h - comp.lang.objective-c simple sample Objective-C program
  148.  
  149. #include <objc/Object.h>
  150.  
  151. @interface      Node : Object
  152. {
  153.   id    next;
  154.   id    data;
  155. }
  156.  
  157.  +    new: anItem;     // create a Node and store anItem in it
  158.  -     free;         // free a Node and return the item in it
  159.  -     next;         // report the id of the next node after this one
  160.  -    set_next: aNode; // make the next node be aNode
  161. @end
  162. @EOF
  163. set `wc -lwc <objc-sample/Node.h`
  164. if test $1$2$3 != 1568379
  165. then
  166.     echo ERROR: wc results of objc-sample/Node.h are $* should be 15 68 379
  167. fi
  168.  
  169. chmod 644 objc-sample/Node.h
  170.  
  171. if test -f objc-sample/Node.m
  172. then
  173.     echo Ok to overwrite existing file objc-sample/Node.m\?
  174.     read answer
  175.     case "$answer" in
  176.     [yY]*)    echo Proceeding;;
  177.     *)    echo Aborting; exit 1;;
  178.     esac
  179.     rm -f objc-sample/Node.m
  180.     if test -f objc-sample/Node.m
  181.     then
  182.         echo Error: could not remove objc-sample/Node.m, aborting
  183.         exit 1
  184.     fi
  185. fi
  186. echo x - objc-sample/Node.m
  187. cat >objc-sample/Node.m <<'@EOF'
  188. // Node.m - comp.lang.objective-c simple sample Objective-C program
  189.  
  190. #include <objc/Object.h>
  191. #include "Node.h"
  192.  
  193. @implementation    Node : Object
  194.  
  195. + new: anItem
  196. {
  197.     self = [super new];
  198.     next = 0;
  199.     data = anItem;
  200.     return self;
  201. }
  202.  
  203. - free
  204. {
  205.     id tmp = data;
  206.     [super free];
  207.     return tmp;
  208. }
  209.  
  210. - next { return next; }
  211.  
  212. - set_next: aNode { next = aNode; return self; }
  213.  
  214. @end
  215. @EOF
  216. set `wc -lwc <objc-sample/Node.m`
  217. if test $1$2$3 != 2762382
  218. then
  219.     echo ERROR: wc results of objc-sample/Node.m are $* should be 27 62 382
  220. fi
  221.  
  222. chmod 644 objc-sample/Node.m
  223.  
  224. if test -f objc-sample/Queue.h
  225. then
  226.     echo Ok to overwrite existing file objc-sample/Queue.h\?
  227.     read answer
  228.     case "$answer" in
  229.     [yY]*)    echo Proceeding;;
  230.     *)    echo Aborting; exit 1;;
  231.     esac
  232.     rm -f objc-sample/Queue.h
  233.     if test -f objc-sample/Queue.h
  234.     then
  235.         echo Error: could not remove objc-sample/Queue.h, aborting
  236.         exit 1
  237.     fi
  238. fi
  239. echo x - objc-sample/Queue.h
  240. cat >objc-sample/Queue.h <<'@EOF'
  241.  
  242. // Queue.h - comp.lang.objective-c simple sample Objective-C program
  243.  
  244. #import <objc/Object.h>
  245. #import "Node.h"
  246.  
  247. @interface    Queue : Object
  248. {
  249.     id    head;
  250.     id    tail;
  251.     unsigned    qsize;
  252. }
  253.  
  254.  +        new;        // create a new Queue
  255.  -        empty;        // clear out all contents of the Queue
  256.  -        put: anItem;    // put anItem on the Queue
  257.  -        get;        // return the item on top of the Queue
  258.  - (unsigned)    size;        // tell us the current size of the Queue
  259. @end
  260. @EOF
  261. set `wc -lwc <objc-sample/Queue.h`
  262. if test $1$2$3 != 1974433
  263. then
  264.     echo ERROR: wc results of objc-sample/Queue.h are $* should be 19 74 433
  265. fi
  266.  
  267. chmod 644 objc-sample/Queue.h
  268.  
  269. if test -f objc-sample/Queue.m
  270. then
  271.     echo Ok to overwrite existing file objc-sample/Queue.m\?
  272.     read answer
  273.     case "$answer" in
  274.     [yY]*)    echo Proceeding;;
  275.     *)    echo Aborting; exit 1;;
  276.     esac
  277.     rm -f objc-sample/Queue.m
  278.     if test -f objc-sample/Queue.m
  279.     then
  280.         echo Error: could not remove objc-sample/Queue.m, aborting
  281.         exit 1
  282.     fi
  283. fi
  284. echo x - objc-sample/Queue.m
  285. cat >objc-sample/Queue.m <<'@EOF'
  286.  
  287. // Queue.m - comp.lang.objective-c simple sample Objective-C program
  288.  
  289. #import "Queue.h"
  290.  
  291. @implementation    Queue
  292.  
  293. + new
  294. {
  295.     self = [super new];
  296.     head = tail = 0;
  297.     qsize = 0;
  298.     return self;
  299. }
  300.  
  301. - empty
  302. {
  303.     while([self size])
  304.     [[self get] free];
  305.     return self;
  306. }
  307.  
  308. - put: anItem
  309. {
  310.   if (tail)
  311.     tail = [[tail set_next : [Node new : anItem]] next];
  312.   else
  313.     head = tail = [Node new : anItem];
  314.   ++qsize;
  315.     return self;
  316. }
  317.  
  318. - get
  319. {
  320.   id contents;
  321.   id old_head = head;
  322.   head = [head next];
  323.   contents = [old_head free];
  324.   if (--qsize == 0)
  325.       tail = head;
  326.   return contents;
  327. }
  328.  
  329. - (unsigned) size { return qsize; }
  330.  
  331. @end
  332. @EOF
  333. set `wc -lwc <objc-sample/Queue.m`
  334. if test $1$2$3 != 46105630
  335. then
  336.     echo ERROR: wc results of objc-sample/Queue.m are $* should be 46 105 630
  337. fi
  338.  
  339. chmod 644 objc-sample/Queue.m
  340.  
  341. if test -f objc-sample/Stack.h
  342. then
  343.     echo Ok to overwrite existing file objc-sample/Stack.h\?
  344.     read answer
  345.     case "$answer" in
  346.     [yY]*)    echo Proceeding;;
  347.     *)    echo Aborting; exit 1;;
  348.     esac
  349.     rm -f objc-sample/Stack.h
  350.     if test -f objc-sample/Stack.h
  351.     then
  352.         echo Error: could not remove objc-sample/Stack.h, aborting
  353.         exit 1
  354.     fi
  355. fi
  356. echo x - objc-sample/Stack.h
  357. cat >objc-sample/Stack.h <<'@EOF'
  358.  
  359. // Stack.h - comp.lang.objective-c simple sample Objective-C program
  360.  
  361. #import <objc/Object.h>
  362. #import "Node.h"
  363.  
  364. @interface    Stack : Object
  365. {
  366.     id        stack;
  367.     unsigned    stack_size;
  368. }
  369.  
  370.  +        new;        // create a new Stack
  371.  -        empty;        // clear out all contents of the Stack
  372.  -        put: anItem;    // put anItem on the Stack
  373.  -        get;        // return the item on top of the Stack
  374.  - (unsigned)    size;        // tell us the current size of the Stack
  375. @end
  376. @EOF
  377. set `wc -lwc <objc-sample/Stack.h`
  378. if test $1$2$3 != 1872421
  379. then
  380.     echo ERROR: wc results of objc-sample/Stack.h are $* should be 18 72 421
  381. fi
  382.  
  383. chmod 644 objc-sample/Stack.h
  384.  
  385. echo mkdir - objc-sample/RCS
  386. mkdir objc-sample/RCS
  387.  
  388. if test -f objc-sample/RCS/Char.m,v
  389. then
  390.     echo Ok to overwrite existing file objc-sample/RCS/Char.m,v\?
  391.     read answer
  392.     case "$answer" in
  393.     [yY]*)    echo Proceeding;;
  394.     *)    echo Aborting; exit 1;;
  395.     esac
  396.     rm -f objc-sample/RCS/Char.m,v
  397.     if test -f objc-sample/RCS/Char.m,v
  398.     then
  399.         echo Error: could not remove objc-sample/RCS/Char.m,v, aborting
  400.         exit 1
  401.     fi
  402. fi
  403. echo x - objc-sample/RCS/Char.m,v
  404. cat >objc-sample/RCS/Char.m,v <<'@EOF'
  405. head    1.1;
  406. access;
  407. symbols;
  408. locks
  409.     tiggr:1.1; strict;
  410. comment    @# @;
  411.  
  412.  
  413. 1.1
  414. date    93.12.02.09.24.28;    author tiggr;    state Exp;
  415. branches;
  416. next    ;
  417.  
  418.  
  419. desc
  420. @@
  421.  
  422.  
  423. 1.1
  424. log
  425. @Initial revision
  426. @
  427. text
  428. @#import <stdio.h>
  429. #import "Char.h"
  430.  
  431. @@implementation Char
  432. {
  433.   int value;
  434. }
  435.  
  436. - init: (int) x
  437. {
  438.   [super init];        // In case the parent class is doing
  439.               // something special in its init...
  440.   value = x;
  441.   return self;
  442. }
  443.  
  444. - report
  445. {
  446.   printf("   %c", value);
  447.   return self;
  448. }
  449.  
  450. @@end
  451. @
  452. @EOF
  453. set `wc -lwc <objc-sample/RCS/Char.m,v`
  454. if test $1$2$3 != 4776468
  455. then
  456.     echo ERROR: wc results of objc-sample/RCS/Char.m,v are $* should be 47 76 468
  457. fi
  458.  
  459. chmod 444 objc-sample/RCS/Char.m,v
  460.  
  461. if test -f objc-sample/RCS/Float.m,v
  462. then
  463.     echo Ok to overwrite existing file objc-sample/RCS/Float.m,v\?
  464.     read answer
  465.     case "$answer" in
  466.     [yY]*)    echo Proceeding;;
  467.     *)    echo Aborting; exit 1;;
  468.     esac
  469.     rm -f objc-sample/RCS/Float.m,v
  470.     if test -f objc-sample/RCS/Float.m,v
  471.     then
  472.         echo Error: could not remove objc-sample/RCS/Float.m,v, aborting
  473.         exit 1
  474.     fi
  475. fi
  476. echo x - objc-sample/RCS/Float.m,v
  477. cat >objc-sample/RCS/Float.m,v <<'@EOF'
  478. head    1.1;
  479. access;
  480. symbols;
  481. locks
  482.     tiggr:1.1; strict;
  483. comment    @# @;
  484.  
  485.  
  486. 1.1
  487. date    93.12.02.09.24.56;    author tiggr;    state Exp;
  488. branches;
  489. next    ;
  490.  
  491.  
  492. desc
  493. @@
  494.  
  495.  
  496. 1.1
  497. log
  498. @Initial revision
  499. @
  500. text
  501. @#import <stdio.h>
  502. #import "Float.h"
  503.  
  504. @@implementation Float
  505. {
  506.   float value;
  507. }
  508.  
  509. - init: (float) x
  510. {
  511.   [super init];        // In case the parent class is doing
  512.               // something special in its init...
  513.   value = x;
  514.   return self;
  515. }
  516.  
  517. - report
  518. {
  519.   printf("%4.1f", value);
  520.   return self;
  521. }
  522.  
  523. @@end
  524. @
  525. @EOF
  526. set `wc -lwc <objc-sample/RCS/Float.m,v`
  527. if test $1$2$3 != 4775474
  528. then
  529.     echo ERROR: wc results of objc-sample/RCS/Float.m,v are $* should be 47 75 474
  530. fi
  531.  
  532. chmod 444 objc-sample/RCS/Float.m,v
  533.  
  534. chmod 755 objc-sample/RCS
  535.  
  536. if test -f objc-sample/Stack.m
  537. then
  538.     echo Ok to overwrite existing file objc-sample/Stack.m\?
  539.     read answer
  540.     case "$answer" in
  541.     [yY]*)    echo Proceeding;;
  542.     *)    echo Aborting; exit 1;;
  543.     esac
  544.     rm -f objc-sample/Stack.m
  545.     if test -f objc-sample/Stack.m
  546.     then
  547.         echo Error: could not remove objc-sample/Stack.m, aborting
  548.         exit 1
  549.     fi
  550. fi
  551. echo x - objc-sample/Stack.m
  552. cat >objc-sample/Stack.m <<'@EOF'
  553.  
  554. // Stack.m - comp.lang.objective-c simple sample Objective-C program
  555.  
  556. #import "Stack.h"
  557.  
  558. @implementation    Stack
  559.  
  560. + new
  561. {
  562.     self = [super new];
  563.     stack = 0;
  564.     stack_size = 0;
  565.     return self;
  566. }
  567.  
  568. - empty
  569. {
  570.     while([self size])
  571.     [[self get] free];
  572.     return self;
  573. }
  574.  
  575. - put: anItem
  576. {
  577.   stack = [[Node new : anItem] set_next : stack];
  578.   ++stack_size;
  579.   return self;
  580. }
  581.  
  582. - get
  583. {
  584.   id contents;
  585.   id old_stack = stack;
  586.   stack = [stack next];
  587.   contents = [old_stack free];
  588.   --stack_size;
  589.   return contents;
  590. }
  591.  
  592. - (unsigned) size { return stack_size; }
  593.  
  594. @end
  595. @EOF
  596. set `wc -lwc <objc-sample/Stack.m`
  597. if test $1$2$3 != 4285560
  598. then
  599.     echo ERROR: wc results of objc-sample/Stack.m are $* should be 42 85 560
  600. fi
  601.  
  602. chmod 644 objc-sample/Stack.m
  603.  
  604. if test -f objc-sample/output
  605. then
  606.     echo Ok to overwrite existing file objc-sample/output\?
  607.     read answer
  608.     case "$answer" in
  609.     [yY]*)    echo Proceeding;;
  610.     *)    echo Aborting; exit 1;;
  611.     esac
  612.     rm -f objc-sample/output
  613.     if test -f objc-sample/output
  614.     then
  615.         echo Error: could not remove objc-sample/output, aborting
  616.         exit 1
  617.     fi
  618. fi
  619. echo x - objc-sample/output
  620. cat >objc-sample/output <<'@EOF'
  621. Output from demo, excluding Char class:
  622.  
  623. Include the Char class in the demo? (y/n): n
  624. queue:   5, stack:-5.0
  625. queue: 4.0, stack:  -4
  626. queue:   3, stack:-3.0
  627. queue: 2.0, stack:  -2
  628. queue:   1, stack:-1.0
  629. queue: 0.0, stack:   0
  630. queue:  -1, stack: 1.0
  631. queue:-2.0, stack:   2
  632. queue:  -3, stack: 3.0
  633. queue:-4.0, stack:   4
  634. queue:  -5, stack: 5.0
  635.  
  636. Output from demo, including Char class:
  637.  
  638. Include the Char class in the demo? (y/n): y
  639. queue:   5, stack:   h
  640. queue:   q, stack:-4.0
  641. queue:   3, stack:   j
  642. queue:   o, stack:-2.0
  643. queue:   1, stack:   l
  644. queue:   m, stack: 0.0
  645. queue:  -1, stack:   n
  646. queue:   k, stack: 2.0
  647. queue:  -3, stack:   p
  648. queue:   i, stack: 4.0
  649. queue:  -5, stack:   r
  650. @EOF
  651. set `wc -lwc <objc-sample/output`
  652. if test $1$2$3 != 29111679
  653. then
  654.     echo ERROR: wc results of objc-sample/output are $* should be 29 111 679
  655. fi
  656.  
  657. chmod 644 objc-sample/output
  658.  
  659. if test -f objc-sample/Char.m
  660. then
  661.     echo Ok to overwrite existing file objc-sample/Char.m\?
  662.     read answer
  663.     case "$answer" in
  664.     [yY]*)    echo Proceeding;;
  665.     *)    echo Aborting; exit 1;;
  666.     esac
  667.     rm -f objc-sample/Char.m
  668.     if test -f objc-sample/Char.m
  669.     then
  670.         echo Error: could not remove objc-sample/Char.m, aborting
  671.         exit 1
  672.     fi
  673. fi
  674. echo x - objc-sample/Char.m
  675. cat >objc-sample/Char.m <<'@EOF'
  676. #include <stdio.h>
  677. #include "Char.h"
  678.  
  679. @implementation Char
  680. {
  681.   int value;
  682. }
  683.  
  684. - init: (int) x
  685. {
  686.   [super init];        // In case the parent class is doing
  687.               // something special in its init...
  688.   value = x;
  689.   return self;
  690. }
  691.  
  692. - report
  693. {
  694.   printf("   %c", value);
  695.   return self;
  696. }
  697.  
  698. @end
  699. @EOF
  700. set `wc -lwc <objc-sample/Char.m`
  701. if test $1$2$3 != 2347281
  702. then
  703.     echo ERROR: wc results of objc-sample/Char.m are $* should be 23 47 281
  704. fi
  705.  
  706. chmod 644 objc-sample/Char.m
  707.  
  708. if test -f objc-sample/Makefile
  709. then
  710.     echo Ok to overwrite existing file objc-sample/Makefile\?
  711.     read answer
  712.     case "$answer" in
  713.     [yY]*)    echo Proceeding;;
  714.     *)    echo Aborting; exit 1;;
  715.     esac
  716.     rm -f objc-sample/Makefile
  717.     if test -f objc-sample/Makefile
  718.     then
  719.         echo Error: could not remove objc-sample/Makefile, aborting
  720.         exit 1
  721.     fi
  722. fi
  723. echo x - objc-sample/Makefile
  724. cat >objc-sample/Makefile <<'@EOF'
  725. # This Makefile assumes you have GNU gcc 2.3 or better and a suitable
  726. # runtime library with object support.  It also works on a NeXT.  Don't know
  727. # about Stepstone.
  728.  
  729. .SUFFIXES: .o .m
  730. .m.o:
  731.     $(CC) -c $(CFLAGS) $<
  732.  
  733. # Use this on a NeXT
  734. #CC=        cc
  735. #LIBS=        
  736. # Use this with GNU CC on a non-NeXT.
  737. CC=        gcc
  738. LIBS=        -lobjc
  739.  
  740. CFLAGS=        -Wall -O2 -g
  741. OFILES=        main.o Node.o Queue.o Stack.o Float.o Char.o
  742.  
  743. demo: $(OFILES)
  744.     $(CC) $(CFLAGS) $(LDFLAGS) -o demo $(OFILES) $(LIBS)
  745.  
  746. clean:
  747.     rm -f $(OFILES) demo
  748.     
  749. Char.o : Char.m Char.h 
  750.  
  751. Float.o : Float.m Float.h 
  752.  
  753. Node.o : Node.m Node.h 
  754.  
  755. Queue.o : Queue.m Queue.h Node.h 
  756.  
  757. Stack.o : Stack.m Stack.h Node.h 
  758.  
  759. main.o : main.m Queue.h Node.h Stack.h Float.h
  760. @EOF
  761. set `wc -lwc <objc-sample/Makefile`
  762. if test $1$2$3 != 35115689
  763. then
  764.     echo ERROR: wc results of objc-sample/Makefile are $* should be 35 115 689
  765. fi
  766.  
  767. chmod 644 objc-sample/Makefile
  768.  
  769. if test -f objc-sample/Float.m
  770. then
  771.     echo Ok to overwrite existing file objc-sample/Float.m\?
  772.     read answer
  773.     case "$answer" in
  774.     [yY]*)    echo Proceeding;;
  775.     *)    echo Aborting; exit 1;;
  776.     esac
  777.     rm -f objc-sample/Float.m
  778.     if test -f objc-sample/Float.m
  779.     then
  780.         echo Error: could not remove objc-sample/Float.m, aborting
  781.         exit 1
  782.     fi
  783. fi
  784. echo x - objc-sample/Float.m
  785. cat >objc-sample/Float.m <<'@EOF'
  786. #include <stdio.h>
  787. #include "Float.h"
  788.  
  789. @implementation Float
  790. {
  791.   float value;
  792. }
  793.  
  794. - init: (float) x
  795. {
  796.   [super init];        // In case the parent class is doing
  797.               // something special in its init...
  798.   value = x;
  799.   return self;
  800. }
  801.  
  802. - report
  803. {
  804.   printf("%4.1f", value);
  805.   return self;
  806. }
  807.  
  808. @end
  809. @EOF
  810. set `wc -lwc <objc-sample/Float.m`
  811. if test $1$2$3 != 2346287
  812. then
  813.     echo ERROR: wc results of objc-sample/Float.m are $* should be 23 46 287
  814. fi
  815.  
  816. chmod 644 objc-sample/Float.m
  817.  
  818. if test -f objc-sample/main.m
  819. then
  820.     echo Ok to overwrite existing file objc-sample/main.m\?
  821.     read answer
  822.     case "$answer" in
  823.     [yY]*)    echo Proceeding;;
  824.     *)    echo Aborting; exit 1;;
  825.     esac
  826.     rm -f objc-sample/main.m
  827.     if test -f objc-sample/main.m
  828.     then
  829.         echo Error: could not remove objc-sample/main.m, aborting
  830.         exit 1
  831.     fi
  832. fi
  833. echo x - objc-sample/main.m
  834. cat >objc-sample/main.m <<'@EOF'
  835. // main.m - comp.lang.objective-c simple sample Objective-C program
  836.  
  837. // This is a comment.  Everything to the right of a double slash
  838. // is ignored.
  839.  
  840. /* This is too.  Objective-C is a superset of ANSI C, so you don't lose any
  841.    of your favorite features.  */
  842.  
  843. // Classes are the one real extension which Objective C adds to
  844. // ANSI C.  A class is a description of a collection of data, like
  845. // a C structure, and the methods by which that data may be accessed
  846. // or manipulated.  Instances of a class are called objects, and
  847. // methods are invoked by sending messages to either the class itself,
  848. // to produce objects, or to those objects.  The recipient of a message
  849. // is called a "receiver".  The form of a message is:
  850. //
  851. //    [receiver method andMaybeSomeArguments]
  852. //
  853.  
  854. // the receiver and method components are mandatory, as are the square
  855. // brackets surrounding the message.  Additional arguments may or may not be
  856. // present, depending upon the method definition.  Messages may appear
  857. // anywhere a statement is allowed in C.
  858.  
  859. // The first thing we do is bring in some include files, as in
  860. // C.  On the NeXT, it is customary to use the "import" statement which
  861. // guarantees that the file isn't included more than once.  Using GNU CC
  862. // this is not all that nice sinds it generates a huge warning for every
  863. // file being compiled.  So, since it does not really matter, we'll stick to
  864. // `#include'.
  865.  
  866. #include <stdio.h>
  867. #include <objc/Object.h>
  868. #include "Queue.h"
  869. #include "Stack.h"
  870.  
  871. // That brought in class definitions for Objects, Queues, and
  872. // Stacks.  The Object class is the basis for all other classes,
  873. // which is why it gets brought in first.  It provides basic functional
  874. // behavior which is inherited by all derived classes.  All user created
  875. // classes should have Object somewhere in their ancestry.
  876.  
  877. // Queue and Stack are classes of our own construction, and provide FIFO and
  878. // LIFO storage capabilities, respectively.  I'm not going to go into
  879. // implementation details here.  It's irrelevant how they work, all that is
  880. // important is that they both respond to 'put:' and 'get'.  If you want to
  881. // inspect them, look into the Queue.m, Stack.m, Queue.h and Stack.h files.
  882.  
  883. // A simple Class definition follows.  It inherits directly from the base
  884. // class "Object".  This gives it lots of nice properties, not the least of
  885. // which is the ability to be referenced by any pointer of the generic
  886. // object type "id".  All objects can be pointed to by any id variable, and
  887. // the default return type from methods is id.  This allows messages to be
  888. // embedded in other messages, either as receivers or arguments.
  889.  
  890. // An Int object allocates space for a single integer.  The "report" message
  891. // causes it to report its value.  Everything between the @implementation
  892. // and the @end is part of the class definition.
  893.  
  894. // Note - It is *highly* unusual to have a class implementation in your main
  895. // program.  Since the object is fully defined before it gets used, no
  896. // interface description is required.  There is nothing illegal about doing
  897. // things this way, but it is so unusual that the compiler will produce a
  898. // warning for this class.  We have included the Int class implementation
  899. // here solely for expository purposes.
  900.  
  901. @implementation Int: Object    // Int is derived from Object
  902. {
  903.     int value;        // This is the data portion.  Like a struct.
  904. }
  905.  
  906. // The following are the method definitions.  A "+" prefix means
  907. // it is a factory method, i.e., how to manufacture instances of
  908. // the class.  The body of the method is between braces, like a C
  909. // function.
  910.  
  911. // This class doesn't define any factory methods.  It relies on the
  912. // +alloc method defined in class Object.  For examples of factory
  913. // methods, look at the +new method defined in the Stack or
  914. // Queue implementations.
  915.  
  916. // Self is a special variable, which refers to the object currently
  917. // being manipulated.  Super refers to the parent class of self.
  918. // The following method asks the parent class (Object) to hand us a
  919. // new instance, which becomes self.  Then we update the instance
  920. // variables and return a pointer to the new object.
  921.  
  922. // It is standard for methods that do not need to return any
  923. // special value to instead return self.  This allows for a 
  924. // nested syntax of method calls.
  925.  
  926. // The "-" in front of init means that it's an instance method,
  927. // i.e., something a particular object should respond to.
  928.  
  929. - init: (int) i        // This method will initialize a new Int 
  930. {
  931.   [super init];        // In case the parent class is doing
  932.               // something special in its init...
  933.   value = i;
  934.   return self;
  935. }
  936.  
  937. - report
  938. {
  939.   printf("%4d", value);
  940.   return self;
  941. }
  942.  
  943. @end
  944.  
  945. // We have implemented Float and Char classes more traditionally, using
  946. // separate files for the interface (.h) and implementation (.m).
  947. // The Float and Char objects are like the Int object, but with the
  948. // obvious difference that they work with floats and characters.
  949. // We include the interface definitions at this point.
  950.  
  951. #import "Float.h"
  952. #import "Char.h"
  953.  
  954. // If you inspect those files, note polymorphism -- methods have same
  955. // names as in the Int class.
  956.  
  957. int main(void)
  958. {
  959.     // First we create instances of "Stack" and "Queue" data structures
  960.  
  961.     id queue = [Queue new];    // +new is an older convention for
  962.     id stack = [Stack new];    // allocation & initialization.
  963.                     // [[x alloc] init] is the preferred
  964.                 // way to do this now.
  965.     int i;
  966.     int reply;
  967.     
  968.     fprintf(stderr, "Include the Char class in the demo? (y/n): ");
  969.     reply = getchar();    // no error checking...
  970.  
  971.     for (i = 5; i > -6; --i)
  972.     {
  973.     // Depending on which version of the demo we're running,
  974.     // we alternate putting Int's and Floats onto the queue and
  975.     // stack, or Int's, Float's, and Char's.
  976.  
  977.     if (reply == 'y')
  978.     {
  979.         // If "i" is odd we put an Int on the queue and a Char on
  980.         // the stack.  If "i" is even we put an Char on the queue
  981.         // and a Float on the stack.
  982.  
  983.         // Since there is more than one method "init", and
  984.         // Objective-C uses run-time binding, the compiler doesn't
  985.         // know what the type of the object returned by alloc.
  986.         // We disambiguate with an explicit C-style cast to make
  987.         // sure the appropriate "init" is invoked. 
  988.  
  989.             [queue put: (i & 1) ?
  990.                 [(Int*)[Int alloc] init: i] :
  991.           [(Char*)[Char alloc] init: 'm'+i]];
  992.             [stack put: (i & 1) ?
  993.                 [(Char*)[Char alloc] init: 'm'+i] :
  994.           [(Float*)[Float alloc] init: i]];
  995.     }
  996.     else    // Note - garbage input will invoke the else clause.
  997.     {
  998.         // If "i" is odd we put an Int on the queue and a Float on
  999.         // the stack.  If "i" is even we put a Float on the queue
  1000.         // and an Int on the stack.
  1001.  
  1002.             [queue put: (i & 1) ?
  1003.                 [(Int*)[Int alloc] init: i] :
  1004.           [(Float*)[Float alloc] init: i]];
  1005.             [stack put: (i & 1) ?
  1006.                 [(Float*)[Float alloc] init: i] :
  1007.           [(Int*)[Int alloc] init: i]];
  1008.     }
  1009.     }
  1010.  
  1011.     while ([queue size] && [stack size])
  1012.     {
  1013.     // The following illustrates run-time binding.  Will report be
  1014.     // invoked for a Float object or an Int object?  Did the user
  1015.     // elect for Char objects at run time?  We don't know ahead
  1016.     // of time, but with run-time binding and polymorphism
  1017.     // it works properly.  The burden is on the class
  1018.     // implementer rather than the class user.  
  1019.  
  1020.     // Note that the following lines remain unchanged, whether
  1021.     // we are using the Char class or not.  The queue and stack
  1022.     // hand us the next object, it reports itself regardless
  1023.     // of its type, and then it frees itself.
  1024.  
  1025.     printf("queue:");    [[[queue get] report] free];
  1026.     printf(", stack:");    [[[stack get] report] free];
  1027.     putchar('\n');
  1028.     }
  1029.     return 0;
  1030. }
  1031. @EOF
  1032. set `wc -lwc <objc-sample/main.m`
  1033. if test $1$2$3 != 19613247686
  1034. then
  1035.     echo ERROR: wc results of objc-sample/main.m are $* should be 196 1324 7686
  1036. fi
  1037.  
  1038. chmod 644 objc-sample/main.m
  1039.  
  1040. if test -f objc-sample/COPYRIGHT
  1041. then
  1042.     echo Ok to overwrite existing file objc-sample/COPYRIGHT\?
  1043.     read answer
  1044.     case "$answer" in
  1045.     [yY]*)    echo Proceeding;;
  1046.     *)    echo Aborting; exit 1;;
  1047.     esac
  1048.     rm -f objc-sample/COPYRIGHT
  1049.     if test -f objc-sample/COPYRIGHT
  1050.     then
  1051.         echo Error: could not remove objc-sample/COPYRIGHT, aborting
  1052.         exit 1
  1053.     fi
  1054. fi
  1055. echo x - objc-sample/COPYRIGHT
  1056. cat >objc-sample/COPYRIGHT <<'@EOF'
  1057. This copyright notice applies to all source files distributed in the
  1058. comp.lang.objective-c FAQ: `A Simple Sample Objective-C program'.
  1059.  
  1060. Copyright (C) 1993 Paul J. Sanchez and Bill Shirley
  1061.  
  1062. The `simple sample Objective-C program' is free software; you can
  1063. redistribute it and/or modify it under the terms of the GNU General Public
  1064. License as published by the Free Software Foundation; either version 2, or
  1065. (at your option) any later version.
  1066.  
  1067. The `simple sample Objective-C program' is distributed in the hope that it
  1068. will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  1069. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
  1070. Public License for more details.
  1071.  
  1072. You should have received a copy of the GNU General Public License
  1073. along with GNU Emacs; see the file COPYING.  If not, write to
  1074. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  1075. @EOF
  1076. set `wc -lwc <objc-sample/COPYRIGHT`
  1077. if test $1$2$3 != 18144902
  1078. then
  1079.     echo ERROR: wc results of objc-sample/COPYRIGHT are $* should be 18 144 902
  1080. fi
  1081.  
  1082. chmod 644 objc-sample/COPYRIGHT
  1083.  
  1084. if test -f objc-sample/Float.h
  1085. then
  1086.     echo Ok to overwrite existing file objc-sample/Float.h\?
  1087.     read answer
  1088.     case "$answer" in
  1089.     [yY]*)    echo Proceeding;;
  1090.     *)    echo Aborting; exit 1;;
  1091.     esac
  1092.     rm -f objc-sample/Float.h
  1093.     if test -f objc-sample/Float.h
  1094.     then
  1095.         echo Error: could not remove objc-sample/Float.h, aborting
  1096.         exit 1
  1097.     fi
  1098. fi
  1099. echo x - objc-sample/Float.h
  1100. cat >objc-sample/Float.h <<'@EOF'
  1101. #import <objc/Object.h>
  1102.  
  1103. @interface Float: Object
  1104. {
  1105.   float value;
  1106. }
  1107.  
  1108. - init: (float) x;
  1109. - report;
  1110.  
  1111. @end
  1112. @EOF
  1113. set `wc -lwc <objc-sample/Float.h`
  1114. if test $1$2$3 != 1116105
  1115. then
  1116.     echo ERROR: wc results of objc-sample/Float.h are $* should be 11 16 105
  1117. fi
  1118.  
  1119. chmod 644 objc-sample/Float.h
  1120.  
  1121. if test -f objc-sample/Float.m.\~1\~
  1122. then
  1123.     echo Ok to overwrite existing file objc-sample/Float.m.\~1\~\?
  1124.     read answer
  1125.     case "$answer" in
  1126.     [yY]*)    echo Proceeding;;
  1127.     *)    echo Aborting; exit 1;;
  1128.     esac
  1129.     rm -f objc-sample/Float.m.\~1\~
  1130.     if test -f objc-sample/Float.m.\~1\~
  1131.     then
  1132.         echo Error: could not remove objc-sample/Float.m.\~1\~, aborting
  1133.         exit 1
  1134.     fi
  1135. fi
  1136. echo x - objc-sample/Float.m.\~1\~
  1137. cat >objc-sample/Float.m.\~1\~ <<'@EOF'
  1138. #import <stdio.h>
  1139. #import "Float.h"
  1140.  
  1141. @implementation Float
  1142. {
  1143.   float value;
  1144. }
  1145.  
  1146. - init: (float) x
  1147. {
  1148.   [super init];        // In case the parent class is doing
  1149.               // something special in its init...
  1150.   value = x;
  1151.   return self;
  1152. }
  1153.  
  1154. - report
  1155. {
  1156.   printf("%4.1f", value);
  1157.   return self;
  1158. }
  1159.  
  1160. @end
  1161. @EOF
  1162. set `wc -lwc <objc-sample/Float.m.\~1\~`
  1163. if test $1$2$3 != 2346285
  1164. then
  1165.     echo ERROR: wc results of objc-sample/Float.m.\~1\~ are $* should be 23 46 285
  1166. fi
  1167.  
  1168. chmod 644 objc-sample/Float.m.\~1\~
  1169.  
  1170. if test -f objc-sample/Char.m.\~1\~
  1171. then
  1172.     echo Ok to overwrite existing file objc-sample/Char.m.\~1\~\?
  1173.     read answer
  1174.     case "$answer" in
  1175.     [yY]*)    echo Proceeding;;
  1176.     *)    echo Aborting; exit 1;;
  1177.     esac
  1178.     rm -f objc-sample/Char.m.\~1\~
  1179.     if test -f objc-sample/Char.m.\~1\~
  1180.     then
  1181.         echo Error: could not remove objc-sample/Char.m.\~1\~, aborting
  1182.         exit 1
  1183.     fi
  1184. fi
  1185. echo x - objc-sample/Char.m.\~1\~
  1186. cat >objc-sample/Char.m.\~1\~ <<'@EOF'
  1187. #import <stdio.h>
  1188. #import "Char.h"
  1189.  
  1190. @implementation Char
  1191. {
  1192.   int value;
  1193. }
  1194.  
  1195. - init: (int) x
  1196. {
  1197.   [super init];        // In case the parent class is doing
  1198.               // something special in its init...
  1199.   value = x;
  1200.   return self;
  1201. }
  1202.  
  1203. - report
  1204. {
  1205.   printf("   %c", value);
  1206.   return self;
  1207. }
  1208.  
  1209. @end
  1210. @EOF
  1211. set `wc -lwc <objc-sample/Char.m.\~1\~`
  1212. if test $1$2$3 != 2347279
  1213. then
  1214.     echo ERROR: wc results of objc-sample/Char.m.\~1\~ are $* should be 23 47 279
  1215. fi
  1216.  
  1217. chmod 644 objc-sample/Char.m.\~1\~
  1218.  
  1219. chmod 755 objc-sample
  1220.  
  1221. exit 0
  1222.