home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / GRAPHICS / MISC / PVQUAN15.ZIP / ANIM.ZIP / MAKEANIM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-06  |  5.9 KB  |  216 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                  Copyright (c) 1992, Frank van der Hulst             *
  4.  *                          All Rights Reserved                         *
  5.  *                                                                      *
  6.  ************************************************************************
  7.  *                                                                      *
  8.  * Authors:                                                             *
  9.  *        FvdH - Frank van der Hulst (Wellington, NZ)                   *
  10.  *                                                                      *
  11.  * Versions:                                                            *
  12.  *    V1.0 911031 FvdH - Released as part of PVQUAN13.ZIP               *
  13.  *    V1.4 920303 FvdH - Ported to gcc (PVQUAN14.ZIP)                   *
  14.  *    V1.5 920401 FvdH - Improved usage                                 *
  15.  *             920406 FvdH - Fixed bug which created files with leading     *
  16.  *                                 spaces in their names                          *
  17.  *                                                                      *
  18.  ************************************************************************/
  19.  
  20. /*
  21.     This program reads an animation source file (*.ANM), and uses it to
  22.     generate data files for the PoV ray tracer. An initial data file
  23.     must also be given.
  24.  
  25.     Command format:
  26.          MAKEANIM data animation
  27.  
  28.     The animation file has the following format:
  29.     Line 1: name1  [,name2]...
  30.     Line 2: value1 [,value2]...
  31.  
  32.     name1, name2, etc. are the names of items #declared in the initial data
  33.     file, separated by commas. The names must be in the same order as they
  34.     are #defined in the data file.
  35.  
  36.     value1, value2, etc. are the values to be assigned to those #defined
  37.     items, separated by commas.
  38.     Each line represents one screen.
  39.  
  40.     This program reads the initial data file, searching for the lines where
  41.     the items named in the animation file are defined.
  42.  
  43.     Data files are then generated, replacing the lines containing the
  44.     #defines identified with new values.
  45. */
  46.  
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50.  
  51. #define FALSE 0
  52. #define TRUE 1
  53.  
  54. #ifdef __GNUC__
  55. #define SEEK_SET 0
  56. #define cdecl
  57. #endif
  58.  
  59. #define MAX_ITEMS 20
  60.  
  61. struct ITEM {
  62.     char        name[256];
  63.     int        line;
  64. } ;
  65.  
  66. struct ITEM item[MAX_ITEMS];
  67. int num_items;
  68.  
  69. FILE *anim_file, *data_file;
  70.  
  71. #ifndef __TURBOC__
  72. void strlwr(char *s)
  73. {
  74.     while (*s) {
  75.         *s = tolower(*s);
  76.         s++;
  77.     }
  78. }
  79. #endif
  80.  
  81. int get_field_names(char *field_buff, struct ITEM *field)
  82. {
  83.     int i;
  84.     char *start = field_buff;
  85.     char *dest;
  86.  
  87.     strlwr(field_buff);
  88.     for (i = 0; i < MAX_ITEMS; i++) {
  89.         dest = field[i].name;
  90.         while (*start == ' ' || *start == '\t' && *start != '\n') start++;
  91.         while (*start != ',' && *start != '\n') {
  92.             if (*start != ' ' && *start != '\t')    *dest++ = *start;
  93.             start++;
  94.         }
  95.         if (*start == '\n') break;
  96.         start++;
  97.     }
  98.     return i+1;
  99. }
  100.  
  101. void split_fields(char *anim_buff, char name[256][MAX_ITEMS], int num_items)
  102. {
  103.     int i;
  104.     char *end;
  105.  
  106.     for (i = 0; i < num_items; i++) {
  107.         end = strpbrk(anim_buff, ",\n");
  108.         *end = '\0';
  109.         strcpy(name[i], anim_buff);
  110.         anim_buff = end + 1;
  111.     }
  112. }
  113.  
  114. void scan_data_file(struct ITEM *field, int num_items)
  115. {
  116.     char data_buff[256];
  117.     char *name;
  118.     char *end;
  119.     int line, i;
  120.  
  121.     for (line = 0; !feof(data_file); line++) {
  122.         fgets(data_buff, sizeof(data_buff), data_file);
  123.         strlwr(data_buff);
  124.         if (strncmp("#declare", data_buff, 8) != 0) continue;
  125.         name = data_buff+8;
  126.         while (*name == ' ' || *name == '\t') name++;
  127.         end = name;
  128.         while (*end != ' ' && *end != '\t') end++;
  129.         *end = '\0';
  130.         for (i = 0; i < num_items; i++)
  131.             if (strcmp(name, field[i].name) == 0) {
  132.                 field[i].line = line;
  133.                 break;
  134.             }
  135.     }
  136. }
  137.  
  138. void generate_file(char *fname, struct ITEM *field, int num_fields, char *anim_line)
  139. {
  140.     int line, i;
  141.     FILE *out;
  142.     static char value[256][MAX_ITEMS];
  143.     char data_buff[256];
  144.  
  145.     fseek(data_file, 0L, SEEK_SET);
  146.     if ((out = fopen(fname, "wt")) == NULL) {
  147.         printf("Couldn't open %s\n", fname);
  148.         exit(1);
  149.     }
  150.  
  151.     line = 0;
  152.     split_fields(anim_line, value, num_fields);
  153.     for (i = 0; i < num_fields; i++) {
  154.         for ( ; line < field[i].line; line++) {
  155.             fgets(data_buff, sizeof(data_buff), data_file);
  156.             fputs(data_buff, out);
  157.         }
  158.         fgets(data_buff, sizeof(data_buff), data_file);
  159.         fprintf(out, "#declare %s = %s\n", field[i].name, value[i]);
  160.         line++;
  161.     }
  162.     fgets(data_buff, sizeof(data_buff), data_file);
  163.     while (!feof(data_file)) {
  164.         fputs(data_buff, out);
  165.         fgets(data_buff, sizeof(data_buff), data_file);
  166.     }
  167.     fclose(out);
  168. }
  169.  
  170. void cdecl main(int argc, char *argv[])
  171. {
  172.     char fname[256];
  173.     char anim_line[256];
  174.     int file_no;
  175.  
  176.     printf("MAKEANIM v1.50 -- Create a series of PVRay data files.\n");
  177.     printf("By F van der Hulst. Copyright 1992\n\n");
  178.  
  179.     if (argc != 3) {
  180.         printf("Usage: %s PVRay_data_file Animation_script_file\n", argv[0]);
  181.         exit(1);
  182.     }
  183.  
  184.     strcpy(fname, argv[1]);
  185.     strcat(fname, ".dat");
  186.     if ((data_file = fopen(fname, "rt")) == NULL) {
  187.         printf("Couldn't open %s\n", fname);
  188.         exit(1);
  189.     }
  190.  
  191.     strcpy(fname, argv[2]);
  192.     strcat(fname, ".anm");
  193.     if ((anim_file = fopen(fname, "rt")) == NULL) {
  194.         printf("Couldn't open %s\n", fname);
  195.         exit(1);
  196.     }
  197.  
  198.     fgets(anim_line, sizeof(anim_line), anim_file);
  199.     num_items = get_field_names(anim_line, item);
  200.     if (num_items == 0) {
  201.         printf("No items specified\n");
  202.         exit(1);
  203.     }
  204.  
  205.     printf("%12.12s  %65.65s\n", " ", anim_line);
  206.     scan_data_file(item, num_items);
  207.  
  208.     fgets(anim_line, sizeof(anim_line), anim_file);
  209.     for (file_no = 0; !feof(anim_file); file_no++) {
  210.         sprintf(fname, "%s_%d.dat", argv[2], file_no);
  211.         printf("%12.12s: %65.65s\n", fname, anim_line);
  212.         generate_file(fname, item, num_items, anim_line);
  213.         fgets(anim_line, sizeof(anim_line), anim_file);
  214.     }
  215. }
  216.