home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / utility / dos / txt2raw / txt2raw.c next >
Encoding:
C/C++ Source or Header  |  1993-10-16  |  2.7 KB  |  116 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void add_ext (char *fname, char *ext, int force);
  5.  
  6.  
  7. int main (int argc, char *argv[])
  8. {
  9.     char infile[64] = "", outfile[64] = "";
  10.     char line[256];
  11.     FILE *f, *g;
  12.     int i, n;
  13.     int convert_colours = 0, bad_colours = 0;
  14.     float ax, ay, az, bx, by, bz, cx, cy, cz, red, green, blue;
  15.  
  16.     for (i = 1; i < argc; i++) {
  17.     if (argv[i][0] != '-' && argv[i][0] != '/') {
  18.         if (strlen(infile) == 0) {
  19.         strcpy (infile, argv[i]);
  20.         add_ext (infile, "txt", 0);
  21.         }
  22.         else if (strlen(outfile) == 0) {
  23.         strcpy (outfile, argv[i]);
  24.         add_ext (outfile, "raw", 0);
  25.         }
  26.         else {
  27.         printf ("Too many file names specified\n");
  28.         return 1;
  29.         }
  30.     }
  31.     else {
  32.         if (argv[i][1] == 'c' || argv[i][1] == 'C')
  33.         convert_colours = 1;
  34.     }
  35.     }
  36.  
  37.     if (strlen (infile) == 0) {
  38.     printf ("TXT2RAW Converter by Steve Anger\n");
  39.     printf ("Converts TXT files created by the PV3D modeller to RAW\n");
  40.     printf ("files readable by programs such as RAW2POV\n\n");
  41.     printf ("Usage:   txt2raw inputfile[.txt] [outputfile[.raw]] [options]\n");
  42.     printf ("Options: -c  Convert RGB colour information\n\n");
  43.     return 1;
  44.     }
  45.  
  46.     if (strlen (outfile) == 0) {
  47.     strcpy (outfile, infile);
  48.     add_ext (outfile, "raw", 1);
  49.     }
  50.  
  51.     f = fopen (infile, "r");
  52.     if (f == NULL) {
  53.     printf ("Unable to open input file '%s'\n", infile);
  54.     return 1;
  55.     }
  56.  
  57.     g = fopen (outfile, "w");
  58.     if (g == NULL) {
  59.     printf ("Unable to open output file '%s'\n", outfile);
  60.     return 1;
  61.     }
  62.  
  63.     i = 0;
  64.     while (!feof(f)) {
  65.     if (++i % 10 == 0)
  66.         printf ("%d\r", i);
  67.  
  68.     fgets (line, 256, f);
  69.  
  70.     n = sscanf (line, "%f %f %f %f %f %f %f %f %f %f %f %f", &ax, &ay,
  71.             &az, &bx, &by, &bz, &cx, &cy, &cz, &red, &green, &blue);
  72.  
  73.     if (n != 9 && n != 12)
  74.         continue;
  75.  
  76.     if (n == 12 && convert_colours) {
  77.         fprintf (g, "%f %f %f %f %f %f %f %f %f %f %f %f\n", red, green,
  78.              blue, ax, ay, az, bx, by, bz, cx, cy, cz);
  79.  
  80.         if (red < -0.01 || red > 1.01 || green < -0.01 || green > 1.01 ||
  81.         blue < -0.01 || blue > 1.01)
  82.           bad_colours = 1;
  83.     }
  84.     else {
  85.         fprintf (g, "%f %f %f %f %f %f %f %f %f\n", ax, ay, az,
  86.              bx, by, bz, cx, cy, cz);
  87.     }
  88.     }
  89.  
  90.     printf ("\n");
  91.  
  92.     fclose (f);
  93.     fclose (g);
  94.  
  95.     if (bad_colours) {
  96.     printf ("WARNING: Input file contains one or more illegal colour values.\n");
  97.     printf ("Input file may not be in the correct format\n\n");
  98.     }
  99. }
  100.  
  101.  
  102. void add_ext (char *fname, char *ext, int force)
  103. {
  104.     int i;
  105.  
  106.     for (i = 0; i < strlen(fname); i++)
  107.     if (fname[i] == '.') break;
  108.  
  109.     if (fname[i] == '\0' || force) {
  110.     if (strlen(ext) > 0)
  111.         fname[i++] = '.';
  112.  
  113.     strcpy (&fname[i], ext);
  114.     }
  115. }
  116.