home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / tracker-4.13.lha / tracker / split.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  3.1 KB  |  166 lines

  1. /* split.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5.  
  6. /* $Id: split.c,v 4.8 1995/02/08 13:14:56 espie Exp $
  7.  * $Log: split.c,v $
  8.  * Revision 4.8  1995/02/08  13:14:56  espie
  9.  * *** empty log message ***
  10.  *
  11.  * Revision 4.8  1995/02/08  13:14:56  espie
  12.  * *** empty log message ***
  13.  *
  14.  * Revision 4.7  1995/02/01  20:41:45  espie
  15.  * Added color.
  16.  *
  17.  * Revision 4.7  1995/02/01  20:41:45  espie
  18.  * Added color.
  19.  *
  20.  * Revision 4.6  1995/02/01  16:39:04  espie
  21.  * *** empty log message ***
  22.  *
  23.  * Revision 4.6  1995/02/01  16:39:04  espie
  24.  * *** empty log message ***
  25.  *
  26.  * Revision 4.0  1994/01/11  17:56:24  espie
  27.  * Cleaner.
  28.  * run_in_fg() and create_notes_table() not needed !
  29.  * Use new pref scheme.
  30.  */
  31.      
  32.  
  33. #include <signal.h>
  34.      
  35. #include "defs.h"
  36. #include "song.h"
  37. #include "extern.h"
  38.      
  39. #include "getopt.h"
  40.      
  41. ID("$Id: split.c,v 4.8 1995/02/08 13:14:56 espie Exp $")
  42.  
  43. /* global variable to catch various types of errors
  44.  * and achieve the desired flow of control
  45.  */
  46. int error;
  47.  
  48. LOCAL struct song *do_scan_song(name, type)
  49. char *name;
  50. int type;
  51.    {
  52.    struct song *song;
  53.    struct exfile *file;
  54.  
  55.    file = open_file(name, "r", getenv("MODPATH"));
  56.    if (!file)
  57.       return NULL;
  58.    song = read_song(file, type); 
  59.    close_file(file);
  60.    return song;
  61.    }
  62.  
  63. #define CHUNK_SIZE 32000
  64.  
  65. LOCAL char *suffix[] =
  66.    {
  67.    "lzh", "lha", "Z", "z", "shn", "zoo", 0
  68.    };
  69.  
  70. LOCAL void truncate(name)
  71. char *name;
  72.    {
  73.    int i;
  74.    int last_point = 0;
  75.  
  76.    for (i = 0; name[i]; i++)
  77.       {
  78.       if (name[i] == '.')
  79.          last_point = i + 1;
  80.       }
  81.    if (last_point)
  82.       {
  83.       for (i = 0; suffix[i]; i++)
  84.       if (strcmp(name + last_point, suffix[i]) == 0)
  85.          {
  86.          name[last_point - 1] = 0;
  87.          return;
  88.          }
  89.       }
  90.    }
  91.  
  92.    
  93.    
  94. void split_module(name, cutpoint)
  95. char *name;
  96. long cutpoint;
  97.    {
  98.    char buffer[300];
  99.    FILE *mod;
  100.    FILE *samp;
  101.    struct exfile *file;
  102.    char *copy_buff;
  103.    int chunk;
  104.  
  105.    file = open_file(name, "r", getenv("MODPATH"));
  106.    truncate(name);
  107.    sprintf(buffer, "%s.mod", name);
  108.    mod = fopen(buffer, "w");
  109.    if (!mod)
  110.       exit(10);
  111.    sprintf(buffer, "%s.samp", name);
  112.    samp = fopen(buffer, "w");
  113.    if (!samp)
  114.       exit(10);
  115.    copy_buff = malloc(CHUNK_SIZE);
  116.    if (!copy_buff)
  117.       exit(10);
  118.    while(cutpoint >= CHUNK_SIZE)
  119.       {
  120.         read_file(copy_buff, 1, CHUNK_SIZE, file);
  121.       fwrite(copy_buff, 1, CHUNK_SIZE, mod);
  122.       cutpoint -= CHUNK_SIZE;
  123.       }
  124.    if (cutpoint > 0)
  125.       {
  126.       read_file(copy_buff, 1, cutpoint, file);
  127.       fwrite(copy_buff, 1, cutpoint, mod);
  128.       }
  129.    fclose(mod);
  130.    while ((chunk = read_file(copy_buff, 1, CHUNK_SIZE, file)) > 0)
  131.       fwrite(copy_buff, 1, chunk, samp);
  132.    fclose(samp);
  133.    close_file(file);
  134.    }
  135.       
  136.    
  137.  
  138.  
  139. int main(argc, argv)
  140. int argc;
  141. char **argv;
  142.    {
  143.    struct song *song;
  144.    int i;
  145.    int default_type;
  146.  
  147.  
  148.    default_type = BOTH;
  149.  
  150.    for (i = 1; i < argc; i++)
  151.       {
  152.       song = do_scan_song(argv[i], NEW);
  153.       if (!song && error != NEXT_SONG)
  154.          song = do_scan_song(argv[i], OLD);
  155.       if (song)
  156.          {
  157.          dump_song(song); 
  158.          split_module(argv[i], song->samples_start);
  159.          release_song(song);
  160.          }
  161.       }
  162.    return 0;
  163.    }
  164.  
  165.  
  166.