home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / CL / CL.CPP next >
C/C++ Source or Header  |  1991-12-12  |  6KB  |  262 lines

  1. #include "cl.h"
  2.  
  3. // Interface Dependencies ---------------------------------------------------
  4.  
  5. #ifndef __CLSTYPES_H
  6. #include <clstypes.h>
  7. #endif
  8.  
  9. #ifndef __OBJECT_H
  10. #include <object.h>
  11. #endif
  12.  
  13. #ifndef __CONTAIN_H
  14. #include <contain.h>
  15. #endif
  16.  
  17. #ifndef __ARRAY_H
  18. #include <array.h>
  19. #endif
  20.  
  21. // End Interface Dependencies ------------------------------------------------
  22.  
  23. // Implementation Dependencies ----------------------------------------------
  24. // End Implementation Dependencies -------------------------------------------
  25.  
  26. // Member Function //
  27.  
  28. void Array::add (Object& toAdd)
  29.  
  30. // Summary -----------------------------------------------------------------
  31. //
  32. //      Adds the given object to the array.
  33. //
  34. // Parameters
  35. //
  36. //      toAdd
  37. //
  38. //      The object we are to add to the array.  Once the object is
  39. //      added, it is owned by the array.
  40. //
  41. // End ---------------------------------------------------------------------
  42. {
  43.  
  44. // Body Comment
  45. //
  46. //      We search for the first available space for an array element.
  47. //      Since the user may have inserted an element with addAt or
  48. //      with the subscript operator, we check first before overwriting
  49. //      anything.
  50. //
  51. // End
  52.  
  53. // This one is down to Borland - unless they intended Arrays to be sparse
  54. //
  55. // Reset whereToAdd to zero else new items are only added at the end
  56. // and free slots are not reused. OK until you detach items and then
  57. // try to access what has become a sparse array assuming it to be dense !
  58.  
  59.     whereToAdd = 0;
  60.     while (whereToAdd <= upperbound &&
  61.              theArray [whereToAdd] != ZERO) whereToAdd++;
  62.     if (whereToAdd > upperbound) reallocate (whereToAdd - lowerbound + 1);
  63.     theArray [whereToAdd] = &toAdd;
  64.     itemsInContainer++;
  65. }
  66. // End Member Function Array::add //
  67.  
  68. static char *switches  = "/-";
  69.  
  70. void CmdLn::expand ()
  71. {
  72.   char drive [MAXDRIVE],
  73.          dir   [MAXDIR],
  74.          fname [MAXFILE],
  75.          ext   [MAXEXT],
  76.          fn    [MAXPATH],
  77.          path  [MAXPATH];
  78.  
  79.     fnsplit (opt,drive,dir,fname,ext);
  80.     fnmerge (path,drive,dir,"","");
  81.     struct ffblk fileBlock;
  82.     int morefiles = !findfirst (opt,&fileBlock,0);
  83.     while (morefiles)
  84.     {
  85.         sprintf (fn,"%s%s%s",drive,dir,fileBlock.ff_name);
  86.         Names->add (*(new nclass (fn)));
  87.         morefiles = !findnext (&fileBlock);
  88.     } // end while more files.
  89. }
  90.  
  91. void CmdLn::pName ()
  92. {
  93.     if (strchr (opt,'?') || strchr (opt,'*'))
  94.         expand ();
  95.     else
  96.         Names->add (*(new nclass (opt)));
  97. }
  98.  
  99. void CmdLn::reset ()
  100. {
  101.     clearAll ();
  102.     for (int i = 0; i < _argc; i++)
  103.     {
  104.         opt = _argv [i];                        //next possible option cluster
  105.         if (strchr (switches,*opt))        //an option cluster ?
  106.             pCluster ();
  107.         else
  108.             pName ();
  109.     }
  110. }
  111.  
  112. CmdLn::CmdLn (char *Aoptions)
  113. {
  114.     Options = new Array (10,0,10);
  115.     Names   = new Array (10,0,10);
  116.     Errors  = new Array (10,0,10);
  117.     options = Aoptions;
  118.     nameindex = optionindex = errorindex = 0;
  119.     reset ();
  120. }
  121.  
  122. CmdLn::~CmdLn ()
  123. {
  124.     clearAll ();
  125.     delete Options;
  126.     delete Names;
  127.     delete Errors;
  128. }
  129.  
  130. void CmdLn::add (char *Aopt)
  131. {
  132.     char *p1,*p0 = strdup (Aopt);
  133.     p1 = strtok (p0,"\t ");
  134.     while (p1)
  135.     {
  136.         opt = p1;
  137.         if (strchr (switches,*opt))            //an option cluster ?
  138.             pCluster ();
  139.         else
  140.             pName ();
  141.         p1 = strtok(NULL,"\t ");
  142.     }
  143.     delete [strlen (Aopt) + 1] p0;
  144. }
  145.  
  146. char *CmdLn::getName (int no)
  147. {
  148.     if (no >= 0 && no < Names->getItemsInContainer ())
  149.         return ((nclass&)(*Names) [no]).getName ();
  150.     else
  151.         return NULL;
  152. }
  153.  
  154. char CmdLn::gOpt (Array *p,int no,char *&optarg)
  155. {
  156.     if (no >= 0 && no < p->getItemsInContainer ())
  157.         return ((oclass&) (*p) [no]).getOption (optarg);
  158.     else
  159.         return CL_ENDOFLIST;
  160. }
  161.  
  162. char CmdLn::getOption (int no,char *&optarg)
  163. {
  164.     return gOpt (Options,no,optarg);
  165. }
  166.  
  167. char CmdLn::getError (int no)
  168. {
  169.     char *optarg;
  170.     return gOpt (Errors,no,optarg);
  171. }
  172.  
  173. void CmdLn::pOption (char *lookup)
  174. {
  175.     coption = new oclass (*opt++);
  176.     if (lookup)
  177.     {
  178.         if (opt && lookup [1] == ':')
  179.         {
  180.             coption->setOptionArg (opt);
  181.             opt += strlen (opt);
  182.         }
  183.         Options->add (*coption);
  184.     }
  185.     else Errors->add (*coption);
  186. }
  187.  
  188. void CmdLn::pCluster ()
  189. {
  190.     char *lookup;
  191.     opt++;
  192.     pOption (strchr (options,*opt));
  193.     while (*opt && (lookup = strchr (options,*opt))) pOption (lookup);
  194. }
  195.  
  196. void CmdLn::clearNames ()
  197. {
  198.     int n = Names->getItemsInContainer ();
  199.     for (int i = 0; i < n; i++)
  200.     {
  201.         {    //inner scope forces nclass destructor call
  202.             nclass nc = (nclass&)(*Names) [i];
  203.             Names->detach (nc);
  204.         }
  205.     }
  206. }
  207.  
  208. void CmdLn::clear (Array *p)
  209. {
  210.     int n = p->getItemsInContainer ();
  211.     for (int i = 0; i < n; i++)
  212.     {
  213.         {    //inner scope forces oclass destructor call
  214.             oclass oc = (oclass&)(*p) [i];
  215.             p->detach (oc);
  216.         }
  217.     }
  218. }
  219.  
  220. void CmdLn::clearOptions ()
  221. {
  222.     clear (Options);
  223. }
  224.  
  225. void CmdLn::clearErrors ()
  226. {
  227.     clear (Errors);
  228. }
  229.  
  230. void CmdLn::clearAll ()
  231. {
  232.     clearNames ();
  233.     clear (Options);
  234.     clear (Errors);
  235. }
  236.  
  237. void CmdLn::printContentsOn (Rostream os)
  238. {
  239.     int i;
  240.     os << "*** Contents ***" << endl;
  241.     if (numNames ()) os << "*** Names ***" << endl;
  242.     for (i = 0; i < numNames (); i++)
  243.     {
  244.         nclass *nc = &(nclass&)(*Names) [i];
  245.         nc->printOn (os);
  246.     }
  247.     if (numOptions ()) os << "*** Options ***" << endl;
  248.     for (i = 0; i < numOptions (); i++)
  249.     {
  250.         oclass *oc = &(oclass&)(*Options) [i];
  251.         oc->printOn (os);
  252.     }
  253.     if (numErrors ()) os << "*** Errors ***" << endl;
  254.     for (i = 0; i < numErrors (); i++)
  255.     {
  256.         oclass *oc = &(oclass&)(*Errors) [i];
  257.         oc->printOn (os);
  258.     }
  259.     os << "*** End of Contents ***" << endl;
  260. }
  261.  
  262.