1 | /* $Id: iter.c 13 2011-04-20 15:41:43Z mmamonski $ */ |
---|
2 | /* |
---|
3 | * PSNC DRMAA 2.0 utilities library |
---|
4 | * Copyright (C) 2012 Poznan Supercomputing and Networking Center |
---|
5 | * |
---|
6 | * This program is free software: you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation, either version 3 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | |
---|
20 | |
---|
21 | #include <drmaa_utils/iter.h> |
---|
22 | |
---|
23 | #ifndef lint |
---|
24 | static char rcsid[] |
---|
25 | # ifdef __GNUC__ |
---|
26 | __attribute__ ((unused)) |
---|
27 | # endif |
---|
28 | = "$Id: iter.c 13 2011-04-20 15:41:43Z mmamonski $"; |
---|
29 | #endif |
---|
30 | |
---|
31 | |
---|
32 | static const char * |
---|
33 | fsd_iter_next( fsd_iter_t *self ) |
---|
34 | { |
---|
35 | if( self->_position < self->_length ) |
---|
36 | return self->_list[ self->_position++ ]; |
---|
37 | else |
---|
38 | fsd_exc_raise_code( FSD_ERRNO_STOP_ITERATION ); |
---|
39 | } |
---|
40 | |
---|
41 | static void |
---|
42 | fsd_iter_reset( fsd_iter_t *self ) |
---|
43 | { |
---|
44 | self->_length = 0; |
---|
45 | } |
---|
46 | |
---|
47 | static int |
---|
48 | fsd_iter_len( fsd_iter_t *self ) |
---|
49 | { |
---|
50 | return self->_length; |
---|
51 | } |
---|
52 | |
---|
53 | static void |
---|
54 | fsd_iter_append( fsd_iter_t *self, char *string ) |
---|
55 | { |
---|
56 | TRY |
---|
57 | { |
---|
58 | if( !self->_own_list ) |
---|
59 | fsd_exc_raise_msg( |
---|
60 | FSD_ERRNO_INTERNAL_ERROR, |
---|
61 | "iter::append called on constant list" ); |
---|
62 | fsd_realloc( self->_list, self->_length+1, char* ); |
---|
63 | self->_list[ self->_length++ ] = string; |
---|
64 | } |
---|
65 | EXCEPT_DEFAULT |
---|
66 | { |
---|
67 | fsd_free( string ); |
---|
68 | fsd_exc_reraise(); |
---|
69 | } |
---|
70 | END_TRY |
---|
71 | } |
---|
72 | |
---|
73 | static void |
---|
74 | fsd_iter_destroy( fsd_iter_t *self ) |
---|
75 | { |
---|
76 | int i; |
---|
77 | if( self->_own_list && self->_list ) |
---|
78 | { |
---|
79 | for( i = 0; i < self->_length; i++ ) |
---|
80 | fsd_free( self->_list[i] ); |
---|
81 | fsd_free( self->_list ); |
---|
82 | } |
---|
83 | fsd_free( self ); |
---|
84 | } |
---|
85 | |
---|
86 | static fsd_iter_t * |
---|
87 | fsd_iter_new_impl( char **list, int length, bool own ) |
---|
88 | { |
---|
89 | fsd_iter_t *volatile self = NULL; |
---|
90 | TRY |
---|
91 | { |
---|
92 | fsd_malloc( self, fsd_iter_t ); |
---|
93 | self->next = fsd_iter_next; |
---|
94 | self->reset = fsd_iter_reset; |
---|
95 | self->len = fsd_iter_len; |
---|
96 | self->append = fsd_iter_append; |
---|
97 | self->destroy = fsd_iter_destroy; |
---|
98 | self->_list = list; |
---|
99 | self->_position = 0; |
---|
100 | if( list == NULL ) |
---|
101 | self->_length = 0; |
---|
102 | else if( length >= 0 ) |
---|
103 | self->_length = length; |
---|
104 | else |
---|
105 | { |
---|
106 | char **i; |
---|
107 | int cnt = 0; |
---|
108 | for( i = self->_list; *i != NULL; i++ ) |
---|
109 | cnt++; |
---|
110 | self->_length = cnt; |
---|
111 | } |
---|
112 | self->_own_list = own; |
---|
113 | } |
---|
114 | EXCEPT_DEFAULT |
---|
115 | { |
---|
116 | if( own && list ) |
---|
117 | { |
---|
118 | if( length >= 0 ) |
---|
119 | { |
---|
120 | int i; |
---|
121 | for( i = 0; i < length; i++ ) |
---|
122 | fsd_free( list[i] ); |
---|
123 | fsd_free( list ); |
---|
124 | } |
---|
125 | else |
---|
126 | fsd_free_vector( list ); |
---|
127 | } |
---|
128 | fsd_exc_reraise(); |
---|
129 | } |
---|
130 | END_TRY |
---|
131 | return self; |
---|
132 | } |
---|
133 | |
---|
134 | fsd_iter_t * |
---|
135 | fsd_iter_new( char **list, int length ) |
---|
136 | { |
---|
137 | return fsd_iter_new_impl( list, length, true ); |
---|
138 | } |
---|
139 | |
---|
140 | fsd_iter_t * |
---|
141 | fsd_iter_new_const( const char *const *list, int length ) |
---|
142 | { |
---|
143 | return fsd_iter_new_impl( (char**)list, length, false ); |
---|
144 | } |
---|
145 | |
---|