[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

gEDA-cvs: CVS update: a_basic.c



  User: peterb  
  Date: 07/05/28 03:30:31

  Modified:    .        a_basic.c o_attrib.c o_picture.c o_text_basic.c
  Log:
  Make libgeda parse schematics from buffered data.
  
  
  
  In order to support more complex methods of acquiring symbol
  
  data, libgeda needs to be able to load schematics and symbols
  
  directly from a memory buffer as well as from files.
  
  
  
  
  Revision  Changes    Path
  1.24                 eda/geda/gaf/libgeda/src/a_basic.c
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: a_basic.c
  ===================================================================
  RCS file: /home/cvspsrv/cvsroot/eda/geda/gaf/libgeda/src/a_basic.c,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -b -r1.23 -r1.24
  --- a_basic.c	17 Apr 2007 20:19:20 -0000	1.23
  +++ a_basic.c	28 May 2007 07:30:31 -0000	1.24
  @@ -288,19 +288,30 @@
     return 1;
   }
   
  -/*! \brief Read a file
  +/*! \brief Read a memory buffer
    *  \par Function Description
  - *  This function reads a file in libgead format.
  + *  This function reads data in libgeda format from a memory buffer.
  + *
  + *  If the size argument is negative, the buffer is assumed to be
  + *  null-terminated.
  + *
  + *  The name argument is used for debugging, and should be set to a
  + *  meaningful string (e.g. the name of the file the data is from).
    *
    *  \param [in,out] w_current    The current TOPLEVEL structure.
    *  \param [in]     object_list  The object_list to read data to.
  - *  \param [in]     filename     The filename to read from.
  + *  \param [in]     buffer       The memory buffer to read from.
  + *  \param [in]     size         The size of the buffer.
  + *  \param [in]     name         The name to describe the data with.
    *  \return object_list if successful read, or NULL on error.
    */
  -OBJECT *o_read(TOPLEVEL *w_current, OBJECT *object_list, char *filename)
  +OBJECT *o_read_buffer(TOPLEVEL *w_current, OBJECT *object_list, 
  +		      char *buffer, const int size, 
  +		      const char *name)
   {
  -  FILE *fp;
  -  char buf[1024];
  +  char *line = NULL;
  +  TextBuffer *tb = NULL;
  +
     char objtype;
     OBJECT *object_list_save=NULL;
     OBJECT *temp_tail=NULL;
  @@ -315,19 +326,24 @@
   
     int embedded_level = 0;
   
  +
     /* fill version with default file format version (the current one) */
     current_fileformat_ver = FILEFORMAT_VERSION;
   
  -  fp = fopen(filename, "r");
  -
  -  if (fp == NULL) {
  -    s_log_message("o_read: Could not open [%s]\n", filename);
  +  if (buffer == NULL) {
  +    s_log_message("o_read_buffer: Received NULL buffer\n");
       return(NULL);
     }
   
  -  while ( fgets(buf, 1024, fp) != NULL) {
  +  tb = s_textbuffer_new (buffer, size);
  +  g_assert (tb != NULL);
  +
  +  while (1) {
   
  -    sscanf(buf, "%c", &objtype);
  +    line = s_textbuffer_next_line(tb);
  +    if (line == NULL) break;
  +
  +    sscanf(line, "%c", &objtype);
   
       /* Do we need to check the symbol version?  Yes, but only if */
       /* 1) the last object read was a complex and */
  @@ -346,40 +362,42 @@
       switch (objtype) {
   
         case(OBJ_LINE):
  -        object_list = (OBJECT *) o_line_read(w_current, object_list, buf, 
  +        object_list = (OBJECT *) o_line_read(w_current, object_list, line, 
   	                                     release_ver, fileformat_ver);
           break;
   
   
         case(OBJ_NET):
  -        object_list = (OBJECT *) o_net_read(w_current, object_list, buf,
  +        object_list = (OBJECT *) o_net_read(w_current, object_list, line,
                                               release_ver, fileformat_ver);
           break;
   
         case(OBJ_BUS):
  -        object_list = (OBJECT *) o_bus_read(w_current, object_list, buf,
  +        object_list = (OBJECT *) o_bus_read(w_current, object_list, line,
                                               release_ver, fileformat_ver);
           break;
   
         case(OBJ_BOX):
  -        object_list = (OBJECT *) o_box_read(w_current, object_list, buf,
  +        object_list = (OBJECT *) o_box_read(w_current, object_list, line,
                                               release_ver, fileformat_ver);
           break;
   		
         case(OBJ_PICTURE):
  -        object_list = (OBJECT *) o_picture_read(w_current, object_list, buf,
  -						fp,
  +	line = g_strdup(line);
  +        object_list = (OBJECT *) o_picture_read(w_current, object_list, 
  +						line, tb,
   						release_ver, fileformat_ver);
  +	g_free (line);
           break;
   		
         case(OBJ_CIRCLE):
  -        object_list = (OBJECT *) o_circle_read(w_current, object_list, buf,
  +        object_list = (OBJECT *) o_circle_read(w_current, object_list, line,
                                                  release_ver, fileformat_ver);
           break;
   
         case(OBJ_COMPLEX):
         case(OBJ_PLACEHOLDER):
  -        object_list = (OBJECT *) o_complex_read(w_current, object_list, buf,
  +        object_list = (OBJECT *) o_complex_read(w_current, object_list, line,
                                                   release_ver, fileformat_ver);
   
           /* this is necessary because complex may add attributes which float */
  @@ -391,20 +409,21 @@
           break;
   
         case(OBJ_TEXT):
  -        /* fgets(string, 1024, fp); string lines are now read inside: */
  -        object_list = (OBJECT *) o_text_read(w_current, object_list, buf,
  -                                             fp,
  +	line = g_strdup(line);
  +        object_list = (OBJECT *) o_text_read(w_current, object_list, 
  +					     line, tb,
                                                release_ver, fileformat_ver);
  +	g_free(line);
           break;
   
         case(OBJ_PIN):
  -        object_list = (OBJECT *) o_pin_read(w_current, object_list, buf,
  +        object_list = (OBJECT *) o_pin_read(w_current, object_list, line,
                                               release_ver, fileformat_ver);
           found_pin++;
           break;
   
         case(OBJ_ARC):
  -        object_list = (OBJECT *) o_arc_read(w_current, object_list, buf,
  +        object_list = (OBJECT *) o_arc_read(w_current, object_list, line,
                                               release_ver, fileformat_ver);
           break;
   
  @@ -412,7 +431,8 @@
           object_before_attr = object_list;
   	/* first is the fp */
   	/* 2nd is the object to get the attributes */ 
  -        object_list = (OBJECT *) o_read_attribs(w_current, fp, object_list,
  +        object_list = (OBJECT *) o_read_attribs(w_current, object_list,
  +						tb,
                                                   release_ver, fileformat_ver);
   
           /* by now we have finished reading all the attributes */
  @@ -452,7 +472,7 @@
   	} else {
           	fprintf(stderr, "Read unexpected embedded "
   				"symbol start marker in [%s] :\n>>\n%s<<\n", 
  -				filename, buf);
  +				name, line);
   	}
          	break;
   
  @@ -472,7 +492,7 @@
   	} else {
           	fprintf(stderr, "Read unexpected embedded "
   				"symbol end marker in [%s] :\n>>\n%s<<\n", 
  -				filename, buf);
  +				name, line);
   	}
   	
           break;
  @@ -482,7 +502,7 @@
           break;	
   
         case(INFO_FONT): 
  -        o_text_set_info_font(buf);
  +        o_text_set_info_font(line);
           break;	
   
         case(COMMENT):
  @@ -490,7 +510,7 @@
           break;
   
         case(VERSION_CHAR):
  -        itemsread = sscanf(buf, "v %u %u\n", &release_ver, &fileformat_ver);
  +        itemsread = sscanf(line, "v %u %u\n", &release_ver, &fileformat_ver);
   
   	/* 20030921 was the last version which did not have a fileformat */
   	/* version.  The below latter test should not happen, but it is here */
  @@ -503,18 +523,17 @@
   	if (fileformat_ver < current_fileformat_ver)
           {
          	  s_log_message("Read an old format sym/sch file!\n"
  -                        "Please run g[sym|sch]update on:\n[%s]\n", filename);
  +                        "Please run g[sym|sch]update on:\n[%s]\n", name);
   	}
           break;
   
         default:
           fprintf(stderr, "Read garbage in [%s] :\n>>\n%s<<\n",
  -                filename, buf);
  +                name, line);
           break;
       }
   
     }
  -  fclose(fp);
   
     /* Was the very last thing we read a complex and has it not been checked */
     /* yet?  This would happen if the complex is at the very end of the file  */
  @@ -531,7 +550,47 @@
       }
     }
     
  +  tb = s_textbuffer_free(tb);
  +  
     return(object_list);
  +
  +}
  +
  +/*! \brief Read a file
  + *  \par Function Description
  + *  This function reads a file in libgead format.
  + *
  + *  \param [in,out] w_current    The current TOPLEVEL structure.
  + *  \param [in]     object_list  The object_list to read data to.
  + *  \param [in]     filename     The filename to read from.
  + *  \return object_list if successful read, or NULL on error.
  + */
  +OBJECT *o_read(TOPLEVEL *w_current, OBJECT *object_list, char *filename)
  +{
  +  GError *err = NULL;
  +  char *buffer = NULL;
  +  size_t size;
  +  OBJECT *result = NULL;
  +
  +  g_file_get_contents(filename, &buffer, &size, &err);
  +
  +  g_assert ((buffer == NULL && err != NULL) 
  +	    || (buffer != NULL && err == NULL));
  +
  +  if (err != NULL)
  +    {
  +      /* Report error to user, and free error */
  +      g_assert (buffer == NULL);
  +      fprintf (stderr, "o_read: Unable to read file: [%s]\n", err->message);
  +      g_error_free (err);
  +      return NULL;
  +    } 
  +
  +  /* Parse file contents */
  +  g_assert (buffer != NULL);
  +  result = o_read_buffer (w_current, object_list, buffer, size, filename);
  +  g_free (buffer);
  +  return result;
   }
   
   /*! \brief Scale a set of lines.
  
  
  
  1.42                 eda/geda/gaf/libgeda/src/o_attrib.c
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: o_attrib.c
  ===================================================================
  RCS file: /home/cvspsrv/cvsroot/eda/geda/gaf/libgeda/src/o_attrib.c,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -b -r1.41 -r1.42
  --- o_attrib.c	29 Apr 2007 12:01:21 -0000	1.41
  +++ o_attrib.c	28 May 2007 07:30:31 -0000	1.42
  @@ -647,37 +647,42 @@
   #endif
   }
   
  -/*! \brief Read attributes from a file.
  +/*! \brief Read attributes from a buffer.
    *  \par Function Description
  - *  Read attributes from a file.
  + *  Read attributes from a TextBuffer.
    *
    *  \param [in]  w_current              The TOPLEVEL object.
  - *  \param [in]  fp                     FILE pointer to read from.
    *  \param [out] object_to_get_attribs  Storage for attributes.
  + *  \param [in]  tb                     The text buffer to read from.
    *  \param [in]  release_ver            libgeda release version number.
    *  \param [in]  fileformat_ver         file format version number.
    *  \return Pointer to object_to_get_attribs.
    */
  -OBJECT *o_read_attribs(TOPLEVEL *w_current, FILE *fp, OBJECT *object_to_get_attribs,
  +OBJECT *o_read_attribs(TOPLEVEL *w_current, 
  +		       OBJECT *object_to_get_attribs,
  +   		       TextBuffer *tb,
   		       unsigned int release_ver, unsigned int fileformat_ver)
   {
     OBJECT *object_list=NULL;
  -  char buf[1024];
  +  char *line = NULL;
     char objtype;
     int ATTACH=FALSE;
     int saved_color = -1;
   
     object_list = object_to_get_attribs;
   
  -  while ( fgets(buf, 1024, fp) != NULL) {
  +  while (1) {
   
  -    sscanf(buf, "%c", &objtype);
  +    line = s_textbuffer_next_line (tb);
  +    if (line == NULL) break;
  +
  +    sscanf(line, "%c", &objtype);
       switch (objtype) {
   
         case(OBJ_LINE):
           object_list = (OBJECT *) o_line_read(w_current, 
                                                object_list,
  -                                             buf, 
  +                                             line, 
                                                release_ver, fileformat_ver);
           break;
   
  @@ -685,21 +690,21 @@
         case(OBJ_NET):
           object_list = (OBJECT *) o_net_read(w_current, 
                                               object_list, 
  -                                            buf, 
  +                                            line, 
                                               release_ver, fileformat_ver);
           break;
   
         case(OBJ_BUS):
           object_list = (OBJECT *) o_bus_read(w_current, 
                                               object_list, 
  -                                            buf, 
  +                                            line, 
                                               release_ver, fileformat_ver);
           break;
   
         case(OBJ_BOX):
           object_list = (OBJECT *) o_box_read(w_current, 
                                               object_list, 
  -                                            buf, 
  +                                            line, 
                                               release_ver, fileformat_ver);
           break;
   		
  @@ -707,7 +712,7 @@
           object_list = (OBJECT *) o_circle_read(
                                                  w_current, 
                                                  object_list, 
  -                                               buf, 
  +                                               line, 
                                                  release_ver, fileformat_ver);
           break;
   
  @@ -717,7 +722,7 @@
           object_list = (OBJECT *) o_complex_read(
                                                   w_current, 
                                                   object_list, 
  -                                                buf, 
  +                                                line, 
                                                   release_ver, fileformat_ver);
   
   				/* this is necessary because complex may add
  @@ -730,24 +735,25 @@
         case(OBJ_PIN):
           object_list = (OBJECT *) o_pin_read(w_current, 
                                               object_list, 
  -                                            buf, 
  +                                            line, 
                                               release_ver, fileformat_ver);
           break;
   
         case(OBJ_ARC):
           object_list = (OBJECT *) o_arc_read(w_current, 
                                               object_list, 
  -                                            buf, 
  +                                            line, 
                                               release_ver, fileformat_ver);
           break;
   
         case(OBJ_TEXT):
  -        /* fgets(string, 1024, fp); text strings are now read inside: */
  +	line = g_strdup (line);
           object_list = (OBJECT *) o_text_read(w_current,
                                                object_list, 
  -                                             buf, 
  -                                             fp, 
  +                                             line,
  +					     tb,
                                                release_ver, fileformat_ver);
  +	g_free (line);
           saved_color = object_list->color;
           ATTACH=TRUE;
   		
  
  
  
  1.8                  eda/geda/gaf/libgeda/src/o_picture.c
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: o_picture.c
  ===================================================================
  RCS file: /home/cvspsrv/cvsroot/eda/geda/gaf/libgeda/src/o_picture.c,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -b -r1.7 -r1.8
  --- o_picture.c	17 Apr 2007 20:19:21 -0000	1.7
  +++ o_picture.c	28 May 2007 07:30:31 -0000	1.8
  @@ -43,30 +43,32 @@
   
   /*! \brief Create picture OBJECT from character string.
    *  \par Function Description
  - *  This function will get the description of a picture from the character
  - *  string <B>*buf</B>. The new picture is then added to the list of object of
  - *  which <B>*object_list</B> is the last element before the call.
  - *  The function returns the new last element, that is the added
  - *  picture object.
  + *  This function will get the description of a picture from the
  + *  character string <B>*first_line</B>. The new picture is then added
  + *  to the list of object of which <B>*object_list</B> is the last
  + *  element before the call.  The function returns the new last
  + *  element, that is the added picture object.
    *
    *  \param [in]  w_current       The TOPLEVEL object.
    *  \param [out] object_list     OBJECT list to create picture in.
  - *  \param [in]  buf             Character string with picture description.
  - *  \param [in]  fp              Picture file to read.
  + *  \param [in]  first_line      Character string with picture description.
  + *  \param [in]  tb              Text buffer to load embedded data from.
    *  \param [in]  release_ver     libgeda release version number.
    *  \param [in]  fileformat_ver  libgeda file format version number.
    *  \return A pointer to the new picture object.
    */
   OBJECT *o_picture_read(TOPLEVEL *w_current, OBJECT *object_list,
  -		       char buf[], FILE *fp,
  -		       unsigned int release_ver,unsigned int fileformat_ver)
  +		       const char *first_line,
  +		       TextBuffer *tb,
  +		       unsigned int release_ver,
  +		       unsigned int fileformat_ver)
   {
     int x1, y1;
     int width, height, angle;
     gchar mirrored, embedded;
     int num_conv;
     gchar type;
  -  gchar buffer[MAX_TEXT_LINE_LENGTH]; 
  +  gchar *line = NULL;
     gchar *filename;
     GdkPixbuf *pixbuf;
     static char gdk_initialized=0;
  @@ -79,12 +81,12 @@
       gdk_initialized = 1;
     }
   
  -  num_conv = sscanf(buf, "%c %d %d %d %d %d %c %c\n",
  +  num_conv = sscanf(first_line, "%c %d %d %d %d %d %c %c\n",
   	 &type, &x1, &y1, &width, &height, &angle, &mirrored, &embedded);
     
     if (num_conv != 8) {
  -    fprintf(stderr, "Error reading picture definition line: %s.\n", buf);
  -    s_log_message ("Error reading picture definition line: %s.\n", buf);
  +    fprintf(stderr, "Error reading picture definition line: %s.\n", first_line);
  +    s_log_message ("Error reading picture definition line: %s.\n", first_line);
     }
   
     /* Convert from ascii character to number */
  @@ -137,9 +139,7 @@
   
     }
   
  -  fgets(buffer, 1024, fp);
  -  
  -  filename = g_strdup (buffer);
  +  filename = g_strdup(s_textbuffer_next_line(tb));
     filename = remove_last_nl(filename);	
       
     pixbuf = NULL;
  @@ -160,10 +160,12 @@
   
       /* Read the encoded picture */
       do {
  -      finished = 0;
  -      fgets(buffer, 1024, fp);
  -      if (g_strcasecmp(buffer, ".\n") != 0) {
  -	encoded_picture=g_string_append (encoded_picture, buffer);
  +
  +      line = s_textbuffer_next_line(tb);
  +      if (line == NULL) break;
  +
  +      if (g_strcasecmp(line, ".\n") != 0) {
  +	encoded_picture=g_string_append (encoded_picture, line);
   	encoded_picture=g_string_append (encoded_picture, "\n");
         }
         else {
  
  
  
  1.30                 eda/geda/gaf/libgeda/src/o_text_basic.c
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: o_text_basic.c
  ===================================================================
  RCS file: /home/cvspsrv/cvsroot/eda/geda/gaf/libgeda/src/o_text_basic.c,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -b -r1.29 -r1.30
  --- o_text_basic.c	14 May 2007 03:35:09 -0000	1.29
  +++ o_text_basic.c	28 May 2007 07:30:31 -0000	1.30
  @@ -1015,7 +1015,9 @@
    *
    */
   OBJECT *o_text_read(TOPLEVEL *w_current, OBJECT *object_list,
  -		    char buf[], FILE *fp, unsigned int release_ver,
  +		    const char *first_line,
  +		    TextBuffer *tb,
  +		    unsigned int release_ver,
   		    unsigned int fileformat_ver)
   {
     char type; 
  @@ -1032,21 +1034,21 @@
     GString *textstr;
   
     if (fileformat_ver == 1) {
  -    sscanf(buf, "%c %d %d %d %d %d %d %d %d %d\n", &type, &x, &y, 
  +    sscanf(first_line, "%c %d %d %d %d %d %d %d %d %d\n", &type, &x, &y, 
              &color, &size,
              &visibility, &show_name_value, 
              &angle, &alignment, &num_lines);	
     } else if (release_ver < VERSION_20000220) {
       /* yes, above less than (not less than and equal) is correct. The format */
       /* change occurred in 20000220 */
  -    sscanf(buf, "%c %d %d %d %d %d %d %d\n", &type, &x, &y, 
  +    sscanf(first_line, "%c %d %d %d %d %d %d %d\n", &type, &x, &y, 
              &color, &size,
              &visibility, &show_name_value, 
              &angle);	
       alignment = LOWER_LEFT; /* older versions didn't have this */
       num_lines = 1; /* only support a single line */
     } else {
  -    sscanf(buf, "%c %d %d %d %d %d %d %d %d\n", &type, &x, &y, 
  +    sscanf(first_line, "%c %d %d %d %d %d %d %d %d\n", &type, &x, &y, 
              &color, &size,
              &visibility, &show_name_value, 
              &angle, &alignment);	
  @@ -1097,8 +1099,8 @@
     }
   
     if (color < 0 || color > MAX_COLORS) {
  -    fprintf(stderr, "Found an invalid color [ %s ]\n", buf);
  -    s_log_message("Found an invalid color [ %s ]\n", buf);
  +    fprintf(stderr, "Found an invalid color [ %s ]\n", first_line);
  +    s_log_message("Found an invalid color [ %s ]\n", first_line);
       s_log_message("Setting color to WHITE\n");
       color = WHITE;
     }
  @@ -1107,11 +1109,14 @@
   
     textstr = g_string_new ("");
     for (i = 0; i < num_lines; i++) {
  -    gchar buffer[MAX_TEXT_LINE_LENGTH];
  +    gchar *line;
       
  -    fgets (buffer, MAX_TEXT_LINE_LENGTH, fp);
  +    line = s_textbuffer_next_line (tb);
   
  -    textstr = g_string_append (textstr, buffer);
  +    if (line != NULL)
  +      {
  +	textstr = g_string_append (textstr, line);
  +      }
     }
     /* retrieve the character string from the GString */
     string = g_string_free (textstr, FALSE);
  
  
  


_______________________________________________
geda-cvs mailing list
geda-cvs@xxxxxxxxxxxxxx
http://www.seul.org/cgi-bin/mailman/listinfo/geda-cvs