update j2k_lib with new opj type
[openjpeg.git] / applications / jpip / libopenjpip / manfbox_manager.c
1 /*
2  * $Id: manfbox_manager.c 44 2011-02-15 12:32:29Z kaori $
3  *
4  * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2011, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include "manfbox_manager.h"
35
36 #ifdef SERVER
37 #include "fcgi_stdio.h"
38 #define logstream FCGI_stdout
39 #else
40 #define FCGI_stdout stdout
41 #define FCGI_stderr stderr
42 #define logstream stderr
43 #endif /*SERVER */
44
45 manfbox_param_t * gene_manfbox( box_param_t *box)
46 {
47   manfbox_param_t *manf;   /* manifest parameters */
48   boxheader_param_t *bh;   /* current box pointer */
49   boxheader_param_t *last; /* last boxheader pointer of the list */
50   OPJ_OFF_T pos;                 /* current position in manf_box contents; */
51   
52   manf = ( manfbox_param_t *)malloc( sizeof( manfbox_param_t));
53
54   pos = 0;
55   manf->first = last = NULL;
56
57   while( (OPJ_SIZE_T)pos < get_DBoxlen( box)){
58
59     bh = gene_childboxheader( box, pos);
60     pos += bh->headlen;
61     
62     /* insert into the list */
63     if( manf->first)
64       last->next = bh;
65     else
66       manf->first = bh;
67     last = bh;
68   }
69   return manf;
70 }
71
72 void delete_manfbox( manfbox_param_t **manf)
73 {
74   boxheader_param_t *bhPtr, *bhNext;
75   
76   bhPtr = (*manf)->first;
77   while( bhPtr != NULL){
78     bhNext = bhPtr->next;
79 #ifndef SERVER
80     /*      fprintf( logstream, "local log: boxheader %.4s deleted!\n", bhPtr->type); */
81 #endif
82       free(bhPtr);
83       bhPtr = bhNext;
84   }
85   free( *manf);
86 }
87
88 void print_manfbox( manfbox_param_t *manf)
89 {
90   boxheader_param_t *ptr;
91
92   ptr = manf->first;
93   while( ptr != NULL){
94     print_boxheader( ptr);
95     ptr=ptr->next;
96   }
97 }
98
99 boxheader_param_t * search_boxheader( const char type[], manfbox_param_t *manf)
100 {
101   boxheader_param_t *found;
102   
103   found = manf->first;
104   
105   while( found != NULL){
106     
107     if( strncmp( type, found->type, 4) == 0)
108       return found;
109       
110     found = found->next;
111   }
112   fprintf( FCGI_stderr, "Error: Boxheader %s not found\n", type);
113
114   return NULL;
115 }