Enlightenment CVS committal

Author  : gilbertt
Project : misc
Module  : gozer

Dir     : misc/gozer/src


Modified Files:
        Makefile.am gozer.h imlib.c main.c options.c 
Added Files:
        gozer.c libgozer.h 
Removed Files:
        options.h structs.h 


Log Message:

Sat Jan 18 18:13:51 GMT 2003  Tom Gilbert <[EMAIL PROTECTED]>

  * Patch from Jonas Jensen <[EMAIL PROTECTED]> which makes the gozer functionality
    usable as a library. You can now just include libgozer.h and link against
    gozer.o, util.o and options.o if you want to use gozer in your own
    programs.


===================================================================
RCS file: /cvsroot/enlightenment/misc/gozer/src/Makefile.am,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -3 -r1.1.1.1 -r1.2
--- Makefile.am 27 Jan 2001 00:22:51 -0000      1.1.1.1
+++ Makefile.am 18 Jan 2003 18:15:08 -0000      1.2
@@ -10,6 +10,6 @@
 LIBOBJS = @LIBOBJS@
 
 bin_PROGRAMS      = gozer
-gozer_SOURCES       = main.c getopt.c getopt1.c getopt.h gozer.h \
-options.c options.h debug.h utils.c utils.h imlib.c structs.h
+gozer_SOURCES       = main.c gozer.c getopt.c getopt1.c getopt.h gozer.h \
+options.c debug.h utils.c utils.h imlib.c
 gozer_LDADD         = @GIBLIB_LIBS@
===================================================================
RCS file: /cvsroot/enlightenment/misc/gozer/src/gozer.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -3 -r1.2 -r1.3
--- gozer.h     8 Aug 2001 20:39:02 -0000       1.2
+++ gozer.h     18 Jan 2003 18:15:09 -0000      1.3
@@ -39,11 +39,13 @@
 #include <stdarg.h>
 #include <giblib/giblib.h>
 
-#include "config.h"
-#include "structs.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 #include "utils.h"
 #include "getopt.h"
 #include "debug.h"
+#include "libgozer.h"
 
 #ifndef __GNUC__
 # define __attribute__(x)
@@ -51,14 +53,6 @@
 
 #define PIPE_BUF_MAX 10240
 
-void show_usage(void);
-void show_version(void);
 void init_imlib(void);
-void show_mini_usage(void);
-gib_list *gozer_calculate_lines(Imlib_Font fn, gib_style *style);
-char *gozer_read_file(char *filename);
-char *gozer_read_pipe();
-
-void gozer_stream_file(char *file, int headers);
 
 #endif
===================================================================
RCS file: /cvsroot/enlightenment/misc/gozer/src/imlib.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -3 -r1.1.1.1 -r1.2
--- imlib.c     27 Jan 2001 00:23:05 -0000      1.1.1.1
+++ imlib.c     18 Jan 2003 18:15:09 -0000      1.2
@@ -24,7 +24,6 @@
 */
 
 #include "gozer.h"
-#include "options.h"
 
 void
 init_imlib(void)
===================================================================
RCS file: /cvsroot/enlightenment/misc/gozer/src/main.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -3 -r1.6 -r1.7
--- main.c      4 Dec 2002 12:33:44 -0000       1.6
+++ main.c      18 Jan 2003 18:15:09 -0000      1.7
@@ -24,21 +24,25 @@
 */
 
 #include "gozer.h"
-#include "options.h"
+
+static void gozer_parse_option_array(int argc, char **argv, gozeroptions *opt);
+static void gozer_parse_options_internal(gozeroptions *opt);
+static void init_parse_options(int argc, char **argv, gozeroptions *opt);
+static void show_usage(void);
+static void show_version(void);
+static void show_mini_usage(void);
+static void gozer_stream_file(char *file, int headers);
+static char *gozer_read_file(char *filename);
+static char *gozer_read_pipe();
 
 int
 main(int argc, char **argv)
 {
-   Imlib_Font fn;
-   Imlib_Image image, bg_image = NULL;
+   Imlib_Image image;
    Imlib_Load_Error err;
-   gib_list *lines, *l, *ll, *words;
-   int w = 0, h = 0, ww, hh, x = 0, y = 0, bgw = 0, bgh = 0;
-   DATA8 atab[256];
-   gib_style *style = NULL;
-   char *p, *pp;
+   gozeroptions opt;
 
-   init_parse_options(argc, argv);
+   init_parse_options(argc, argv, &opt);
 
    init_imlib();
 
@@ -47,6 +51,7 @@
       weprintf("no output file specified");
       show_mini_usage();
    }
+
    if (!opt.text && opt.textfile)
       opt.text = gozer_read_file(opt.textfile);
 
@@ -61,293 +66,335 @@
       show_mini_usage();
    }
 
-   fn = imlib_load_font(opt.font);
-   if (!fn)
-      eprintf("failed to load font %s", opt.font);
+   image = gozer_render(&opt);
 
-   if (opt.stylefile)
-   {
-      style = gib_style_new_from_ascii(opt.stylefile);
-      if (!style)
-         weprintf("failed to load style %s", opt.stylefile);
-   }
+   imlib_context_set_image(image);
+   imlib_image_attach_data_value("quality", NULL, opt.quality, NULL);
 
-   if (opt.bg_image)
+   gib_imlib_save_image_with_error_return(image, opt.output_file, &err);
+   if (err)
+      eprintf("Saving to file %s failed", opt.output_file);
+   if (opt.to_stdout)
    {
-      if (gib_imlib_load_image(&bg_image, opt.bg_image))
-      {
-         bgw = gib_imlib_image_get_width(bg_image);
-         bgh = gib_imlib_image_get_height(bg_image);
-      }
-      else
-      {
-         weprintf("failed to load background image \"%s\"", opt.bg_image);
-      }
+      gozer_stream_file(opt.output_file, opt.cgi);
+      unlink(opt.output_file);
    }
 
-   lines = gozer_calculate_lines(fn, style);
+   return 0;
+}
 
-   l = lines;
-   while (l)
-   {
-      p = (char *) l->data;
-      gib_imlib_get_text_size(fn, p, style, &ww, &hh, IMLIB_TEXT_TO_RIGHT);
-      if (ww > w)
-        w = ww;
-      h += hh;
-      if (l->next)
-         h += opt.line_spacing;
-      l = l->next;
-   }
-
-   /* If we have an offset, increase the size */
-   if (opt.x)
-      w += opt.x;
-   if (opt.y)
-      h += opt.y;
-
-   if (bg_image && opt.bg_resize)
-      image = imlib_create_image(bgw, bgh);
-   else
-      image = imlib_create_image(w, h);
-
-   if (!image)
-      eprintf("couldn't create imlib image for text area.");
-
-   /* make image transparent (HACK - imlib2 should do this nicely) */
-   gib_imlib_image_set_has_alpha(image, 1);
-   memset(atab, 0, sizeof(atab));
-   gib_imlib_apply_color_modifier_to_rectangle(image, 0, 0, w, h, NULL, NULL,
-                                               NULL, atab);
-
-   gib_imlib_image_fill_rectangle(image, 0, 0, w, h, opt.bg_r, opt.bg_g,
-                                  opt.bg_b, opt.bg_a);
-   if (bg_image)
+static void
+init_parse_options(int argc, char **argv, gozeroptions *opt)
+{
+   default_options(opt);
+
+   /* Parse the cmdline args */
+   gozer_parse_option_array(argc, argv, opt);
+
+   if (opt->rc_file)
+      gozer_parse_rc_file(opt->rc_file, 1, opt);
+
+   gozer_parse_options_internal(opt);
+}
+
+static void
+gozer_parse_option_array(int argc, char **argv, gozeroptions *opt)
+{
+   static char stropts[] = "b:f:F:G:hj:l:oOp:Pr:s:t:T:vw:x:y:012";
+   static struct option lopts[] = {
+      /* actions */
+      {"help", 0, 0, 'h'},
+      {"version", 0, 0, 'v'},
+      /* toggles */
+      {"stdout", 0, 0, 'o'},
+      {"cgi", 0, 0, 'O'},
+      /* options */
+      {"background", 1, 0, 'b'},
+      {"foreground", 1, 0, 'f'},
+      {"font", 1, 0, 'F'},
+      {"bg-file", 1, 0, 'G'},
+      {"bg-nocrop", 0, 0, '0'},
+      {"bg-scale", 0, 0, '1'},
+      {"bg-tile", 0, 0, '2'},
+      {"justification", 1, 0, 'j'},
+      {"line-spacing", 1, 0, 'l'},
+      {"fontpath", 1, 0, 'p'},
+      {"pipe", 0, 0, 'P'},
+      {"quality", 1, 0, 'q'},
+      {"rc-file", 1, 0, 'r'},
+      {"style", 1, 0, 's'},
+      {"text", 1, 0, 't'},
+      {"textfile", 1, 0, 'T'},
+      {"wrap", 1, 0, 'w'},
+      {"x-offset", 1, 0, 'x'},
+      {"y-offset", 1, 0, 'y'},
+      {0, 0, 0, 0}
+   };
+   int optch = 0, cmdx = 0;
+
+   /* Now to parse some optionarinos */
+   while ((optch = getopt_long_only(argc, argv, stropts, lopts, &cmdx)) != EOF)
    {
-      if (opt.bg_scale)
-      {
-         gib_imlib_blend_image_onto_image(image, bg_image, 1, 0, 0, bgw, bgh,
-                                          0, 0, w, h, 0, 1, 1);
-      }
-      else if (opt.bg_tile)
+      switch (optch)
       {
-         gib_imlib_image_tile(bg_image);
-         gib_imlib_blend_image_onto_image(image, bg_image, 1, 0, 0, bgw, bgh,
-                                          0, 0, w, h, 0, 1, 1);
-      }
-      else
-      {
-         gib_imlib_blend_image_onto_image(image, bg_image, 1, 0, 0, bgw, bgh,
-                                          0, 0, bgw, bgh, 0, 1, 1);
-      }
-   }
-   l = lines;
-   x = opt.x;
-   y = opt.y;
-   while (l)
-   {
-      p = (char *) l->data;
-      gib_imlib_get_text_size(fn, p, style, &ww, &hh, IMLIB_TEXT_TO_RIGHT);
-      switch (opt.justification)
-      {
-        case JUST_LEFT:
-           x = opt.x;
-           gib_imlib_text_draw(image, fn, style, x, y, p, IMLIB_TEXT_TO_RIGHT,
-                               opt.fn_r, opt.fn_g, opt.fn_b, opt.fn_a);
-           break;
-        case JUST_CENTER:
-           x = ((((opt.bg_resize && bgw) ? bgw : w) - ww) / 2);
-           /* y = ((((opt.bg_resize && bgw) ? bgh : h) - hh) / 2); */
-           gib_imlib_text_draw(image, fn, style, x, y, p, IMLIB_TEXT_TO_RIGHT,
-                               opt.fn_r, opt.fn_g, opt.fn_b, opt.fn_a);
-           break;
-        case JUST_RIGHT:
-           x = (((opt.bg_resize && bgw) ? bgw : w) - ww);
-           gib_imlib_text_draw(image, fn, style, x, y, p, IMLIB_TEXT_TO_RIGHT,
-                               opt.fn_r, opt.fn_g, opt.fn_b, opt.fn_a);
-
-           break;
-        case JUST_BLOCK:
-           words = gib_string_split(p, " ");
-           if (words)
-           {
-              int wordcnt, word_spacing, line_w;
-              int t_width, m_width, space_width, offset = 0;
-
-              wordcnt = gib_list_length(words);
-              gib_imlib_get_text_size(fn, p, style, &line_w, NULL,
-                                      IMLIB_TEXT_TO_RIGHT);
-              gib_imlib_get_text_size(fn, "M M", style, &t_width, NULL,
-                                      IMLIB_TEXT_TO_RIGHT);
-              gib_imlib_get_text_size(fn, "M", style, &m_width, NULL,
-                                      IMLIB_TEXT_TO_RIGHT);
-              space_width = t_width - (2 * m_width);
-
-              if (wordcnt > 1)
-                 word_spacing = (w - line_w) / (wordcnt - 1);
-              else
-                 word_spacing = (w - line_w);
-
-              ll = words;
-              while (ll)
-              {
-                 pp = (char *) ll->data;
-                 if (strcmp(pp, " "))
-                 {
-                    int wordw;
-
-                    gib_imlib_text_draw(image, fn, style, x + offset, y, pp,
-                                        IMLIB_TEXT_TO_RIGHT, opt.fn_r,
-                                        opt.fn_g, opt.fn_b, opt.fn_a);
-                    gib_imlib_get_text_size(fn, pp, style, &wordw, NULL,
-                                            IMLIB_TEXT_TO_RIGHT);
-                    offset += (wordw + space_width + word_spacing);
-                 }
-                 ll = ll->next;
-              }
-              gib_list_free_and_data(words);
-           }
+        case 0:
+           break;
+        case 'h':
+           show_usage();
+           break;
+        case 'v':
+           show_version();
+           break;
+        case 'b':
+           opt->background_col = estrdup(optarg);
+           break;
+        case 'f':
+           opt->font_col = estrdup(optarg);
+           break;
+        case 'F':
+           opt->font = estrdup(optarg);
+           break;
+        case 'G':
+           opt->bg_image = estrdup(optarg);
+           break;
+        case 'j':
+           if (!strcasecmp(optarg, "left"))
+              opt->justification = JUST_LEFT;
+           else if (!strcasecmp(optarg, "right"))
+              opt->justification = JUST_RIGHT;
+           else if (!strcasecmp(optarg, "center"))
+              opt->justification = JUST_CENTER;
+           else if (!strcasecmp(optarg, "block"))
+              opt->justification = JUST_BLOCK;
+           else
+              weprintf("unknown justification setting %s ignored\n", optarg);
+           break;
+        case 'l':
+           opt->line_spacing = atoi(optarg);
+           break;
+        case 'o':
+           opt->to_stdout = 1;
+           break;
+        case 'O':
+           opt->to_stdout = 1;
+           opt->cgi = 1;
+           break;
+        case 'p':
+           gib_imlib_parse_fontpath(optarg);
+           break;
+               case 'P':
+           opt->pipe = 1;
+           break;
+        case 'q':
+           opt->quality = atoi(optarg);
+           break;
+        case 'r':
+           opt->rc_file = estrdup(optarg);
+           break;
+        case 's':
+           opt->stylefile = estrdup(optarg);
+           break;
+        case 't':
+           opt->text = estrdup(optarg);
+           break;
+        case 'T':
+           opt->textfile = estrdup(optarg);
+           break;
+        case 'w':
+           opt->wrap_width = atoi(optarg);
+           break;
+        case 'x':
+           opt->x = atoi(optarg);
+           break;
+        case 'y':
+           opt->y = atoi(optarg);
+           break;
+               case '0':
+           opt->bg_resize = 1;
+           opt->bg_scale = 0;
+           opt->bg_tile = 0;
+           break;
+               case '1':
+           opt->bg_resize = 0;
+           opt->bg_scale = 1;
+           opt->bg_tile = 0;
+           break;
+               case '2':
+           opt->bg_resize = 0;
+           opt->bg_scale = 0;
+           opt->bg_tile = 1;
            break;
-
         default:
            break;
       }
-      y += hh + opt.line_spacing;
-      l = l->next;
    }
 
-   imlib_context_set_image(image);
-   imlib_image_attach_data_value("quality", NULL, opt.quality, NULL);
-
-   gib_imlib_save_image_with_error_return(image, opt.output_file, &err);
-   if (err)
-      eprintf("Saving to file %s failed", opt.output_file);
-   if (opt.to_stdout)
+   /* Now the leftovers, which must be files */
+   if (optind < argc)
    {
-      gozer_stream_file(opt.output_file, opt.cgi);
-      unlink(opt.output_file);
+      while (optind < argc)
+         if (!opt->output_file)
+            opt->output_file = estrdup(argv[optind++]);
+         else
+            weprintf("unrecognised option %s\n", argv[optind++]);
    }
 
-   return 0;
+   /* So that we can safely be called again */
+   optind = 1;
+}
+
+
+static void
+show_version(void)
+{
+   printf(PACKAGE " version " VERSION "\n");
+   exit(0);
+}
+
+static void
+show_mini_usage(void)
+{
+   printf("Usage: " PACKAGE " [OPTIONS] file\nUse " PACKAGE
+          " --help for detailed usage information\n");
+   exit(0);
 }
 
 
-gib_list *
-gozer_calculate_lines(Imlib_Font fn, gib_style * style)
+static void
+show_usage(void)
 {
-   gib_list *ll, *lines = NULL, *list = NULL, *words;
-   gib_list *l = NULL;
-   char delim[2] = { '\n', '\0' };
-   int w, line_width;
-   int tw, th;
-   char *p, *pp;
-   char *line = NULL;
-   char *temp;
-   int space_width = 0, m_width = 0, t_width = 0, new_width = 0;
-
-   lines = gib_string_split(opt.text, delim);
-
-   if (opt.wrap_width)
-   {
-      gib_imlib_get_text_size(fn, "M M", style, &t_width, NULL,
-                              IMLIB_TEXT_TO_RIGHT);
-      gib_imlib_get_text_size(fn, "M", style, &m_width, NULL,
-                              IMLIB_TEXT_TO_RIGHT);
-      space_width = t_width - (2 * m_width);
-      w = opt.wrap_width;
-      l = lines;
-      while (l)
+   fprintf(stdout,
+           "Usage: " PACKAGE " [OPTIONS] file\n"
+           " Where file is the target file for the rendered text.\n"
+           " See man " PACKAGE " for more detailed information.\n"
+           " Options can also be specified in an rc file , allowing you\n"
+           " to save commonly used options (such as fontpaths) for\n"
+           " reuse. The rc file $HOME/.gozerrc is always read first for default\n"
+           " options, and other rc files can be specified on the commandline.\n"
+           " See RC FILES for information on syntax.\n"
+           " -h, --help               display this help and exit.\n"
+           " -v, --version            output version information and exit.\n"
+           " -b, --background COL     use COL as the background color,\n"
+           "                          see COLOUR_DEFINITIONS for syntax,\n"
+           "                          default is transparent (0,0,0,0).\n"
+           " -f, --foreground COL     use COL as the foreground color. Default is\n"
+           "                          white (255,255,255,255).\n"
+           " -F, --font STR           draw using font STR. Size is specified with 
+the\n"
+           "                          name, eg \"arial/12\" for 12pt arial, default\n"
+           "                          is helmetr/16 (a font supplied by gozer).\n"
+           " -G, --bg-file FILE       load image FILE and draw the text on it\n"
+           " -j, --justification STR  justify text, allowed values for STR are 
+left,\n"
+           "                          right, center or block. Default is left.\n"
+           " -l, --line-spacing INT   separate multiple lines by INT pixels, default 
+1.\n"
+           " -o, --stdout             send image data to STDOUT (for redirection)\n"
+           " -O, --cgi                send image data to STDOUT with headers for 
+CGI\n"
+           "                          For both of these you still need to supply an\n"
+           "                          output file, so gozer knows what image type to 
+save\n"
+           "                          as, but the file will be deleted 
+automatically\n"
+           " -p, --fontpath STR       colon seperated list of font directories to\n"
+           "                          search for fonts in. Best used in the RCFILE\n"
+           "                          to save typing it each time.\n"
+           " -q, --quality NUM        Image quality (1-100) high value means\n"
+           "                          high size, low compression. Default: 75.\n"
+           "                          For lossless compression formats, like png,\n"
+           "                          low quality means high compression.\n"
+           " -P, --pipe               Pipe input text from standard in instead of\n"
+           "                          reading it from a file\n"
+           " -r, --rc-file STR        use STR as an rc file to read for extra 
+options.\n"
+           " -s, --style STR          use STR as a file to load a fontstyle from,\n"
+           "                          see FONTSTYLES for syntax.\n"
+           " -t, --text STR           use STR as text to draw.\n"
+           " -T, --textfile STR       read text to draw from file STR.\n"
+           " -w, --wrap INT           wordwrap the text to INT pixels.\n" "\n"
+           " -x, --x-offset INT       Offset text horizontally from top left by INT 
+pixels.\n" "\n"
+           " -y, --y-offset INT       Offset text vertically from top left by INT 
+pixels.\n" "\n"
+           " -0, --bg-nocrop          set output size to background image size\n"
+           " -1, --bg-scale           scale background image\n"
+           " -2, --bg-tile            tile background image\n"
+           " RC FILES\n"
+           " rc file syntax is simple. The '#' character at  the  start\n"
+           " of  a  line  denotes that the line is a comment, otherwise\n"
+           " lines are started with the name of a long option from  the\n"
+           " commandline  (without  prefixing  the --), some whitespace\n"
+           " and the value of the option. E.g.\n"
+           " fontpath /usr/share/truetype:/usr/local/share/truetype\n"
+           " NOTE that options in the rc file OVERRIDE those  from  the\n"
+           " commandline.\n" "\n" " COLOUR_DEFINITIONS\n"
+           " You can define colours in 4 ways. Using the html style:\n"
+           " #RRGGBB (in which case alpha defaults to 255),\n" " #RRGGBBAA,\n"
+           " or an alternative style:\n"
+           " r,g,b or r,g,b,a (no spaces between commas please).\n"
+           " eg for white text, either use #ffffff, #ffffffff, \"255,255,255\" or\n"
+           " \"255,255,255,255\". For red, #ff0000, #ff0000ff or \"255,0,0\" etc.\n"
+           "\n" " FONTSTYLES\n"
+           " fontstyles can be defined in the EFM syntax (for compatibility only, 
+these\n"
+           " are not as powerful), or the recommended syntax defined here.\n"
+           " The first line contains the line:\n" " #Style\n"
+           " The second, an optional style name,\n" " #NAME mystyle\n"
+           " There follows a list of layers. Each is described by this rule:\n"
+           " RED GREEN BLUE ALPHA X_OFFSET Y_OFFSET\n"
+           " The special values or 0,0,0,0 for red, green, blue and alpha specify 
+the\n"
+           " positioning of the actual text in it's selected colour.\n"
+           " For example, the following style defines a very simple shadow for 
+text:\n"
+           " #Style\n" " #NAME shadow\n" " 0 0 0 128 -3 -3\n" " 0 0 0 0 0 0\n"
+           "\n"
+           "This program is free software - see the file COPYING for licensing 
+info.\n"
+           "Copyright Tom Gilbert 2000\n"
+           "Email bugs to <[EMAIL PROTECTED]>\n");
+   exit(0);
+}
+
+static void
+gozer_parse_options_internal(gozeroptions *opt)
+{
+   if (opt->background_col)
+      gib_imlib_parse_color(opt->background_col,
+            &opt->bg_r, &opt->bg_g, &opt->bg_b, &opt->bg_a);
+   if (opt->font_col)
+      gib_imlib_parse_color(opt->font_col,
+            &opt->fn_r, &opt->fn_g, &opt->fn_b, &opt->fn_a);
+}
+
+static void
+gozer_stream_file(char *file, int headers)
+{
+   FILE *fp;
+   char buf[10240];
+   size_t count;
+
+   if (headers)
+   {
+      /* print headers for user-agents */
+      char *extension;
+
+      extension = strrchr(file, '.');
+      if (extension)
       {
-         line_width = 0;
-         p = (char *) l->data;
-         /* quick check to see if whole line fits okay */
-         gib_imlib_get_text_size(fn, p, style, &tw, &th, IMLIB_TEXT_TO_RIGHT);
-         if (tw <= w)
-            list = gib_list_add_end(list, estrdup(p));
-         else if (strlen(p) == 0)
-            list = gib_list_add_end(list, estrdup(""));
-         else if (!strcmp(p, " "))
-            list = gib_list_add_end(list, estrdup(" "));
+         extension++;
+         if (!strcasecmp(extension, "jpg"))
+            printf("Content-type: image/jpeg\n");
          else
-         {
-            words = gib_string_split(p, " ");
-            if (words)
-            {
-               ll = words;
-               while (ll)
-               {
-                  pp = (char *) ll->data;
-                  if (strcmp(pp, " "))
-                  {
-                     gib_imlib_get_text_size(fn, pp, style, &tw, &th,
-                                             IMLIB_TEXT_TO_RIGHT);
-                     if (line_width == 0)
-                        new_width = tw;
-                     else
-                        new_width = line_width + space_width + tw;
-                     if (new_width <= w)
-                     {
-                        /* add word to line */
-                        if (line)
-                        {
-                           int len;
-
-                           len = strlen(line) + strlen(pp) + 2;
-                           temp = emalloc(len);
-                           snprintf(temp, len, "%s %s", line, pp);
-                           free(line);
-                           line = temp;
-                        }
-                        else
-                           line = estrdup(pp);
-                        line_width = new_width;
-                     }
-                     else if (line_width == 0)
-                     {
-                        /* can't fit single word in :/
-                           increase width limit to width of word and jam the bastard
-                           in anyhow */
-                        w = tw;
-                        line = estrdup(pp);
-                        line_width = new_width;
-                     }
-                     else
-                     {
-                        /* finish this line, start next and add word there */
-                        if (line)
-                        {
-                           list = gib_list_add_end(list, estrdup(line));
-                           free(line);
-                           line = NULL;
-                        }
-                        line = estrdup(pp);
-                        line_width = tw;
-                     }
-                  }
-                  ll = ll->next;
-               }
-               if (line)
-               {
-                  /* finish last line */
-                  list = gib_list_add_end(list, estrdup(line));
-                  free(line);
-                  line = NULL;
-                  line_width = 0;
-               }
-               gib_list_free_and_data(words);
-            }
-         }
-         l = l->next;
+            printf("Content-type: image/%s\n", extension);
       }
-      gib_list_free_and_data(lines);
-      lines = list;
+      else
+         weprintf("couldn't determine file extension for mime type setting");
+      printf("\n");
    }
-   else
-      lines = lines;
-   return lines;
+
+   fp = fopen(file, "r");
+   if (!fp)
+   {
+      weprintf("couldn't open file %s for streaming\n", file);
+      return;
+   }
+   while ((count = fread(buf, 1, sizeof buf, fp)))
+   {
+      fwrite(buf, 1, count, stdout);
+   }
+   fclose(fp);
 }
 
-char *
+static char *
 gozer_read_file(char *filename)
 {
    FILE *fp;
@@ -372,10 +419,12 @@
    fread(text, 1, st.st_size, fp);
    text[st.st_size] = '\0';
 
+   fclose(fp);
+
    return text;
 }
 
-char *
+static char *
 gozer_read_pipe()
 {
    char buf[PIPE_BUF_MAX] = "", buf2[1023] = "", *text = NULL;
@@ -395,43 +444,4 @@
    }
    text = _estrdup(buf);
    return text;
-}
-
-void
-gozer_stream_file(char *file, int headers)
-{
-   FILE *fp;
-   char buf[10240];
-   size_t count;
-
-   if (headers)
-   {
-      /* print headers for user-agents */
-      char *extension;
-
-      extension = strrchr(file, '.');
-      if (extension)
-      {
-         extension++;
-         if (!strcasecmp(extension, "jpg"))
-            printf("Content-type: image/jpeg\n");
-         else
-            printf("Content-type: image/%s\n", extension);
-      }
-      else
-         weprintf("couldn't determine file extension for mime type setting");
-      printf("\n");
-   }
-
-   fp = fopen(file, "r");
-   if (!fp)
-   {
-      weprintf("couldn't open file %s for streaming\n", file);
-      return;
-   }
-   while ((count = fread(buf, 1, sizeof buf, fp)))
-   {
-      fwrite(buf, 1, count, stdout);
-   }
-   fclose(fp);
 }
===================================================================
RCS file: /cvsroot/enlightenment/misc/gozer/src/options.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -3 -r1.5 -r1.6
--- options.c   4 Dec 2002 12:33:44 -0000       1.5
+++ options.c   18 Jan 2003 18:15:09 -0000      1.6
@@ -20,41 +20,33 @@
 THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
 */
 
 #include "gozer.h"
-#include "options.h"
-
-static void gozer_parse_option_array(int argc, char **argv);
-static void gozer_parse_options_internal(void);
-static void gozer_parse_rc_file(char *file, int user_defined);
-gozeroptions opt;
 
 void
-init_parse_options(int argc, char **argv)
+default_options(gozeroptions *opt)
 {
    char *home, *rcfile;
    struct stat ignored;
 
    /* Set default options */
-   memset(&opt, 0, sizeof(gozeroptions));
-
-   opt.line_spacing = 1;
-   opt.bg_a = 255;
-   opt.bg_g = 255;
-   opt.bg_b = 255;
-   opt.bg_r = 255;
-   opt.fn_a = 255;
-   opt.justification = JUST_LEFT;
-   opt.font = estrdup("helmetr/16");
-   opt.pipe = 0;
-   opt.bg_image = NULL;
-   opt.bg_resize = 0;
-   opt.bg_tile = 0;
-   opt.bg_scale = 0;
+   memset(opt, 0, sizeof(gozeroptions));
 
-   opt.quality = 75;
+   opt->line_spacing = 1;
+   opt->bg_a = 255;
+   opt->bg_g = 255;
+   opt->bg_b = 255;
+   opt->bg_r = 255;
+   opt->fn_a = 255;
+   opt->justification = JUST_LEFT;
+   opt->font = estrdup("helmetr/16");
+   opt->pipe = 0;
+   opt->bg_image = NULL;
+   opt->bg_resize = 0;
+   opt->bg_tile = 0;
+   opt->bg_scale = 0;
+   opt->quality = 75;
 
    home = getenv("HOME");
    if (!home)
@@ -62,281 +54,14 @@
                "It would really help me out in loading your rc file...\n");
    rcfile = estrjoin("/", home, ".gozerrc", NULL);
    if (stat(rcfile, &ignored) < 0)
-         gozer_parse_rc_file("/etc/gozerrc", 0);
+         gozer_parse_rc_file("/etc/gozerrc", 0, opt);
    else
-         gozer_parse_rc_file(rcfile, 0);
+         gozer_parse_rc_file(rcfile, 0, opt);
    free(rcfile);
-
-   /* Parse the cmdline args */
-   gozer_parse_option_array(argc, argv);
-
-   if (opt.rc_file)
-      gozer_parse_rc_file(opt.rc_file, 1);
-
-   gozer_parse_options_internal();
-}
-
-static void
-gozer_parse_option_array(int argc, char **argv)
-{
-   static char stropts[] = "b:f:F:G:hj:l:oOp:Pr:s:t:T:vw:x:y:012";
-   static struct option lopts[] = {
-      /* actions */
-      {"help", 0, 0, 'h'},
-      {"version", 0, 0, 'v'},
-      /* toggles */
-      {"stdout", 0, 0, 'o'},
-      {"cgi", 0, 0, 'O'},
-      /* options */
-      {"background", 1, 0, 'b'},
-      {"foreground", 1, 0, 'f'},
-      {"font", 1, 0, 'F'},
-      {"bg-file", 1, 0, 'G'},
-      {"bg-nocrop", 0, 0, '0'},
-      {"bg-scale", 0, 0, '1'},
-      {"bg-tile", 0, 0, '2'},
-      {"justification", 1, 0, 'j'},
-      {"line-spacing", 1, 0, 'l'},
-      {"fontpath", 1, 0, 'p'},
-      {"pipe", 0, 0, 'P'},
-      {"quality", 1, 0, 'q'},
-      {"rc-file", 1, 0, 'r'},
-      {"style", 1, 0, 's'},
-      {"text", 1, 0, 't'},
-      {"textfile", 1, 0, 'T'},
-      {"wrap", 1, 0, 'w'},
-      {"x-offset", 1, 0, 'x'},
-      {"y-offset", 1, 0, 'y'},
-      {0, 0, 0, 0}
-   };
-   int optch = 0, cmdx = 0;
-
-   /* Now to parse some optionarinos */
-   while ((optch = getopt_long_only(argc, argv, stropts, lopts, &cmdx)) != EOF)
-   {
-      switch (optch)
-      {
-        case 0:
-           break;
-        case 'h':
-           show_usage();
-           break;
-        case 'v':
-           show_version();
-           break;
-        case 'b':
-           opt.background_col = estrdup(optarg);
-           break;
-        case 'f':
-           opt.font_col = estrdup(optarg);
-           break;
-        case 'F':
-           opt.font = estrdup(optarg);
-           break;
-        case 'G':
-           opt.bg_image = estrdup(optarg);
-           break;
-        case 'j':
-           if (!strcasecmp(optarg, "left"))
-              opt.justification = JUST_LEFT;
-           else if (!strcasecmp(optarg, "right"))
-              opt.justification = JUST_RIGHT;
-           else if (!strcasecmp(optarg, "center"))
-              opt.justification = JUST_CENTER;
-           else if (!strcasecmp(optarg, "block"))
-              opt.justification = JUST_BLOCK;
-           else
-              weprintf("unknown justification setting %s ignored\n", optarg);
-           break;
-        case 'l':
-           opt.line_spacing = atoi(optarg);
-           break;
-        case 'o':
-           opt.to_stdout = 1;
-           break;
-        case 'O':
-           opt.to_stdout = 1;
-           opt.cgi = 1;
-           break;
-        case 'p':
-           gib_imlib_parse_fontpath(optarg);
-           break;
-               case 'P':
-           opt.pipe = 1;
-           break;
-        case 'q':
-           opt.quality = atoi(optarg);
-           break;
-        case 'r':
-           opt.rc_file = estrdup(optarg);
-           break;
-        case 's':
-           opt.stylefile = estrdup(optarg);
-           break;
-        case 't':
-           opt.text = estrdup(optarg);
-           break;
-        case 'T':
-           opt.textfile = estrdup(optarg);
-           break;
-        case 'w':
-           opt.wrap_width = atoi(optarg);
-           break;
-        case 'x':
-           opt.x = atoi(optarg);
-           break;
-        case 'y':
-           opt.y = atoi(optarg);
-           break;
-               case '0':
-           opt.bg_resize = 1;
-           opt.bg_scale = 0;
-           opt.bg_tile = 0;
-           break;
-               case '1':
-           opt.bg_resize = 0;
-           opt.bg_scale = 1;
-           opt.bg_tile = 0;
-           break;
-               case '2':
-           opt.bg_resize = 0;
-           opt.bg_scale = 0;
-           opt.bg_tile = 1;
-           break;
-        default:
-           break;
-      }
-   }
-
-   /* Now the leftovers, which must be files */
-   if (optind < argc)
-   {
-      while (optind < argc)
-         if (!opt.output_file)
-            opt.output_file = estrdup(argv[optind++]);
-         else
-            weprintf("unrecognised option %s\n", argv[optind++]);
-   }
-
-   /* So that we can safely be called again */
-   optind = 1;
-}
-
-
-void
-show_version(void)
-{
-   printf(PACKAGE " version " VERSION "\n");
-   exit(0);
-}
-
-void
-show_mini_usage(void)
-{
-   printf("Usage: " PACKAGE " [OPTIONS] file\nUse " PACKAGE
-          " --help for detailed usage information\n");
-   exit(0);
 }
 
-
 void
-show_usage(void)
-{
-   fprintf(stdout,
-           "Usage: " PACKAGE " [OPTIONS] file\n"
-           " Where file is the target file for the rendered text.\n"
-           " See man " PACKAGE " for more detailed information.\n"
-           " Options can also be specified in an rc file , allowing you\n"
-           " to save commonly used options (such as fontpaths) for\n"
-           " reuse. The rc file $HOME/.gozerrc is always read first for default\n"
-           " options, and other rc files can be specified on the commandline.\n"
-           " See RC FILES for information on syntax.\n"
-           " -h, --help               display this help and exit.\n"
-           " -v, --version            output version information and exit.\n"
-           " -b, --background COL     use COL as the background color,\n"
-           "                          see COLOUR_DEFINITIONS for syntax,\n"
-           "                          default is transparent (0,0,0,0).\n"
-           " -f, --foreground COL     use COL as the foreground color. Default is\n"
-           "                          white (255,255,255,255).\n"
-           " -F, --font STR           draw using font STR. Size is specified with 
the\n"
-           "                          name, eg \"arial/12\" for 12pt arial, default\n"
-           "                          is helmetr/16 (a font supplied by gozer).\n"
-           " -G, --bg-file FILE       load image FILE and draw the text on it\n"
-           " -j, --justification STR  justify text, allowed values for STR are 
left,\n"
-           "                          right, center or block. Default is left.\n"
-           " -l, --line-spacing INT   separate multiple lines by INT pixels, default 
1.\n"
-           " -o, --stdout             send image data to STDOUT (for redirection)\n"
-           " -O, --cgi                send image data to STDOUT with headers for 
CGI\n"
-           "                          For both of these you still need to supply an\n"
-           "                          output file, so gozer knows what image type to 
save\n"
-           "                          as, but the file will be deleted 
automatically\n"
-           " -p, --fontpath STR       colon seperated list of font directories to\n"
-           "                          search for fonts in. Best used in the RCFILE\n"
-           "                          to save typing it each time.\n"
-           " -q, --quality NUM        Image quality (1-100) high value means\n"
-           "                          high size, low compression. Default: 75.\n"
-           "                          For lossless compression formats, like png,\n"
-           "                          low quality means high compression.\n"
-           " -P, --pipe               Pipe input text from standard in instead of\n"
-           "                          reading it from a file\n"
-           " -r, --rc-file STR        use STR as an rc file to read for extra 
options.\n"
-           " -s, --style STR          use STR as a file to load a fontstyle from,\n"
-           "                          see FONTSTYLES for syntax.\n"
-           " -t, --text STR           use STR as text to draw.\n"
-           " -T, --textfile STR       read text to draw from file STR.\n"
-           " -w, --wrap INT           wordwrap the text to INT pixels.\n" "\n"
-           " -x, --x-offset INT       Offset text horizontally from top left by INT 
pixels.\n" "\n"
-           " -y, --y-offset INT       Offset text vertically from top left by INT 
pixels.\n" "\n"
-           " -0, --bg-nocrop          set output size to background image size\n"
-           " -1, --bg-scale           scale background image\n"
-           " -2, --bg-tile            tile background image\n"
-           " RC FILES\n"
-           " rc file syntax is simple. The '#' character at  the  start\n"
-           " of  a  line  denotes that the line is a comment, otherwise\n"
-           " lines are started with the name of a long option from  the\n"
-           " commandline  (without  prefixing  the --), some whitespace\n"
-           " and the value of the option. E.g.\n"
-           " fontpath /usr/share/truetype:/usr/local/share/truetype\n"
-           " NOTE that options in the rc file OVERRIDE those  from  the\n"
-           " commandline.\n" "\n" " COLOUR_DEFINITIONS\n"
-           " You can define colours in 4 ways. Using the html style:\n"
-           " #RRGGBB (in which case alpha defaults to 255),\n" " #RRGGBBAA,\n"
-           " or an alternative style:\n"
-           " r,g,b or r,g,b,a (no spaces between commas please).\n"
-           " eg for white text, either use #ffffff, #ffffffff, \"255,255,255\" or\n"
-           " \"255,255,255,255\". For red, #ff0000, #ff0000ff or \"255,0,0\" etc.\n"
-           "\n" " FONTSTYLES\n"
-           " fontstyles can be defined in the EFM syntax (for compatibility only, 
these\n"
-           " are not as powerful), or the recommended syntax defined here.\n"
-           " The first line contains the line:\n" " #Style\n"
-           " The second, an optional style name,\n" " #NAME mystyle\n"
-           " There follows a list of layers. Each is described by this rule:\n"
-           " RED GREEN BLUE ALPHA X_OFFSET Y_OFFSET\n"
-           " The special values or 0,0,0,0 for red, green, blue and alpha specify 
the\n"
-           " positioning of the actual text in it's selected colour.\n"
-           " For example, the following style defines a very simple shadow for 
text:\n"
-           " #Style\n" " #NAME shadow\n" " 0 0 0 128 -3 -3\n" " 0 0 0 0 0 0\n"
-           "\n"
-           "This program is free software - see the file COPYING for licensing 
info.\n"
-           "Copyright Tom Gilbert 2000\n"
-           "Email bugs to <[EMAIL PROTECTED]>\n");
-   exit(0);
-}
-
-static void
-gozer_parse_options_internal(void)
-{
-   if (opt.background_col)
-      gib_imlib_parse_color(opt.background_col, &opt.bg_r, &opt.bg_g, &opt.bg_b,
-                      &opt.bg_a);
-   if (opt.font_col)
-      gib_imlib_parse_color(opt.font_col, &opt.fn_r, &opt.fn_g, &opt.fn_b,
-                      &opt.fn_a);
-}
-
-
-static void
-gozer_parse_rc_file(char *file, int user_defined)
+gozer_parse_rc_file(char *file, int user_defined, gozeroptions *opt)
 {
    FILE *fp = NULL;
    char s[1024], s1[1024], s2[1024];
@@ -344,7 +69,7 @@
    if ((fp = fopen(file, "r")) == NULL)
    {
       if (user_defined)
-         weprintf("couldn't load the specified rcfile %s:", opt.rc_file);
+         weprintf("couldn't load the specified rcfile %s:", opt->rc_file);
       return;
    }
 
@@ -360,53 +85,53 @@
          gib_imlib_parse_fontpath(s2);
       else if (!strcasecmp(s1, "background"))
       {
-         free(opt.background_col);
-         opt.background_col = estrdup(s2);
+         free(opt->background_col);
+         opt->background_col = estrdup(s2);
       }
       else if (!strcasecmp(s1, "foreground"))
       {
-         free(opt.font_col);
-         opt.font_col = estrdup(s2);
+         free(opt->font_col);
+         opt->font_col = estrdup(s2);
       }
       else if (!strcasecmp(s1, "font"))
       {
-         free(opt.font);
-         opt.font = estrdup(s2);
+         free(opt->font);
+         opt->font = estrdup(s2);
       }
       else if (!strcasecmp(s1, "justification"))
       {
          if (!strcasecmp(s2, "left"))
-            opt.justification = JUST_LEFT;
+            opt->justification = JUST_LEFT;
          else if (!strcasecmp(s2, "right"))
-            opt.justification = JUST_RIGHT;
+            opt->justification = JUST_RIGHT;
          else if (!strcasecmp(s2, "center"))
-            opt.justification = JUST_CENTER;
+            opt->justification = JUST_CENTER;
          else if (!strcasecmp(s2, "block"))
-            opt.justification = JUST_BLOCK;
+            opt->justification = JUST_BLOCK;
          else
             weprintf("unknown justification setting %s ignored\n", s2);
       }
       else if (!strcasecmp(s1, "line-spacing"))
-         opt.line_spacing = atoi(s2);
+         opt->line_spacing = atoi(s2);
       else if (!strcasecmp(s1, "style"))
       {
-         free(opt.stylefile);
-         opt.stylefile = estrdup(s2);
+         free(opt->stylefile);
+         opt->stylefile = estrdup(s2);
       }
       else if (!strcasecmp(s1, "text"))
       {
-         free(opt.text);
-         opt.text = estrdup(s2);
+         free(opt->text);
+         opt->text = estrdup(s2);
       }
       else if (!strcasecmp(s1, "textfile"))
       {
-         free(opt.textfile);
-         opt.textfile = estrdup(s2);
+         free(opt->textfile);
+         opt->textfile = estrdup(s2);
       }
       else if (!strcasecmp(s1, "wrap"))
-         opt.wrap_width = atoi(s2);
+         opt->wrap_width = atoi(s2);
       else
-         weprintf("unrecognised option %s in rc file %s\n", s2, opt.rc_file);
+         weprintf("unrecognised option %s in rc file %s\n", s2, opt->rc_file);
    }
    fclose(fp);
 }




-------------------------------------------------------
This SF.NET email is sponsored by: Thawte.com - A 128-bit supercerts will
allow you to extend the highest allowed 128 bit encryption to all your 
clients even if they use browsers that are limited to 40 bit encryption. 
Get a guide here:http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0030en
_______________________________________________
enlightenment-cvs mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs

Reply via email to