Beispiel #1
0
/** Convert <b>po</b> from the PO format to a TS-formatted XML document.
 * <b>ts</b> will be set to the resulting TS document. Return the number of
 * converted strings on success, or -1 on error and <b>errorMessage</b> will
 * be set. */
int
po2ts(QTextStream *po, QDomDocument *ts, QString *errorMessage)
{
  QString line;
  QString msgctxt, msgid, msgstr;
  QHash<QString,QDomElement> contextElements;
  QDomElement contextElement, msgElement, transElement;
  int n_strings = 0;

  Q_ASSERT(po);
  Q_ASSERT(ts);
  Q_ASSERT(errorMessage);

  *ts = new_ts_document();
  
  skip_po_header(po);
  line = read_next_line(po);
  while (!po->atEnd()) {
    /* Ignore all "#" lines except "#:" */
    while (line.startsWith("#")) {
      if (line.startsWith("#:")) {
        /* Context was specified with the stupid overloaded "#:" syntax.*/
        msgctxt = line.section(" ", 1);
        msgctxt = parse_message_context_lame(msgctxt);
      }
      line = read_next_line(po);
    }

    /* A context specified on a "msgctxt" line takes precedence over a context
     * specified using the overload "#:" notation. */
    if (line.startsWith("msgctxt ")) {    
      msgctxt = line.section(" ", 1);
      msgctxt = parse_message_context(msgctxt);
      line = read_next_line(po);
    }
    
    /* Parse the (possibly multiline) message source string */
    if (!line.startsWith("msgid ")) {
      *errorMessage = "expected 'msgid' line";
      return -1;
    }
    msgid = line.section(" ", 1);
    
    line = read_next_line(po);
    while (line.startsWith("\"")) {
      msgid.append(line);
      line = read_next_line(po);
    }
    msgid = parse_message_string(msgid);

    /* Parse the (possibly multiline) translated string */
    if (!line.startsWith("msgstr ")) {
      *errorMessage = "expected 'msgstr' line";
      return -1;
    }
    msgstr = line.section(" ", 1);
    
    line = read_next_line(po);
    while (line.startsWith("\"")) {
      msgstr.append(line);
      line = read_next_line(po);
    }
    msgstr = parse_message_string(msgstr);

    /* Add the message and translation to the .ts document */
    if (contextElements.contains(msgctxt)) {
      contextElement = contextElements.value(msgctxt);
    } else {
      contextElement = new_context_element(ts, msgctxt);
      ts->documentElement().appendChild(contextElement);
      contextElements.insert(msgctxt, contextElement);
    }
    contextElement.appendChild(new_message_element(ts, msgid, msgstr)); 
    
    n_strings++;
  }
  return n_strings;
}
Beispiel #2
0
static int parse_input_file(const char *file_name, struct errors **top_error,
                            struct languages **top_lang)
{
    FILE *file;
    char *str, buff[1000];
    struct errors *current_error= 0, **tail_error= top_error;
    struct message current_message;
    int rcount= 0; /* Number of error codes in current section. */
    int ecount= 0; /* Number of error codes in total. */
    DBUG_ENTER("parse_input_file");

    *top_error= 0;
    *top_lang= 0;
    if (!(file= my_fopen(file_name, O_RDONLY | O_SHARE, MYF(MY_WME))))
        DBUG_RETURN(0);

    while ((str= fgets(buff, sizeof(buff), file)))
    {
        if (is_prefix(str, "language"))
        {
            if (!(*top_lang= parse_charset_string(str)))
            {
                fprintf(stderr, "Failed to parse the charset string!\n");
                DBUG_RETURN(0);
            }
            continue;
        }
        if (is_prefix(str, "start-error-number"))
        {
            if (!(er_offset= parse_error_offset(str)))
            {
                fprintf(stderr, "Failed to parse the error offset string!\n");
                DBUG_RETURN(0);
            }
            rcount= 0; /* Reset count if a fixed number is set. */
            continue;
        }
        if (is_prefix(str, "default-language"))
        {
            if (!(default_language= parse_default_language(str)))
            {
                DBUG_PRINT("info", ("default_slang: %s", default_language));
                fprintf(stderr,
                        "Failed to parse the default language line. Aborting\n");
                DBUG_RETURN(0);
            }
            continue;
        }

        if (*str == '\t' || *str == ' ')
        {
            /* New error message in another language for previous error */
            if (!current_error)
            {
                fprintf(stderr, "Error in the input file format\n");
                DBUG_RETURN(0);
            }
            if (!parse_message_string(&current_message, str))
            {
                fprintf(stderr, "Failed to parse message string for error '%s'",
                        current_error->er_name);
                DBUG_RETURN(0);
            }
            if (find_message(current_error, current_message.lang_short_name, TRUE))
            {
                fprintf(stderr, "Duplicate message string for error '%s'"
                        " in language '%s'\n",
                        current_error->er_name, current_message.lang_short_name);
                DBUG_RETURN(0);
            }
            if (check_message_format(current_error, current_message.text))
            {
                fprintf(stderr, "Wrong formatspecifier of error message string"
                        " for error '%s' in language '%s'\n",
                        current_error->er_name, current_message.lang_short_name);
                DBUG_RETURN(0);
            }
            if (insert_dynamic(&current_error->msg, &current_message))
                DBUG_RETURN(0);
            continue;
        }
        if (is_prefix(str, ER_PREFIX) || is_prefix(str, WARN_PREFIX))
        {
            if (!(current_error= parse_error_string(str, rcount)))
            {
                fprintf(stderr, "Failed to parse the error name string\n");
                DBUG_RETURN(0);
            }
            rcount++;
            ecount++;                         /* Count number of unique errors */

            /* add error to the list */
            *tail_error= current_error;
            tail_error= &current_error->next_error;
            continue;
        }
        if (*str == '#' || *str == '\n')
            continue;					/* skip comment or empty lines */

        fprintf(stderr, "Wrong input file format. Stop!\nLine: %s\n", str);
        DBUG_RETURN(0);
    }
    *tail_error= 0;				/* Mark end of list */

    my_fclose(file, MYF(0));
    DBUG_RETURN(ecount);
}
Beispiel #3
0
static int parse_input_file(const char *file_name, struct errors **top_error,
			    struct languages **top_lang)
{
  FILE *file;
  char *str, buff[1000];
  struct errors *current_error= 0, **tail_error= top_error;
  struct message current_message;
  uint rcount= 0;
  my_bool er_offset_found= 0;
  DBUG_ENTER("parse_input_file");

  *top_error= 0;
  *top_lang= 0;
  if (!(file= my_fopen(file_name, O_RDONLY | O_SHARE, MYF(MY_WME))))
    DBUG_RETURN(0);

  while ((str= fgets(buff, sizeof(buff), file)))
  {
    if (is_prefix(str, "language"))
    {
      if (!(*top_lang= parse_charset_string(str)))
      {
	fprintf(stderr, "Failed to parse the charset string!\n");
	DBUG_RETURN(0);
      }
      continue;
    }
    if (is_prefix(str, "start-error-number"))
    {
      uint tmp_er_offset;
      if (!(tmp_er_offset= parse_error_offset(str)))
      {
	fprintf(stderr, "Failed to parse the error offset string!\n");
	DBUG_RETURN(0);
      }
      if (!er_offset_found)
      {
        er_offset_found= 1;
        er_offset= tmp_er_offset;
      }
      else
      {
        /* Create empty error messages between er_offset and tmp_err_offset */
        if (tmp_er_offset < er_offset + rcount)
        {
          fprintf(stderr, "new start-error-number %u is smaller than current error message: %u\n", tmp_er_offset, er_offset + rcount);
          DBUG_RETURN(0);
        }
        for ( ; er_offset + rcount < tmp_er_offset ; rcount++)
        {
          current_error= generate_empty_message(er_offset + rcount);
          *tail_error= current_error;
          tail_error= &current_error->next_error;
        }
      }
      continue;
    }
    if (is_prefix(str, "default-language"))
    {
      if (!(default_language= parse_default_language(str)))
      {
	DBUG_PRINT("info", ("default_slang: %s", default_language));
	fprintf(stderr,
		"Failed to parse the default language line. Aborting\n");
	DBUG_RETURN(0);
      }
      continue;
    }

    if (*str == '\t' || *str == ' ')
    {
      /* New error message in another language for previous error */
      if (!current_error)
      {
	fprintf(stderr, "Error in the input file format\n");
	DBUG_RETURN(0);
      }
      if (!parse_message_string(&current_message, str))
      {
	fprintf(stderr, "Failed to parse message string for error '%s'",
		current_error->er_name);
	DBUG_RETURN(0);
      }
      if (find_message(current_error, current_message.lang_short_name, TRUE))
      {
	fprintf(stderr, "Duplicate message string for error '%s'"
                        " in language '%s'\n",
		current_error->er_name, current_message.lang_short_name);
	DBUG_RETURN(0);
      }
      if (check_message_format(current_error, current_message.text))
      {
	fprintf(stderr, "Wrong formatspecifier of error message string"
                        " for error '%s' in language '%s'\n",
		current_error->er_name, current_message.lang_short_name);
	DBUG_RETURN(0);
      }
      if (insert_dynamic(&current_error->msg, (uchar *) & current_message))
	DBUG_RETURN(0);
      continue;
    }
    if (is_prefix(str, ER_PREFIX) || is_prefix(str, WARN_PREFIX) ||
        is_prefix(str, ER_PREFIX2))
    {
      if (!(current_error= parse_error_string(str, rcount)))
      {
	fprintf(stderr, "Failed to parse the error name string\n");
	DBUG_RETURN(0);
      }
      rcount++;                         /* Count number of unique errors */

      /* add error to the list */
      *tail_error= current_error;
      tail_error= &current_error->next_error;
      continue;
    }
    if (*str == '#' || *str == '\n')
      continue;                      	/* skip comment or empty lines */

    fprintf(stderr, "Wrong input file format. Stop!\nLine: %s\n", str);
    DBUG_RETURN(0);
  }
  *tail_error= 0;			/* Mark end of list */

  my_fclose(file, MYF(0));
  DBUG_RETURN(rcount);
}