aboutsummaryrefslogtreecommitdiff
path: root/src/main/extractor_print.c
blob: 35304305dd3083a3821b8aa59b0578d18977e499 (plain) (blame)
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
/*
     This file is part of libextractor.
     Copyright (C) 2002, 2003, 2004, 2005, 2006, 2009 Vidyut Samanta and Christian Grothoff

     libextractor 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, or (at your
     option) any later version.

     libextractor 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 libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
 */
/**
 * @file main/extractor_print.c
 * @brief convenience functions for printing meta data
 * @author Christian Grothoff
 */
#include "platform.h"
#include "extractor.h"
#include "extractor_logging.h"
#if HAVE_ICONV
#include "iconv.c"
#endif

/**
 * Simple EXTRACTOR_MetaDataProcessor implementation that simply
 * prints the extracted meta data to the given file.  Only prints
 * those keywords that are in UTF-8 format.
 * 
 * @param handle the file to write to (stdout, stderr), must NOT be NULL,
 *               must be of type "FILE *".
 * @param plugin_name name of the plugin that produced this value
 * @param type libextractor-type describing the meta data
 * @param format basic format information about data 
 * @param data_mime_type mime-type of data (not of the original file);
 *        can be NULL (if mime-type is not known)
 * @param data actual meta-data found
 * @param data_len number of bytes in data
 * @return non-zero if printing failed, otherwise 0.
 */
int 
EXTRACTOR_meta_data_print (void *handle,
			   const char *plugin_name,
			   enum EXTRACTOR_MetaType type,
			   enum EXTRACTOR_MetaFormat format,
			   const char *data_mime_type,
			   const char *data,
			   size_t data_len)
{
#if HAVE_ICONV
  iconv_t cd;
#endif
  char * buf;
  int ret;
  const char *mt;

  if (EXTRACTOR_METAFORMAT_UTF8 != format)
    return 0;
#if HAVE_ICONV
  cd = iconv_open (nl_langinfo(CODESET),
		   "UTF-8");
  if (((iconv_t) -1) == cd)
    {
      LOG_STRERROR ("iconv_open");
      return 1;
    }
  buf = iconv_helper (cd, data, data_len);
  if (NULL == buf)
    {
      LOG_STRERROR ("iconv_helper");
      ret = -1;
    }
  else
    {
      mt = EXTRACTOR_metatype_to_string (type);
      ret = fprintf (handle,
		     "%s - %s\n",
		     (NULL == mt) 
		     ? dgettext ("libextractor", gettext_noop ("unknown"))
		     : dgettext ("libextractor", mt),
		     buf);
      free(buf);
    }
  iconv_close(cd);
#else
  ret = fprintf (handle,
		 "%s - %.*s\n",
		 (NULL == mt) 
		 ? dgettext ("libextractor", gettext_noop ("unknown"))
		 : dgettext ("libextractor", mt),
		 (int) data_len,
		 data);
#endif
  return (ret < 0) ? 1 : 0;
}

/* end of extractor_print.c */