Skip to content

Commit

Permalink
Don't complain about malformed headers when updating from sources.
Browse files Browse the repository at this point in the history
If the headers differ in any way (e.g. some have Plural-Forms added by
xgettext and some not), msgcat will include all versions, delimited with
"#-#-#-#-# (filename) #-#-#-#-#". Poedit's parser then reports these as
"Malformed header".

To add insult to the injury, Poedit discards the header when it's done
with loading the resulting big catalog, so the error is completely
unnecessary. So just ignore the header when loading this particular file
- it was generated under our control, so it should be safe enough.

Fixes vslavik#415.
  • Loading branch information
vslavik committed Aug 29, 2012
1 parent b712d25 commit 6fcd186
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
35 changes: 23 additions & 12 deletions src/catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,13 +655,17 @@ bool CatalogParser::Parse()
}
mtranslations.Add(str);

if (!OnEntry(mstr, wxEmptyString, false,
has_context, msgctxt,
mtranslations,
mflags, mrefs, mcomment, mautocomments, msgid_old,
mlinenum))
bool shouldIgnore = m_ignoreHeader && mstr.empty();
if ( !shouldIgnore )
{
return false;
if (!OnEntry(mstr, wxEmptyString, false,
has_context, msgctxt,
mtranslations,
mflags, mrefs, mcomment, mautocomments, msgid_old,
mlinenum))
{
return false;
}
}

mcomment = mstr = msgid_plural = msgctxt = mflags = wxEmptyString;
Expand Down Expand Up @@ -937,9 +941,9 @@ Catalog::~Catalog()
}


Catalog::Catalog(const wxString& po_file)
Catalog::Catalog(const wxString& po_file, int flags)
{
m_isOk = Load(po_file);
m_isOk = Load(po_file, flags);
}


Expand Down Expand Up @@ -1002,7 +1006,7 @@ void Catalog::CreateNewHeader(const Catalog::HeaderData& pot_header)
}


bool Catalog::Load(const wxString& po_file)
bool Catalog::Load(const wxString& po_file, int flags)
{
wxTextFile f;

Expand All @@ -1016,9 +1020,12 @@ bool Catalog::Load(const wxString& po_file)
if (!f.Open(po_file, wxConvISO8859_1))
return false;

CharsetInfoFinder charsetFinder(&f);
charsetFinder.Parse();
m_header.Charset = charsetFinder.GetCharset();
{
wxLogNull null; // don't report parsing errors from here, report them later
CharsetInfoFinder charsetFinder(&f);
charsetFinder.Parse();
m_header.Charset = charsetFinder.GetCharset();
}

f.Close();
wxCSConv encConv(m_header.Charset);
Expand All @@ -1031,6 +1038,7 @@ bool Catalog::Load(const wxString& po_file)
}

LoadParser parser(this, &f);
parser.IgnoreHeader(flags & CreationFlag_IgnoreHeader);
if (!parser.Parse())
{
wxLogError(
Expand All @@ -1056,6 +1064,9 @@ bool Catalog::Load(const wxString& po_file)

f.Close();

if ( flags & CreationFlag_IgnoreHeader )
CreateNewHeader();

return true;
}

Expand Down
26 changes: 23 additions & 3 deletions src/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,18 @@ class Catalog
const Entry *Find(const wxString& key) const;
};

enum CreationFlags
{
CreationFlag_IgnoreHeader = 1
};

/// Default ctor. Creates empty catalog, you have to call Load.
Catalog();

/// Ctor that loads the catalog from \a po_file with Load.
Catalog(const wxString& po_file);
/// \a flags is CreationFlags combination.
Catalog(const wxString& po_file, int flags = 0);

~Catalog();

/** Creates new, empty header. Sets Charset to something meaningful
Expand All @@ -478,8 +486,10 @@ class Catalog
file contains parts of catalog header data that are not part
of standard .po format, namely SearchPaths, Keywords, BasePath
and Language.
@param flags CreationFlags combination.
*/
bool Load(const wxString& po_file);
bool Load(const wxString& po_file, int flags = 0);

/** Saves catalog to file. Creates both .po (text) and .mo (binary)
version of the catalog (unless the latter was disabled in
Expand Down Expand Up @@ -628,9 +638,16 @@ class Catalog
class CatalogParser
{
public:
CatalogParser(wxTextFile *f) : m_textFile(f) {}
CatalogParser(wxTextFile *f)
: m_textFile(f),
m_ignoreHeader(false)
{}

virtual ~CatalogParser() {}

/// Tell the parser to ignore header entries when processing
void IgnoreHeader(bool ignore) { m_ignoreHeader = ignore; }

/** Parses the entire file, calls OnEntry each time
new msgid/msgstr pair is found.
Expand Down Expand Up @@ -672,6 +689,9 @@ class CatalogParser

/// Textfile being parsed.
wxTextFile *m_textFile;

/// Whether the header should be parsed or not
bool m_ignoreHeader;
};

#endif // _CATALOG_H_
6 changes: 2 additions & 4 deletions src/digger.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of Poedit (http://www.poedit.net)
*
* Copyright (C) 2000-2005 Vaclav Slavik
* Copyright (C) 2000-2012 Vaclav Slavik
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -107,7 +107,7 @@ Catalog *SourceDigger::Dig(const wxArrayString& paths,
if ( !ConcatCatalogs(partials, mergedFile) )
return NULL;

Catalog *c = new Catalog(mergedFile);
Catalog *c = new Catalog(mergedFile, Catalog::CreationFlag_IgnoreHeader);

if ( !c->IsOk() )
{
Expand All @@ -116,8 +116,6 @@ Catalog *SourceDigger::Dig(const wxArrayString& paths,
return NULL;
}

c->CreateNewHeader();

return c;
}

Expand Down

0 comments on commit 6fcd186

Please sign in to comment.