home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / ttddd / code / set_text.c < prev    next >
C/C++ Source or Header  |  1994-06-20  |  3KB  |  135 lines

  1. /* set_texture_path.c - (re)set the filename path for all textures
  2.  *                    - written by Glenn M. Lewis - 1/31/93
  3.  */
  4.  
  5. static char rcs_id[] = "$Id: set_texture_path.c,v 1.3 1993/02/14 17:44:16 glewis Exp glewis $";
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "t3dlib.h"
  10. #ifdef __STDC__
  11. #include <stdlib.h>
  12. #include <strings.h>
  13. #include "set_texture_path_protos.h"
  14. #endif
  15.  
  16. char newpath[128];
  17. int newlen;
  18. char strin[160];
  19.  
  20. void change_name(s)
  21. char *s;
  22. {
  23.     register int i;
  24.     register char *p;
  25.     
  26.     if (!*s || !isalpha(*s)) return;
  27.  
  28.     /* First, search for colon in path name */
  29.     for (p=s,i=0; *p && *p!=':' && i<80; i++,p++) ;
  30.     strcpy(strin, newpath);
  31.     if (*p==':') {
  32.         /* Strip off previous path and use new path */
  33.         strncpy(&strin[newlen], &p[1], 80);
  34.     } else {
  35.         /* Simply add new path to beginning of string */
  36.         strncpy(&strin[newlen], s, 80);
  37.     }
  38.     strncpy(s, strin, 80);
  39.     s[80] = '\0';
  40. }
  41.  
  42. void change_obj(obj)
  43. register OBJECT *obj;
  44. {
  45.     OBJECT *child;
  46.     DESC *d;
  47.     int i;
  48.  
  49.     if (!obj) return;
  50.     if (!obj->desc && !obj->child) return;    /* Not an object */    
  51.  
  52.     if (d=obj->desc) {
  53.         for (i=0; i<4; i++)
  54.             if (d->txt2[i])
  55.                 change_name(&d->txt2[i]->Name[0]);
  56.     }
  57.  
  58.     for (child=obj->child; child; child=child->next)
  59.         change_obj(child);
  60. }
  61.  
  62. main(argc, argv)
  63. int argc;
  64. char *argv[];
  65. {
  66.     char filename[256], rootname[256], *c1, *c2;
  67.     int i;
  68.     WORLD *world;
  69.     FILE *inp, *out;
  70.     OBJECT *p;
  71.  
  72.     newpath[0] = '\0';
  73.     rootname[0] = filename[0] = '\0';
  74. /*    strcpy(rootname, "model");    ** The default for reading stdin */
  75.     for (i=1; i<argc; i++) {
  76.         if (argv[i][0] == '-') {
  77.             switch(argv[i][1]) {
  78.                 case 'h':
  79.                 default:
  80. USAGE:
  81.                     fprintf(stderr, "Usage: %s <newpath:> [infile] [outfile]\n", argv[0]);
  82.                     exit(-1);
  83.             }
  84.         } else if (!newpath[0]) {
  85.             strcpy(newpath, argv[i]);
  86.             newlen = strlen(newpath);
  87.             if (newpath[newlen-1]!=':' && newpath[newlen-1]!='/') {
  88.                 /* Add either a slash or a colon to end of newpath... */
  89.                 for (c1=newpath; *c1; c1++) {
  90.                     if (*c1==':') {
  91.                         /* Add slash to end */
  92.                         newpath[newlen++] = '/';
  93.                         break;
  94.                     }
  95.                 }
  96.                 if (newpath[newlen-1]!='/') newpath[newlen++] = ':';
  97.             }
  98.         } else if (filename[0]) {
  99.             strcpy(rootname, argv[i]);
  100.         } else if (!rootname[0]) {
  101.             strcpy(filename, argv[i]);    /* Make root of filename the default */
  102.             for (c1=rootname,c2=argv[i]; (*c1 = *c2++) && *c1!='.'; c1++) ;
  103.             *c1 = '\0';
  104.             strcat(rootname, ".newiob");
  105.         } else goto USAGE;
  106.     }
  107.  
  108.     if (!filename[0]) inp = stdin;
  109.     else if (!(inp = fopen(filename, "r"))) {
  110.         fprintf(stderr, "Can't open '%s' for input.\n", filename);
  111.         exit(-1);
  112.     }
  113.     if (!rootname[0]) out = stdout;
  114.     else if (!(out = fopen(rootname, "w"))) {
  115.         fprintf(stderr, "Can't open '%s' for output.\n", rootname);
  116.         exit(-1);
  117.     }
  118.  
  119.     world = read_World(inp);    /* Parse it all */
  120.  
  121.     /* Now perform all the texture name changes... */
  122.     /* Handle old stuff first (TS & pre-Imagine2.0) */
  123.     if (world->info) {
  124.         for (i=0; i<8; i++)
  125.             change_name(&world->info->txtr[i][0]);
  126.     }
  127.     /* Now take care of Imagine 2.0... */
  128.     for (p=world->object; p; p=p->next)
  129.         change_obj(p);
  130.  
  131.     write_TDDD(world, out);
  132.     free_World(world);
  133.     exit(0);
  134. }
  135.