File: pospell_read.c

package info (click to toggle)
spellutils 0.7-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze, wheezy
  • size: 1,052 kB
  • ctags: 360
  • sloc: ansic: 3,981; sh: 3,300; makefile: 242; sed: 93
file content (151 lines) | stat: -rw-r--r-- 3,500 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) 2000 Byrial Jensen <byrial@image.dk>
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pospell.h"

/* Static functions in this file */
static void do_body (FILE *pofile, FILE *spellfile, FILE *mergefile,
		     int strict);
static void unsplit (char *src, char *dest);

void readbody (FILE *pofile, FILE *spellfile, FILE *mergefile, int strict)
{
  do_body (pofile, spellfile, mergefile, strict);

  nb_close (pofile);
  nb_close (spellfile);
  nb_close (mergefile);
}

static void do_body (FILE *pofile, FILE *spellfile, FILE *mergefile,
		     int strict)
{
  char *line = NULL;
  char *poline = NULL;
  size_t len = 0;
  size_t poline_len = 0;
  char type;
  char *c;

  while ((line = read_line (line, &len, spellfile)) != NULL)
  {
    if (poline_len < len)
    {
      /* Need room to call unsplit() */
      poline_len = len;
      poline = nb_realloc (poline, poline_len);
    }

    if (*line == '#' || *line == '>' || *line == ')' ||
	*line == ']' || *line == '+')
    {
      type = *line;

      while (1)
      {
	poline = read_line (poline, &poline_len, pofile);
	if (!line)
	  nb_error (0,
		    _("found marker for a removed item in the spell file,\n"
		      "but cannot find a corresponding item in the po file"));
	for (c = poline ; *c == ' ' || *c == '\t' ; c++)
	  ;

	if (type == '#')
	{
	  if (*c == '#')
	    break;
	}
	else if (type == '>')
	{
	  if (strncmp (c, "msgid", 5) == 0)
	    break;
	}
	else if (type == ')' || type == '+')
	{
	  if (strncmp (c, "msgstr", 6) == 0)
	    break;
	}
	else /* type == ']' */
	{
	  if (*c == '"')
	    break;
	}
      }
      if (type == '+')
      {
	char *q = strchr (c, '"');

	if (q)
	  *q = '\0';
	else
	  c[strlen (c) - 1] = '\0';  /* Removes \n from string */
	write_line (poline, mergefile);
	
	unsplit (line, poline);
      }
      write_line (poline, mergefile);
    }
    else if (*line == '"')
    {
      unsplit (line, poline);
      write_line (poline, mergefile);
    }
    else if (*line == '\n')
      write_line (line, mergefile);
    else if (*line == '?' && !strict)
    {
      if (line[1] == '\n')
	write_line (line + 1, mergefile);
      else
	write_line (line + 2, mergefile);
    }
    else
    {
      line[strlen (line) - 1] = '\0';
      nb_error (0, _("found line with unknown content in the spell file:\n"
		     "%s"), line);
    }
  }
}

/*
 * Remove leading text and unsplit splitted source strings
 */
static void unsplit (char *src, char *dest)
{
  while (*src && *src != '"')
    src++;

  while ((*(dest++) = *(src++)))
  {
    if (*src == '\\')
    {
      *(dest++) = *(src++);
      if (*src)
	continue;
      else
	break;
    }
    if (src[0] == '"' && src[1] == ' ' && src[2] == '"')
      src += 3;
  }
}