home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / guicast / replace.C < prev    next >
C/C++ Source or Header  |  2000-11-29  |  2KB  |  112 lines

  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #include "defaults.h"
  7. #include "guicast.h"
  8.  
  9.  
  10. class MWindow : public BC_Window
  11. {
  12. public:
  13.     MWindow() : BC_Window("Replace", INFINITY, INFINITY, 320, 200, 0, 0)
  14.     {
  15.     }
  16.     
  17.     int create_objects(char *input, char *output)
  18.     {
  19.         int x = 10, y = 10;
  20.         
  21.         add_subwindow(title = new BC_Title(x, y, "String to replace:"));
  22.         y += title->get_h() + 10;
  23.         add_subwindow(input_text = new BC_TextBox(x, y, 200, 1, input, 1));
  24.         y += input_text->get_h() + 10;
  25.         add_subwindow(title = new BC_Title(x, y, "Replacement string:"));
  26.         y += title->get_h() + 10;
  27.         add_subwindow(output_text = new BC_TextBox(x, y, 200, 1, output, 1));
  28.         y += output_text->get_h() + 10;
  29.         add_subwindow(new BC_OKButton(x, y));
  30.         x = get_w() - 100;
  31.         add_subwindow(new BC_CancelButton(x, y));
  32.     }
  33.     
  34.     BC_Title *title;
  35.     BC_TextBox *input_text, *output_text;
  36. };
  37.  
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.     char input[1024], output[1024];
  42.     int result;
  43.     Defaults defaults("~/.replacerc");
  44.  
  45.     input[0] = 0;
  46.     output[0] = 0;
  47.     defaults.load();
  48.     defaults.get("INPUT", input);
  49.     defaults.get("OUTPUT", output);
  50.     MWindow window;
  51.     window.create_objects(input, output);
  52.     result = window.run_window();
  53.  
  54.     if(result == 0)
  55.     {
  56.         strcpy(input, window.input_text->get_text());
  57.         strcpy(output, window.output_text->get_text());
  58.         while(--argc > 0)
  59.         {
  60.             FILE *file;
  61.             char *buffer, *ptr1, *ptr2;
  62.             long len;
  63.  
  64.             if(!(file = fopen(argv[argc], "r")))
  65.             {
  66.                 printf("open %s for reading: %s", argv[argc], strerror(errno));
  67.                 continue;
  68.             }
  69.  
  70.             fseek(file, 0, SEEK_END);
  71.             len = ftell(file);
  72.             fseek(file, 0, SEEK_SET);
  73.  
  74.             buffer = (char*)malloc(len);
  75.             fread(buffer, 1, len, file);
  76.             fclose(file);
  77.  
  78.             if(!(file = fopen(argv[argc], "w")))
  79.             {
  80.                 printf("open %s for writing: %s", argv[argc], strerror(errno));
  81.                 continue;
  82.             }
  83.  
  84.             ptr1 = buffer;
  85.             do
  86.             {
  87.                 ptr2 = strstr(ptr1, input);
  88.  
  89.                 if(ptr2)
  90.                 {
  91.                     while(ptr1 < ptr2)
  92.                         fputc(*ptr1++, file);
  93.  
  94.                     ptr1 += strlen(input);
  95.                     fprintf(file, "%s", output);
  96.                 }
  97.                 else
  98.                     while(ptr1 < buffer + len)
  99.                         fputc(*ptr1++, file);
  100.             }while(ptr2);
  101.         }
  102.         printf("Replaced ""%s"" with ""%s"".\n", input, output);
  103.     }
  104.     else
  105.         printf("Cancelled.\n");
  106.  
  107.     defaults.update("INPUT", input);
  108.     defaults.update("OUTPUT", output);
  109.     defaults.save();
  110.  
  111. }
  112.