~ubuntu-branches/ubuntu/precise/ncbi-tools6/precise

« back to all changes in this revision

Viewing changes to algo/blast/core/blast_hspstream.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2005-03-27 12:00:15 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050327120015-embhesp32nj73p9r
Tags: 6.1.20041020-3
* Fix FTBFS under GCC 4.0 caused by inconsistent use of "static" on
  functions.  (Closes: #295110.)
* Add a watch file, now that we can.  (Upstream's layout needs version=3.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  $Id: blast_hspstream.c,v 1.3 2004/06/07 17:06:49 dondosha Exp $
 
2
 * ===========================================================================
 
3
 *
 
4
 *                            PUBLIC DOMAIN NOTICE
 
5
 *               National Center for Biotechnology Information
 
6
 *
 
7
 *  This software/database is a "United States Government Work" under the
 
8
 *  terms of the United States Copyright Act.  It was written as part of
 
9
 *  the author's official duties as a United States Government employee and
 
10
 *  thus cannot be copyrighted.  This software/database is freely available
 
11
 *  to the public for use. The National Library of Medicine and the U.S.
 
12
 *  Government have not placed any restriction on its use or reproduction.
 
13
 *
 
14
 *  Although all reasonable efforts have been taken to ensure the accuracy
 
15
 *  and reliability of the software and data, the NLM and the U.S.
 
16
 *  Government do not and cannot warrant the performance or results that
 
17
 *  may be obtained by using this software or data. The NLM and the U.S.
 
18
 *  Government disclaim all warranties, express or implied, including
 
19
 *  warranties of performance, merchantability or fitness for any particular
 
20
 *  purpose.
 
21
 *
 
22
 *  Please cite the author in any work or product based on this material.
 
23
 *
 
24
 * ===========================================================================
 
25
 *
 
26
 * Author:  Christiam Camacho
 
27
 *
 
28
 */
 
29
 
 
30
/** @file blast_hspstream.c
 
31
 * Definition of ADT to save and retrieve lists of HSPs in the BLAST engine.
 
32
 */
 
33
 
 
34
static char const rcsid[] = 
 
35
    "$Id: blast_hspstream.c,v 1.3 2004/06/07 17:06:49 dondosha Exp $";
 
36
 
 
37
#include <algo/blast/core/blast_hspstream.h>
 
38
#include <algo/blast/core/blast_def.h>      /* needed for sfree */
 
39
 
 
40
/** Complete type definition of Blast Hsp Stream ADT */
 
41
struct BlastHSPStream {
 
42
   BlastHSPStreamConstructor NewFnPtr;    /**< Constructor */
 
43
   BlastHSPStreamDestructor  DeleteFnPtr; /**< Destructor */
 
44
   
 
45
   /* The operational interface */
 
46
   
 
47
   BlastHSPStreamMethod      WriteFnPtr;  /**< Write to BlastHSPStream */
 
48
   BlastHSPStreamMethod      ReadFnPtr;   /**< Read from BlastHSPStream */
 
49
   BlastHSPStreamCloseFnType CloseFnPtr;  /**< Close BlastHSPStream for
 
50
                                             writing */
 
51
   void* DataStructure;                   /**< ADT holding HSPStream */
 
52
};
 
53
 
 
54
BlastHSPStream* BlastHSPStreamNew(const BlastHSPStreamNewInfo* bhsn_info)
 
55
{
 
56
    BlastHSPStream* retval = NULL;
 
57
    BlastHSPStreamFunctionPointerTypes fnptr;
 
58
 
 
59
    if ( bhsn_info == NULL ) {
 
60
        return NULL;
 
61
    }
 
62
 
 
63
    if ( !(retval = (BlastHSPStream*) calloc(1, sizeof(BlastHSPStream)))) {
 
64
        return NULL;
 
65
    }
 
66
 
 
67
    /* Save the constructor and invoke it */
 
68
    fnptr.ctor = bhsn_info->constructor;
 
69
    SetMethod(retval, eConstructor, fnptr);
 
70
    if (retval->NewFnPtr) {
 
71
        retval = (*retval->NewFnPtr)(retval, bhsn_info->ctor_argument);
 
72
    } else {
 
73
        sfree(retval);
 
74
    }
 
75
 
 
76
    ASSERT(retval->DeleteFnPtr);
 
77
    ASSERT(retval->WriteFnPtr);
 
78
    ASSERT(retval->ReadFnPtr);
 
79
 
 
80
    return retval;
 
81
}
 
82
 
 
83
BlastHSPStream* BlastHSPStreamFree(BlastHSPStream* hsp_stream)
 
84
{
 
85
    BlastHSPStreamDestructor destructor_fnptr = NULL;
 
86
 
 
87
    if (!hsp_stream) {
 
88
        return (BlastHSPStream*) NULL;
 
89
    }
 
90
 
 
91
    if ( !(destructor_fnptr = (*hsp_stream->DeleteFnPtr))) {
 
92
        sfree(hsp_stream);
 
93
        return NULL;
 
94
    }
 
95
 
 
96
    return (BlastHSPStream*) (*destructor_fnptr)(hsp_stream);
 
97
}
 
98
 
 
99
void BlastHSPStreamClose(BlastHSPStream* hsp_stream)
 
100
{
 
101
    BlastHSPStreamCloseFnType close_fnptr = NULL;
 
102
 
 
103
    if (!hsp_stream)
 
104
       return;
 
105
 
 
106
    /** Close functionality is optional. If closing function is not provided,
 
107
        just do nothing. */
 
108
    if ( !(close_fnptr = (*hsp_stream->CloseFnPtr))) {
 
109
       return;
 
110
    }
 
111
 
 
112
    (*close_fnptr)(hsp_stream);
 
113
}
 
114
 
 
115
const int kBlastHSPStream_Error = -1;
 
116
const int kBlastHSPStream_Success = 0;
 
117
const int kBlastHSPStream_Eof = 1;
 
118
 
 
119
/** This method is akin to a vtable dispatcher, invoking the method registered
 
120
 * upon creation of the implementation of the BlastHSPStream interface 
 
121
 * @param hsp_stream The BlastHSPStream object [in]
 
122
 * @param name Name of the method to invoke on hsp_stream [in]
 
123
 * @param arg Arbitrary argument passed to the method name [in]
 
124
 * @return kBlastHSPStream_Error on NULL hsp_stream or NULL method pointer 
 
125
 * (i.e.: unimplemented or uninitialized method on the BlastHSPStream 
 
126
 * interface) or return value of the implementation.
 
127
 */
 
128
static int 
 
129
_MethodDispatcher(BlastHSPStream* hsp_stream, EMethodName name, 
 
130
                  BlastHSPList** hsp_list)
 
131
{
 
132
    BlastHSPStreamMethod method_fnptr = NULL;
 
133
 
 
134
    if (!hsp_stream) {
 
135
        return kBlastHSPStream_Error;
 
136
    }
 
137
 
 
138
    ASSERT(name < eMethodBoundary); 
 
139
 
 
140
    switch (name) {
 
141
    case eRead:
 
142
        method_fnptr = (*hsp_stream->ReadFnPtr);
 
143
        break;
 
144
 
 
145
    case eWrite:
 
146
        method_fnptr = (*hsp_stream->WriteFnPtr);
 
147
        break;
 
148
 
 
149
    default:
 
150
        abort();    /* should never happen */
 
151
    }
 
152
 
 
153
    if (!method_fnptr) {
 
154
        return kBlastHSPStream_Error;
 
155
    }
 
156
 
 
157
    return (*method_fnptr)(hsp_stream, hsp_list);
 
158
}
 
159
 
 
160
int BlastHSPStreamRead(BlastHSPStream* hsp_stream, BlastHSPList** hsp_list)
 
161
{
 
162
    return _MethodDispatcher(hsp_stream, eRead, hsp_list);
 
163
}
 
164
 
 
165
int BlastHSPStreamWrite(BlastHSPStream* hsp_stream, BlastHSPList** hsp_list)
 
166
{
 
167
    return _MethodDispatcher(hsp_stream, eWrite, hsp_list);
 
168
}
 
169
 
 
170
/*****************************************************************************/
 
171
 
 
172
void* GetData(BlastHSPStream* hsp_stream)
 
173
{
 
174
    if ( !hsp_stream ) {
 
175
        return NULL;
 
176
    }
 
177
 
 
178
    return hsp_stream->DataStructure;
 
179
}
 
180
 
 
181
int SetData(BlastHSPStream* hsp_stream, void* data)
 
182
{
 
183
    if ( !hsp_stream ) {
 
184
        return kBlastHSPStream_Error;
 
185
    }
 
186
 
 
187
    hsp_stream->DataStructure = data;
 
188
 
 
189
    return kBlastHSPStream_Success;
 
190
}
 
191
 
 
192
int SetMethod(BlastHSPStream* hsp_stream, 
 
193
              EMethodName name,
 
194
              BlastHSPStreamFunctionPointerTypes fnptr_type)
 
195
{
 
196
    if ( !hsp_stream ) {
 
197
        return kBlastHSPStream_Error;
 
198
    }
 
199
 
 
200
    ASSERT(name < eMethodBoundary); 
 
201
 
 
202
    switch (name) {
 
203
    case eConstructor:
 
204
        hsp_stream->NewFnPtr = fnptr_type.ctor;
 
205
        break;
 
206
 
 
207
    case eDestructor:
 
208
        hsp_stream->DeleteFnPtr = fnptr_type.dtor;
 
209
        break;
 
210
 
 
211
    case eRead:
 
212
        hsp_stream->ReadFnPtr = fnptr_type.method;
 
213
        break;
 
214
 
 
215
    case eWrite:
 
216
        hsp_stream->WriteFnPtr = fnptr_type.method;
 
217
        break;
 
218
 
 
219
    case eClose:
 
220
       hsp_stream->CloseFnPtr = fnptr_type.closeFn;
 
221
       break;
 
222
 
 
223
    default:
 
224
        abort();    /* should never happen */
 
225
    }
 
226
 
 
227
    return kBlastHSPStream_Success;
 
228
}