home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / gopher / gopher1.01 / mindexd / gopherstruct.c < prev    next >
C/C++ Source or Header  |  1992-04-08  |  3KB  |  183 lines

  1. #include "gopherd.h"
  2.  
  3. void
  4. GSinit(gs)
  5.   GopherStruct *gs;
  6. {
  7.      GSsetType(gs, '\0');
  8.  
  9.      gs->sPath[0] = '\0';
  10.      gs->sTitle[0] = '\0';
  11.      gs->sHost[0] = '\0';
  12.      gs->iPort = 0;
  13.      gs->iItemnum = 0;
  14.  
  15. }
  16.  
  17. void
  18. GStoNet(gs, sockfd)
  19.   GopherStruct *gs;
  20.   int sockfd;
  21. {
  22.      static char buf[1024];
  23.  
  24.      buf[0] = GSgetType(gs);
  25.  
  26.      sprintf(buf + 1, "%s\t%s\t%s\t%d\r\n",
  27.          GSgetTitle(gs),
  28.          GSgetPath(gs),
  29.          GSgetHost(gs),
  30.          GSgetPort(gs));
  31.  
  32.      writestring(sockfd, buf);
  33.      
  34.      if (DEBUG)
  35.       fprintf(stderr, buf);
  36.  
  37. }
  38.  
  39. /** Copy a gopherstruct ***/
  40.  
  41. void
  42. GScpy(dest, orig)
  43.   GopherStruct *dest, *orig;
  44. {
  45.      dest->sFileType = orig->sFileType;
  46.      dest->iPort     = orig->iPort;
  47.      dest->iItemnum  = orig->iItemnum;
  48.  
  49.      strcpy(dest->sTitle, orig->sTitle);
  50.      strcpy(dest->sPath,  orig->sPath);
  51.      strcpy(dest->sHost,  orig->sHost);
  52.  
  53. }
  54.  
  55. /** Compare two GopherStructs ***/
  56.  
  57. int
  58. GScmp(gs1, gs2)
  59.   GopherStruct *gs1, *gs2;
  60. {
  61.  
  62.      return(strcmp(GSgetTitle(gs1), GSgetTitle(gs2)));
  63. }
  64.  
  65.  
  66.  
  67.  
  68. /***********************************************************************
  69. ** Stuff for GopherDirObjs
  70. **
  71. ***********************************************************************/
  72.  
  73. /** This proc adds a Gopherstruct to a gopherdir. **/
  74.  
  75. void
  76. GDaddGS(gd, gs)
  77.   GopherDirObj *gd;
  78.   GopherStruct *gs;
  79. {
  80.      int x;
  81.      int Top;
  82.  
  83.      Top = GDgetTop(gd);
  84.  
  85.      if (Top >= MAXGOPHERS)
  86.       return;
  87.  
  88.      if (DEBUG)
  89.       fprintf(stderr, "Adding %s, Top=%d, Num=%d\n",
  90.           GSgetTitle(gs), Top, GSgetNum(gs));
  91.  
  92.      if ((x = GSgetNum(gs)) != 0) {
  93.       /** someone wants this to be the nth item. **/
  94.       if ((x-1) <= GDgetTop(gd)) {
  95.  
  96.            while (GSgetNum(GDgetEntry(gd, Top)) !=0)
  97.             Top++;
  98.            /* Helps in tracking down "curious" behavior */
  99.            if (DEBUG)
  100.             printf("Swapping entries %d and %d\n", x-1, Top);
  101.            
  102.            /* If our Numb= specifies something above our current "Top"
  103.         * then act appropriately
  104.         */
  105.            if (x-1 > Top)
  106.             GDsetTop(gd, x-1);
  107.  
  108.  
  109.            GScpy(GDgetEntry(gd, Top), GDgetEntry(gd, x-1));
  110.            GDsetTop(gd, ++Top);
  111.       }
  112.       
  113.       GScpy(GDgetEntry(gd, x-1), gs);
  114.       
  115.      } else {
  116.       /*** First make sure a user-ordered object isn't there ***/
  117.  
  118.       while (GSgetNum(GDgetEntry(gd, Top)) !=0)
  119.            Top++;
  120.  
  121.       /*** Now tack it on the end ***/
  122.  
  123.       GScpy(GDgetEntry(gd, Top), gs);
  124.       GDsetTop(gd, ++Top);
  125.      }
  126. }
  127.  
  128.  
  129. void
  130. GDsort(gd)
  131.   GopherDirObj *gd;
  132. {
  133.      int i;
  134.  
  135.      /*** Find first non-numbered entry ***/
  136.  
  137.      for (i=0; ; i++) {
  138.       if (GSgetNum(GDgetEntry(gd, i)) == 0)
  139.            break;
  140.      }
  141.  
  142.      /*** Everything up to i is already sorted by user-defined ordering ***/
  143.  
  144.      if (GDgetTop(gd) <= i)
  145.       /** No more sorting needed ***/
  146.       return;
  147.  
  148.      
  149.      qsort((char *) GDgetEntry(gd, i), gd->Top-i, 
  150.        sizeof(GopherStruct),GScmp);
  151.  
  152. }
  153.  
  154.  
  155. void
  156. GDinit(gd)
  157.   GopherDirObj *gd;
  158. {
  159.      int i;
  160.  
  161.      for (i=0; i<GDgetTop(gd); i++) {
  162.       GSsetType(GDgetEntry(gd, i), '\0');
  163.      }
  164.  
  165.      GDsetTop(gd, 0);
  166. }
  167.  
  168.  
  169.  
  170. GDtoNet(gd, sockfd)
  171.   GopherDirObj *gd;
  172.   int sockfd;
  173. {
  174.      int i;
  175.  
  176.      for (i=0; i<= GDgetTop(gd); i++) {
  177.       GStoNet(GDgetEntry(gd, i), sockfd);
  178.      }      
  179.  
  180.      writestring(sockfd, ".\r\n");
  181. }
  182.  
  183.