home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / fonts / lib / fs / FSFontInfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  8.6 KB  |  298 lines

  1. /* $XConsortium: FSFontInfo.c,v 1.4 92/05/13 15:41:58 gildea Exp $ */
  2. /*
  3.  * Copyright 1990 Network Computing Devices;
  4.  * Portions Copyright 1987 by Digital Equipment Corporation and the
  5.  * Massachusetts Institute of Technology
  6.  *
  7.  * Permission to use, copy, modify, and distribute this protoype software
  8.  * and its documentation to Members and Affiliates of the MIT X Consortium
  9.  * any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies and that both that
  11.  * copyright notice and this permission notice appear in supporting
  12.  * documentation, and that the names of Network Computing Devices, Digital or
  13.  * MIT not be used in advertising or publicity pertaining to distribution of
  14.  * the software without specific, written prior permission.
  15.  *
  16.  * NETWORK COMPUTING DEVICES, DIGITAL AND MIT DISCLAIM ALL WARRANTIES WITH
  17.  * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  18.  * AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES, DIGITAL OR MIT BE
  19.  * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  20.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  21.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  22.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include    "FSlibint.h"
  26.  
  27. char      **
  28. FSListFontsWithXInfo(svr, pattern, maxNames, count, info, pprops, offsets, prop_data)
  29.     FSServer   *svr;
  30.     char       *pattern;
  31.     int         maxNames;
  32.     int        *count;
  33.     fsFontHeader ***info;
  34.     fsPropInfo ***pprops;
  35.     fsPropOffset ***offsets;
  36.     unsigned char ***prop_data;
  37. {
  38.     long        nbytes;
  39.     int         i,
  40.                 j;
  41.     int         size = 0;
  42.     fsFontHeader **fhdr = (fsFontHeader **) 0;
  43.     fsPropInfo **pi = (fsPropInfo **) 0;
  44.     fsPropOffset **po = (fsPropOffset **) 0;
  45.     unsigned char **pd = (unsigned char **) 0;
  46.     char      **flist = NULL;
  47.     fsListFontsWithXInfoReply reply;
  48.     fsListFontsWithXInfoReq *req;
  49.     Status status;
  50.  
  51.     GetReq(ListFontsWithXInfo, req);
  52.     req->maxNames = maxNames;
  53.     nbytes = req->nbytes = pattern ? strlen(pattern) : 0;
  54.     req->length += (nbytes + 3) >> 2;
  55.     _FSSend(svr, pattern, nbytes);
  56.  
  57.     for (i = 0;; i++) {
  58.     if (FSProtocolVersion(svr) > 1)
  59.     {
  60.         status = _FSReply(svr, (fsReply *) &reply, 0, fsFalse);
  61.         if (status != 0  &&  reply.nameLength == 0)    /* got last reply */
  62.         break;
  63.         if (status)
  64.         _FSRead(svr, (char *) &reply.nReplies,
  65.             SIZEOF(fsListFontsWithXInfoReply) -
  66.             SIZEOF(fsGenericReply));
  67.     } else {
  68.         status = _FSReply(svr, (fsReply *) & reply,
  69.                   ((SIZEOF(fsListFontsWithXInfoReply) -
  70.                 SIZEOF(fsGenericReply)) >> 2), fsFalse);
  71.     }
  72.     if (!status) {
  73.         for (j = (i - 1); j >= 0; j--) {
  74.         FSfree((char *) fhdr[j]);
  75.         FSfree((char *) pi[j]);
  76.         FSfree((char *) po[j]);
  77.         FSfree((char *) pd[j]);
  78.         FSfree(flist[j]);
  79.         }
  80.         if (flist)
  81.         FSfree((char *) flist);
  82.         if (fhdr)
  83.         FSfree((char *) fhdr);
  84.         if (pi)
  85.         FSfree((char *) pi);
  86.         if (po)
  87.         FSfree((char *) po);
  88.         if (pd)
  89.         FSfree((char *) pd);
  90.  
  91.         SyncHandle();
  92.         return (char **) NULL;
  93.     }
  94.     if (reply.nameLength == 0)    /* got last reply in version 1 */
  95.         break;
  96.     if ((i + reply.nReplies) >= size) {
  97.         size = i + reply.nReplies + 1;
  98.  
  99.         if (fhdr) {
  100.         fsFontHeader **tmp_fhdr = (fsFontHeader **)
  101.         FSrealloc((char *) fhdr,
  102.               (unsigned) (sizeof(fsFontHeader *) * size));
  103.         char      **tmp_flist = (char **) FSrealloc((char *) flist,
  104.                      (unsigned) (sizeof(char *) * size));
  105.         fsPropInfo **tmp_pi = (fsPropInfo **)
  106.         FSrealloc((char *) pi,
  107.               (unsigned) (sizeof(fsPropInfo *) * size));
  108.         fsPropOffset **tmp_po = (fsPropOffset **)
  109.         FSrealloc((char *) po,
  110.               (unsigned) (sizeof(fsPropOffset *) * size));
  111.         unsigned char **tmp_pd = (unsigned char **)
  112.         FSrealloc((char *) pd,
  113.               (unsigned) (sizeof(unsigned char *) * size));
  114.  
  115.         if (!tmp_fhdr || !tmp_flist || !tmp_pi || !tmp_po || !tmp_pd) {
  116.             for (j = (i - 1); j >= 0; j--) {
  117.             FSfree((char *) flist[j]);
  118.             FSfree((char *) fhdr[j]);
  119.             FSfree((char *) pi[j]);
  120.             FSfree((char *) po[j]);
  121.             FSfree((char *) pd[j]);
  122.             }
  123.             if (tmp_flist)
  124.             FSfree((char *) tmp_flist);
  125.             else
  126.             FSfree((char *) flist);
  127.             if (tmp_fhdr)
  128.             FSfree((char *) tmp_fhdr);
  129.             else
  130.             FSfree((char *) fhdr);
  131.             if (tmp_pi)
  132.             FSfree((char *) tmp_pi);
  133.             else
  134.             FSfree((char *) pi);
  135.             if (tmp_po)
  136.             FSfree((char *) tmp_po);
  137.             else
  138.             FSfree((char *) po);
  139.             if (tmp_pd)
  140.             FSfree((char *) tmp_pd);
  141.             else
  142.             FSfree((char *) pd);
  143.             goto clearwire;
  144.         }
  145.         fhdr = tmp_fhdr;
  146.         flist = tmp_flist;
  147.         pi = tmp_pi;
  148.         po = tmp_po;
  149.         pd = tmp_pd;
  150.         } else {
  151.         if (!(fhdr = (fsFontHeader **)
  152.               FSmalloc((unsigned) (sizeof(fsFontHeader *) * size))))
  153.             goto clearwire;
  154.         if (!(flist = (char **)
  155.               FSmalloc((unsigned) (sizeof(char *) * size)))) {
  156.             FSfree((char *) fhdr);
  157.             goto clearwire;
  158.         }
  159.         if (!(pi = (fsPropInfo **)
  160.               FSmalloc((unsigned) (sizeof(fsPropInfo *) * size)))) {
  161.             FSfree((char *) fhdr);
  162.             FSfree((char *) flist);
  163.             goto clearwire;
  164.         }
  165.         if (!(po = (fsPropOffset **)
  166.               FSmalloc((unsigned) (sizeof(fsPropOffset *) * size)))) {
  167.             FSfree((char *) fhdr);
  168.             FSfree((char *) flist);
  169.             FSfree((char *) pi);
  170.             goto clearwire;
  171.         }
  172.         if (!(pd = (unsigned char **)
  173.             FSmalloc((unsigned) (sizeof(unsigned char *) * size)))) {
  174.             FSfree((char *) fhdr);
  175.             FSfree((char *) flist);
  176.             FSfree((char *) pi);
  177.             FSfree((char *) po);
  178.             goto clearwire;
  179.         }
  180.         }
  181.     }
  182.     fhdr[i] = (fsFontHeader *) FSmalloc(sizeof(fsFontHeader));
  183.     if (!fhdr[i]) {
  184.         goto badmem;
  185.     }
  186.     bcopy((char *) &reply.header, (char *) fhdr[i], sizeof(fsFontHeader));
  187.     if (FSProtocolVersion(svr) == 1)
  188.     {
  189.         fhdr[i]->char_range.min_char.high = reply.header.char_range.min_char.low;
  190.         fhdr[i]->char_range.min_char.low = reply.header.char_range.min_char.high;
  191.         fhdr[i]->char_range.max_char.high = reply.header.char_range.max_char.low;
  192.         fhdr[i]->char_range.max_char.low = reply.header.char_range.max_char.high;
  193.         fhdr[i]->default_char.high = reply.header.default_char.low;
  194.         fhdr[i]->default_char.low = reply.header.default_char.high;
  195.     }
  196.  
  197.     /* alloc space for the name */
  198.     flist[i] = (char *) FSmalloc((unsigned int) (reply.nameLength + 1));
  199.     if (FSProtocolVersion(svr) == 1)
  200.     {
  201.         /* get the name */
  202.         if (!flist[i]) {
  203.         nbytes = reply.nameLength + 3 & ~3;
  204.         _FSEatData(svr, (unsigned long) nbytes);
  205.         goto badmem;
  206.         }
  207.         _FSReadPad(svr, flist[i], (long) reply.nameLength);
  208.         flist[i][reply.nameLength] = '\0';
  209.     }
  210.  
  211.     pi[i] = (fsPropInfo *) FSmalloc(sizeof(fsPropInfo));
  212.     if (!pi[i]) {
  213.         FSfree((char *) fhdr[i]);
  214.         goto badmem;
  215.     }
  216.     _FSReadPad(svr, (char *) pi[i], sizeof(fsPropInfo));
  217.  
  218.     po[i] = (fsPropOffset *)
  219.         FSmalloc(pi[i]->num_offsets * sizeof(fsPropOffset));
  220.     if (!po[i]) {
  221.         FSfree((char *) fhdr[i]);
  222.         FSfree((char *) pi[i]);
  223.         goto badmem;
  224.     }
  225.     pd[i] = (unsigned char *) FSmalloc(pi[i]->data_len);
  226.     if (!pd[i]) {
  227.         FSfree((char *) fhdr[i]);
  228.         FSfree((char *) pi[i]);
  229.         FSfree((char *) po[i]);
  230.         goto badmem;
  231.     }
  232.     /* get offsets */
  233.     _FSReadPad(svr, (char *) po[i],
  234.            (pi[i]->num_offsets * sizeof(fsPropOffset)));
  235.     /* get prop data */
  236.     if (FSProtocolVersion(svr) == 1)
  237.         _FSReadPad(svr, (char *) pd[i], pi[i]->data_len);
  238.     else
  239.         _FSRead(svr, (char *) pd[i], pi[i]->data_len);
  240.  
  241.     if (FSProtocolVersion(svr) != 1)
  242.     {
  243.         /* get the name */
  244.         if (!flist[i]) {
  245.         nbytes = reply.nameLength + 3 & ~3;
  246.         _FSEatData(svr, (unsigned long) nbytes);
  247.         goto badmem;
  248.         }
  249.         _FSRead(svr, flist[i], (long) reply.nameLength);
  250.         flist[i][reply.nameLength] = '\0';
  251.  
  252.         nbytes = pi[i]->data_len + reply.nameLength;
  253.         _FSEatData(svr, (unsigned long) (((nbytes+3)&~3) - nbytes));
  254.     }
  255.     }
  256.     *info = fhdr;
  257.     *count = i;
  258.     *pprops = pi;
  259.     *offsets = po;
  260.     *prop_data = pd;
  261.     SyncHandle();
  262.     return flist;
  263.  
  264. badmem:
  265.     for (j = (i - 1); j >= 0; j--) {
  266.     FSfree((char *) pi[j]);
  267.     FSfree((char *) po[j]);
  268.     FSfree((char *) pd[j]);
  269.     FSfree(flist[j]);
  270.     FSfree((char *) fhdr[j]);
  271.     }
  272.     if (flist)
  273.     FSfree((char *) flist);
  274.     if (fhdr)
  275.     FSfree((char *) fhdr);
  276.     if (pi)
  277.     FSfree((char *) pi);
  278.     if (po)
  279.     FSfree((char *) po);
  280.     if (pd)
  281.     FSfree((char *) pd);
  282.  
  283.  
  284. clearwire:
  285.     do {
  286.     fsPropInfo  ti;
  287.  
  288.     _FSEatData(svr, (reply.nameLength + 3) & ~3);
  289.     _FSReadPad(svr, (char *) &ti, sizeof(fsPropInfo));
  290.     _FSEatData(svr, (sizeof(fsPropOffset) * ti.num_offsets));
  291.     _FSEatData(svr, ti.data_len);
  292.     } while (_FSReply(svr, (fsReply *) & reply,
  293.               ((SIZEOF(fsListFontsWithXInfoReply)
  294.        - SIZEOF(fsGenericReply)) >> 2), fsFalse) && (reply.nameLength != 0));
  295.     SyncHandle();
  296.     return (char **) NULL;
  297. }
  298.