File: sge_str.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 56,880 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,429; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,701; csh: 3,928; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (227 lines) | stat: -rw-r--r-- 6,931 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 *
 *  Sun Microsystems Inc., March, 2001
 *
 *
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *   Copyright: 2001 by Sun Microsystems, Inc.
 *
 *   All Rights Reserved.
 *
 ************************************************************************/
/*___INFO__MARK_END__*/                                   


#include <string.h>
#include <errno.h>

#include "uti/sge_rmon.h"
#include "uti/sge_string.h"
#include "uti/sge_uidgid.h"
#include "uti/sge_log.h"

#include "cull/cull_list.h"

#include "sgeobj/sge_answer.h"
#include "sgeobj/sge_str.h"
#include "sgeobj/msg_sgeobjlib.h"

#include "msg_common.h"

#define STR_LAYER BASIS_LAYER

/****** sgeobj/str/str_list_append_to_dstring() *******************************
*  NAME
*     str_list_append_to_dstring() -- append strings to dstring 
*
*  SYNOPSIS
*     const char * 
*     str_list_append_to_dstring(const lList *this_list, 
*                                dstring *string, 
*                                const char delimiter) 
*
*  FUNCTION
*     Append the strings contained in "this_list" to the dstring 
*     "string". Separate them by the character contained in 
*     "delimiter". 
*     If "this_list" is NULL or conaines no elements, "NONE" will
*     be added to the dstring.
*
*  INPUTS
*     const lList *this_list - ST_Type list 
*     dstring *string        - dynamic string 
*     const char delimiter   - delimiter  
*
*  RESULT
*     const char * - pointer to the given "string"-buffer 
*
*  SEE ALSO
*     sgeobj/str/str_list_parse_from_string()
*******************************************************************************/
const char *
str_list_append_to_dstring(const lList *this_list, dstring *string,
                           const char delimiter)
{
   const char *ret = NULL;

   DENTER(STR_LAYER, "str_list_append_to_dstring");
   if (string != NULL) {
      lListElem *elem = NULL;
      bool printed = false;

      for_each(elem, this_list) {
         sge_dstring_append(string, lGetString(elem, ST_name));
         if (lNext(elem) != NULL) {
            sge_dstring_sprintf_append(string, "%c", delimiter);
         }
         printed = true;
      }
      if (!printed) {
         sge_dstring_append(string, "NONE");
      }
      ret = sge_dstring_get_string(string);
   }
   DRETURN(ret);
}

/****** sgeobj/str/str_list_parse_from_string() *******************************
*  NAME
*     str_list_parse_from_string() -- Parse a list of strings 
*
*  SYNOPSIS
*     bool 
*     str_list_parse_from_string(lList **this_list, 
*                                const char *string, 
*                                const char *delimitor) 
*
*  FUNCTION
*     Parse a list of strings from "string". The strings have to be 
*     separated by a token contained in "delimitor". for each string
*     an element of type ST_Type will be added to "this_list". 
*     
*  INPUTS
*     lList **this_list     - ST_Type list
*     const char *string    - string to be parsed 
*     const char *delimitor - delimitor string 
*
*  RESULT
*     bool - error state
*        true  - success
*        false - error 
*
*  SEE ALSO
*     sgeobj/str/str_list_append_to_dstring()
* 
*  NOTES
*     MT-NOTE: str_list_parse_from_string() is MT safe
*******************************************************************************/
bool 
str_list_parse_from_string(lList **this_list,
                           const char *string, const char *delimitor)
{
   bool ret = true;

   DENTER(STR_LAYER, "str_list_parse_from_dstring");
   if (this_list != NULL && string != NULL && delimitor != NULL) {
      struct saved_vars_s *context = NULL;
      const char *token;

      token = sge_strtok_r(string, delimitor, &context);
      while (token != NULL) {
         lAddElemStr(this_list, ST_name, token, ST_Type);
         token = sge_strtok_r(NULL, delimitor, &context);
      }
      sge_free_saved_vars(context);
   }
   DRETURN(ret);
}

/****** sgeobj/str/str_list_is_valid() ****************************************
*  NAME
*     str_list_is_valid() -- Are all strings valid 
*
*  SYNOPSIS
*     bool 
*     str_list_is_valid(const lList *this_list, lList **answer_list) 
*
*  FUNCTION
*     Does each element in "this_list" contain a valid string (!= NULL). 
*
*  INPUTS
*     const lList *this_list - ST_Type list 
*     lList **answer_list    - AN_Type list 
*
*  RESULT
*     bool - result
*        true  - all strings are != NULL
*        false - at least one entry is NULL 
*******************************************************************************/
bool
str_list_is_valid(const lList *this_list, lList **answer_list)
{
   bool ret = true;

   DENTER(STR_LAYER, "str_list_is_valid");
   if (this_list != NULL) {
      lListElem *elem;

      for_each(elem, this_list) {
         if (lGetString(elem, ST_name) == NULL) {
            answer_list_add(answer_list, MSG_STR_INVALIDSTR, 
                            STATUS_ENOKEY, ANSWER_QUALITY_ERROR);
            break;
         }
      }
   }
   DRETURN(ret);
}

bool
str_list_transform_user_list(lList **this_list, lList **answer_list, const char *username)
{
   bool ret = true;

   DENTER(STR_LAYER, "str_list_transform_user_list");
   if (this_list != NULL && *this_list != NULL) {
      lListElem *elem;

      for_each(elem, *this_list) {
         const char *string = lGetString(elem, ST_name);

         if (string != NULL) {
            /*
             * '$user' will be replaced by the current unix username
             * '*'     empty the remove the list 
             */
            if (strcasecmp(string, "$user") == 0) {
               lSetString(elem, ST_name, username);
            } else if (strcmp(string, "*") == 0) {
               lFreeList(this_list);
               break;
            }
         }
      }
   } else {
      lAddElemStr(this_list, ST_name, username, ST_Type);
   }
   DRETURN(ret);
}