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