Happy new year
[yazproxy-moved-to-github.git] / src / yaz-usemarcon.cpp
1 /* This file is part of YAZ proxy
2    Copyright (C) 1998-2009 Index Data
3
4 YAZ proxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include <yaz/log.h>
20 #include "proxyp.h"
21
22 Yaz_usemarcon::Yaz_usemarcon()
23 {
24 #if HAVE_USEMARCON
25     m_stage1 = 0;
26     m_stage2 = 0;
27 #endif
28 }
29
30 Yaz_usemarcon::~Yaz_usemarcon()
31 {
32 #if HAVE_USEMARCON
33     delete m_stage1;
34     delete m_stage2;
35 #endif
36 }
37
38 int Yaz_usemarcon::convert(const char *stage1, const char *stage2,
39                            const char *input, int input_len,
40                            char **output, int *output_len)
41 {
42 #if HAVE_USEMARCON
43     if (stage1 && *stage1)
44     {
45         char *converted;
46         int convlen;
47         if (!m_stage1)
48         {
49             m_stage1 = new Usemarcon();
50         }
51         m_stage1->SetIniFileName(stage1);
52         m_stage1->SetMarcRecord((char*) input, input_len);
53         int res = m_stage1->Convert();
54         if (res == 0)
55         {
56             m_stage1->GetMarcRecord(converted, convlen);
57             if (stage2 && *stage2)
58             {
59                 if (!m_stage2)
60                 {
61                     m_stage2 = new Usemarcon();
62                 }
63                 m_stage2->SetIniFileName(stage2);
64                 m_stage2->SetMarcRecord(converted, convlen);
65                 res = m_stage2->Convert();
66                 if (res == 0)
67                 {
68                     free(converted);
69                     m_stage2->GetMarcRecord(converted, convlen);
70                 }
71                 else
72                 {
73                     yaz_log(YLOG_LOG, "USEMARCON stage 2 error %d", res);
74                     return 0;
75                 }
76             }
77             *output = converted;
78             *output_len = convlen;
79             return 1;
80         }
81         else
82         {
83             yaz_log(YLOG_LOG, "USEMARCON stage 1 error %d", res);
84         }
85     }
86 #endif
87     return 0;
88 }
89 /*
90  * Local variables:
91  * c-basic-offset: 4
92  * indent-tabs-mode: nil
93  * End:
94  * vim: shiftwidth=4 tabstop=8 expandtab
95  */
96