~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/sgeobj/sge_str.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 *
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 *
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 *
 
9
 *
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 *
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 *
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 *
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 *
 
28
 *   All Rights Reserved.
 
29
 *
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/                                   
 
32
 
 
33
 
 
34
#include <string.h>
 
35
#include <errno.h>
 
36
 
 
37
#include "sge_string.h"
 
38
#include "sge_uidgid.h"
 
39
#include "sgermon.h"
 
40
#include "sge_log.h"
 
41
#include "cull_list.h"
 
42
 
 
43
#include "sge_answer.h"
 
44
#include "sge_str.h"
 
45
 
 
46
#include "msg_common.h"
 
47
#include "msg_sgeobjlib.h"
 
48
 
 
49
#define STR_LAYER BASIS_LAYER
 
50
 
 
51
/****** sgeobj/str/str_list_append_to_dstring() *******************************
 
52
*  NAME
 
53
*     str_list_append_to_dstring() -- append strings to dstring 
 
54
*
 
55
*  SYNOPSIS
 
56
*     const char * 
 
57
*     str_list_append_to_dstring(const lList *this_list, 
 
58
*                                dstring *string, 
 
59
*                                const char delimiter) 
 
60
*
 
61
*  FUNCTION
 
62
*     Append the strings contained in "this_list" to the dstring 
 
63
*     "string". Separate them by the character contained in 
 
64
*     "delimiter". 
 
65
*     If "this_list" is NULL or conaines no elements, "NONE" will
 
66
*     be added to the dstring.
 
67
*
 
68
*  INPUTS
 
69
*     const lList *this_list - ST_Type list 
 
70
*     dstring *string        - dynamic string 
 
71
*     const char delimiter   - delimiter  
 
72
*
 
73
*  RESULT
 
74
*     const char * - pointer to the given "string"-buffer 
 
75
*
 
76
*  SEE ALSO
 
77
*     sgeobj/str/str_list_parse_from_string()
 
78
*******************************************************************************/
 
79
const char *
 
80
str_list_append_to_dstring(const lList *this_list, dstring *string,
 
81
                           const char delimiter)
 
82
{
 
83
   const char *ret = NULL;
 
84
 
 
85
   DENTER(STR_LAYER, "str_list_append_to_dstring");
 
86
   if (string != NULL) {
 
87
      lListElem *elem = NULL;
 
88
      bool printed = false;
 
89
 
 
90
      for_each(elem, this_list) {
 
91
         sge_dstring_append(string, lGetString(elem, ST_name));
 
92
         if (lNext(elem) != NULL) {
 
93
            sge_dstring_sprintf_append(string, "%c", delimiter);
 
94
         }
 
95
         printed = true;
 
96
      }
 
97
      if (!printed) {
 
98
         sge_dstring_append(string, "NONE");
 
99
      }
 
100
      ret = sge_dstring_get_string(string);
 
101
   }
 
102
   DRETURN(ret);
 
103
}
 
104
 
 
105
/****** sgeobj/str/str_list_parse_from_string() *******************************
 
106
*  NAME
 
107
*     str_list_parse_from_string() -- Parse a list of strings 
 
108
*
 
109
*  SYNOPSIS
 
110
*     bool 
 
111
*     str_list_parse_from_string(lList **this_list, 
 
112
*                                const char *string, 
 
113
*                                const char *delimitor) 
 
114
*
 
115
*  FUNCTION
 
116
*     Parse a list of strings from "string". The strings have to be 
 
117
*     separated by a token contained in "delimitor". for each string
 
118
*     an element of type ST_Type will be added to "this_list". 
 
119
*     
 
120
*  INPUTS
 
121
*     lList **this_list     - ST_Type list
 
122
*     const char *string    - string to be parsed 
 
123
*     const char *delimitor - delimitor string 
 
124
*
 
125
*  RESULT
 
126
*     bool - error state
 
127
*        true  - success
 
128
*        false - error 
 
129
*
 
130
*  SEE ALSO
 
131
*     sgeobj/str/str_list_append_to_dstring()
 
132
 
133
*  NOTES
 
134
*     MT-NOTE: str_list_parse_from_string() is MT safe
 
135
*******************************************************************************/
 
136
bool 
 
137
str_list_parse_from_string(lList **this_list,
 
138
                           const char *string, const char *delimitor)
 
139
{
 
140
   bool ret = true;
 
141
 
 
142
   DENTER(STR_LAYER, "str_list_parse_from_dstring");
 
143
   if (this_list != NULL && string != NULL && delimitor != NULL) {
 
144
      struct saved_vars_s *context = NULL;
 
145
      const char *token;
 
146
 
 
147
      token = sge_strtok_r(string, delimitor, &context);
 
148
      while (token != NULL) {
 
149
         lAddElemStr(this_list, ST_name, token, ST_Type);
 
150
         token = sge_strtok_r(NULL, delimitor, &context);
 
151
      }
 
152
      sge_free_saved_vars(context);
 
153
   }
 
154
   DRETURN(ret);
 
155
}
 
156
 
 
157
/****** sgeobj/str/str_list_is_valid() ****************************************
 
158
*  NAME
 
159
*     str_list_is_valid() -- Are all strings valid 
 
160
*
 
161
*  SYNOPSIS
 
162
*     bool 
 
163
*     str_list_is_valid(const lList *this_list, lList **answer_list) 
 
164
*
 
165
*  FUNCTION
 
166
*     Does each element in "this_list" contain a valid string (!= NULL). 
 
167
*
 
168
*  INPUTS
 
169
*     const lList *this_list - ST_Type list 
 
170
*     lList **answer_list    - AN_Type list 
 
171
*
 
172
*  RESULT
 
173
*     bool - result
 
174
*        true  - all strings are != NULL
 
175
*        false - at least one entry is NULL 
 
176
*******************************************************************************/
 
177
bool
 
178
str_list_is_valid(const lList *this_list, lList **answer_list)
 
179
{
 
180
   bool ret = true;
 
181
 
 
182
   DENTER(STR_LAYER, "str_list_is_valid");
 
183
   if (this_list != NULL) {
 
184
      lListElem *elem;
 
185
 
 
186
      for_each(elem, this_list) {
 
187
         if (lGetString(elem, ST_name) == NULL) {
 
188
            answer_list_add(answer_list, MSG_STR_INVALIDSTR, 
 
189
                            STATUS_ENOKEY, ANSWER_QUALITY_ERROR);
 
190
            break;
 
191
         }
 
192
      }
 
193
   }
 
194
   DRETURN(ret);
 
195
}
 
196
 
 
197
bool
 
198
str_list_transform_user_list(lList **this_list, lList **answer_list, const char *username)
 
199
{
 
200
   bool ret = true;
 
201
 
 
202
   DENTER(STR_LAYER, "str_list_transform_user_list");
 
203
   if (this_list != NULL && *this_list != NULL) {
 
204
      lListElem *elem;
 
205
 
 
206
      for_each(elem, *this_list) {
 
207
         const char *string = lGetString(elem, ST_name);
 
208
 
 
209
         if (string != NULL) {
 
210
            /*
 
211
             * '$user' will be replaced by the current unix username
 
212
             * '*'     empty the remove the list 
 
213
             */
 
214
            if (strcasecmp(string, "$user") == 0) {
 
215
               lSetString(elem, ST_name, username);
 
216
            } else if (strcmp(string, "*") == 0) {
 
217
               lFreeList(this_list);
 
218
               break;
 
219
            }
 
220
         }
 
221
      }
 
222
   } else {
 
223
      lAddElemStr(this_list, ST_name, username, ST_Type);
 
224
   }
 
225
   DRETURN(ret);
 
226
}