Changeset 559
- Timestamp:
- Apr 30, 2004, 2:49:08 PM (22 years ago)
- Location:
- trunk/psLib/src
- Files:
-
- 2 edited
-
sys/psTrace.c (modified) (14 diffs)
-
sysUtils/psTrace.c (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psTrace.c
r556 r559 67 67 componentAlloc(): allocate memory for a new node, and initialize members. 68 68 *****************************************************************************/ 69 static Component *componentAlloc(const char *name, 70 int level) 71 { 69 Component *componentAlloc(const char *name, 70 int level) 71 { 72 //printf("Calling componentAlloc(%s, %d)\n", name, level); 72 73 Component *comp = psAlloc(sizeof(Component)); 73 74 comp->name = psStringCopy(name); … … 75 76 comp->dimen = comp->n = 0; 76 77 comp->subcomp = NULL; 77 78 78 return comp; 79 79 } … … 124 124 125 125 /***************************************************************************** 126 componentAdd(): Adds the component named "addNodeName" to the tree "comp". 127 This procedure works by matching the current level of the component 128 tree, pointed to by "comp", with the top level of the component 129 name, pointed to by "addNodeName", and then recursively searching the 130 component tree and pruning "addNodeName" until either a match is found, 131 and then the level is set, or a new component is created. 126 componentAdd(): Adds the component named "addNodeName" to the root tree. 132 127 133 128 NOTE: replace the call to strsep() with a call to strtok(), which conforms 134 129 to ANSI-C. 135 136 NOTE: Should we check in comp == NULL?137 130 *****************************************************************************/ 138 131 static void componentAdd(const char *addNodeName, 139 132 int level) 140 133 { 141 int i = 0; 142 char name[strlen(addNodeName) + 1]; // buffer for writeable copy of name. 143 char *rest = name; // rest of name 144 char *firstComponent = NULL; // first component of name 134 int i = 0; 135 char name[strlen(addNodeName) + 1]; 136 // buffer for writeable copy. 137 char *pname=name; 138 char *firstComponent = NULL; // first component of name 145 139 Component *currentNode = croot; 146 int nodeExists = 0;147 148 printf("calling componentAdd(%s,%s, %d)\n", addNodeName, level);140 int nodeExists = 0; 141 142 // printf("Calling componentAdd(%s, %d)\n", addNodeName, level); 149 143 // Is this the root node? If so, simply set level and return. 150 144 if (strcmp(".", addNodeName) == 0) { … … 153 147 } 154 148 155 if (addNodeName[0] != '.') 156 printf("ERROR: failed to add %s to the root component tree.\n", addNodeName); 157 exit(1); 158 } 159 160 strcpy(name, addNodeName); 161 // Iterate through the components of addNodeName. 162 while (0 != strcmp(name, "")) 163 { 164 rest = name; 165 firstComponent = strsep(&rest, "."); 166 // firstComponent points to the first component of the name, unless 167 // it was root, in which case it points to the next component. The 168 // point rest points to everything else. 169 170 nodeExists = 0; 171 for (i = 0; i < comp->n; i++) { 172 if (strcmp(currentNode->subcomp[i]->name, firstComponent) == 0) { 173 currentNode = currentNode->subcomp[i]; 174 nodeExists = 1; 175 } 149 if (addNodeName[0] != '.') { 150 printf("ERROR: failed to add %s to the root component tree.\n", 151 addNodeName); 152 exit(1); 153 } 154 155 strcpy(name, addNodeName); 156 pname = &name[1]; 157 // Iterate through the components of addNodeName. Strip off the first 158 // component of the name, find that in the root tree, or add it if it 159 // does not exist, then move to the next component in the name. 160 161 while (pname != NULL) { 162 firstComponent = strsep(&pname, "."); 163 nodeExists = 0; 164 for (i = 0; i < currentNode->n; i++) { 165 if (strcmp(currentNode->subcomp[i]->name, firstComponent) == 0) { 166 currentNode = currentNode->subcomp[i]; 167 nodeExists = 1; 168 if (pname == NULL) { 169 currentNode->level = level; 170 } 171 } 172 } 173 176 174 if (nodeExists == 0) { 177 // GUS: add this component to this level of currentNode. 178 } 179 name = rest; 180 } 181 currentNode->level = level; 175 currentNode->subcomp = psRealloc(currentNode->subcomp, 176 (currentNode->n + 1) * sizeof(Component *)); 177 currentNode->n = (currentNode->n)+1; 178 179 currentNode->subcomp[(currentNode->n)-1] = 180 componentAlloc(firstComponent, level); 181 } 182 } 182 183 } 183 184 … … 197 198 static void setHighestLevel(const Component *comp) 198 199 { 200 int i = 0; 201 202 if (comp == NULL) { 203 printf("ERROR: setHighestLevel(NULL)\n"); 204 exit(1); 205 } 206 199 207 // If this nodes trace level is the max so far, save it in highest_level 200 208 if (comp->level > highest_level) { … … 203 211 204 212 // Do the same for all children nodes. 205 for (i nt i= 0; i < comp->n; i++) {213 for (i = 0; i < comp->n; i++) { 206 214 setHighestLevel(comp->subcomp[i]); 207 215 } … … 228 236 } 229 237 230 printf("Calling psSetTraceLevel(%s, %d)\n", comp, level);238 //printf("Calling psSetTraceLevel(%s, %d)\n", comp, level); 231 239 // invalidate cache 232 240 strncpy(cachedName, NO_CACHE, CACHE_NAME_LEN); … … 244 252 245 253 // return 0 on success. 254 //printf("Called psSetTraceLevel(%s, %d)\n", comp, level); 246 255 return 0; 247 256 } … … 262 271 The trace level of the "name" component. 263 272 *****************************************************************************/ 264 static int doGetTraceLevel(const Component *comp, // the component to search 265 const char *aname) // the name to find 266 { 267 char name[strlen(aname) + 1]; // need a writeable copy: for strsep() 273 static int doGetTraceLevel(const char *aname) 274 { 275 char name[strlen(aname) + 1]; // need a writeable copy: for strsep() 276 char *pname=name; 277 char *firstComponent = NULL; // first component of name 278 Component *currentNode = croot; 279 int i = 0; 280 281 if (strcmp(".", aname) == 0) { 282 return(croot->level); 283 } 284 285 if (aname[0] != '.') { 286 return(UNKNOWN_TRACE_LEVEL); 287 } 288 268 289 strcpy(name, aname); 269 char *firstComponent; // first component of name 270 char *rest = name; // rest of name 271 int level = -1; 272 int i = 0; 273 274 firstComponent = strsep(&rest, "."); 275 276 // Look for a match for firstComponent in this level's subcomps 277 for (i = 0; i < comp->n; i++) { 278 if (strcmp(comp->subcomp[i]->name, firstComponent) == 0) { 279 if (rest == NULL) { 280 // Cool. We have matched the full name. 281 level = comp->subcomp[i]->level; 282 return (level == UNKNOWN_TRACE_LEVEL) ? comp->level : level; 283 } else { 284 // We have matched this component. Now we must recurse a 285 // level deeper and match the next component. 286 287 return doGetTraceLevel(comp->subcomp[i], rest); 290 pname = &name[1]; 291 while (pname != NULL) { 292 firstComponent = strsep(&pname, "."); 293 for (i = 0; i < currentNode->n; i++) { 294 if (strcmp(currentNode->subcomp[i]->name, firstComponent) == 0) { 295 currentNode = currentNode->subcomp[i]; 296 if (pname == NULL) { 297 return(currentNode->level); 298 } 288 299 } 289 300 } 290 301 } 291 292 // There was no exact match. We return the level of the deepest 293 // component that has matched. 294 return comp->level; 302 return(UNKNOWN_TRACE_LEVEL); 295 303 } 296 304 … … 312 320 int level = -1; 313 321 322 //printf("Calling psGetTraceLevel(%s)\n", name); 314 323 // If the root component tree does not exist, then initialize it. 315 324 if (croot == NULL) { … … 321 330 if (strcmp(name, cachedName) == 0) { 322 331 return cachedLevel; 323 } else if (strcmp(name, croot->name) == 0) {324 return(croot->level);325 332 } 326 333 327 334 // Search the component root tree, determine the trace level. 328 level = doGetTraceLevel( croot,name);335 level = doGetTraceLevel(name); 329 336 330 337 // Save this search info in our depth-one cache. … … 332 339 cachedLevel = level; 333 340 341 //printf("Called psGetTraceLevel(%s) (level was %d)\n", name, level); 334 342 return level; 335 343 } … … 353 361 int i = 0; 354 362 355 printf("CALLING (%d): doPrintTraceLevels(%s, %d)\n", depth, comp->name, depth);356 printf("SHIT: comp->n is %d\n", comp->n);357 printf(" [\n");358 363 if (comp->name[0] == '\0') { 359 364 printf("%*s%-*s %d\n", depth, "", 20 - depth, … … 368 373 } 369 374 } 370 printf(" ]\n");371 372 375 373 376 for (i = 0; i < comp->n; i++) { 374 377 doPrintTraceLevels(comp->subcomp[i], depth+1); 375 378 } 376 printf("CALLED (%d): doPrintTraceLevels()\n", depth);377 379 } 378 380 -
trunk/psLib/src/sysUtils/psTrace.c
r556 r559 67 67 componentAlloc(): allocate memory for a new node, and initialize members. 68 68 *****************************************************************************/ 69 static Component *componentAlloc(const char *name, 70 int level) 71 { 69 Component *componentAlloc(const char *name, 70 int level) 71 { 72 //printf("Calling componentAlloc(%s, %d)\n", name, level); 72 73 Component *comp = psAlloc(sizeof(Component)); 73 74 comp->name = psStringCopy(name); … … 75 76 comp->dimen = comp->n = 0; 76 77 comp->subcomp = NULL; 77 78 78 return comp; 79 79 } … … 124 124 125 125 /***************************************************************************** 126 componentAdd(): Adds the component named "addNodeName" to the tree "comp". 127 This procedure works by matching the current level of the component 128 tree, pointed to by "comp", with the top level of the component 129 name, pointed to by "addNodeName", and then recursively searching the 130 component tree and pruning "addNodeName" until either a match is found, 131 and then the level is set, or a new component is created. 126 componentAdd(): Adds the component named "addNodeName" to the root tree. 132 127 133 128 NOTE: replace the call to strsep() with a call to strtok(), which conforms 134 129 to ANSI-C. 135 136 NOTE: Should we check in comp == NULL?137 130 *****************************************************************************/ 138 131 static void componentAdd(const char *addNodeName, 139 132 int level) 140 133 { 141 int i = 0; 142 char name[strlen(addNodeName) + 1]; // buffer for writeable copy of name. 143 char *rest = name; // rest of name 144 char *firstComponent = NULL; // first component of name 134 int i = 0; 135 char name[strlen(addNodeName) + 1]; 136 // buffer for writeable copy. 137 char *pname=name; 138 char *firstComponent = NULL; // first component of name 145 139 Component *currentNode = croot; 146 int nodeExists = 0;147 148 printf("calling componentAdd(%s,%s, %d)\n", addNodeName, level);140 int nodeExists = 0; 141 142 // printf("Calling componentAdd(%s, %d)\n", addNodeName, level); 149 143 // Is this the root node? If so, simply set level and return. 150 144 if (strcmp(".", addNodeName) == 0) { … … 153 147 } 154 148 155 if (addNodeName[0] != '.') 156 printf("ERROR: failed to add %s to the root component tree.\n", addNodeName); 157 exit(1); 158 } 159 160 strcpy(name, addNodeName); 161 // Iterate through the components of addNodeName. 162 while (0 != strcmp(name, "")) 163 { 164 rest = name; 165 firstComponent = strsep(&rest, "."); 166 // firstComponent points to the first component of the name, unless 167 // it was root, in which case it points to the next component. The 168 // point rest points to everything else. 169 170 nodeExists = 0; 171 for (i = 0; i < comp->n; i++) { 172 if (strcmp(currentNode->subcomp[i]->name, firstComponent) == 0) { 173 currentNode = currentNode->subcomp[i]; 174 nodeExists = 1; 175 } 149 if (addNodeName[0] != '.') { 150 printf("ERROR: failed to add %s to the root component tree.\n", 151 addNodeName); 152 exit(1); 153 } 154 155 strcpy(name, addNodeName); 156 pname = &name[1]; 157 // Iterate through the components of addNodeName. Strip off the first 158 // component of the name, find that in the root tree, or add it if it 159 // does not exist, then move to the next component in the name. 160 161 while (pname != NULL) { 162 firstComponent = strsep(&pname, "."); 163 nodeExists = 0; 164 for (i = 0; i < currentNode->n; i++) { 165 if (strcmp(currentNode->subcomp[i]->name, firstComponent) == 0) { 166 currentNode = currentNode->subcomp[i]; 167 nodeExists = 1; 168 if (pname == NULL) { 169 currentNode->level = level; 170 } 171 } 172 } 173 176 174 if (nodeExists == 0) { 177 // GUS: add this component to this level of currentNode. 178 } 179 name = rest; 180 } 181 currentNode->level = level; 175 currentNode->subcomp = psRealloc(currentNode->subcomp, 176 (currentNode->n + 1) * sizeof(Component *)); 177 currentNode->n = (currentNode->n)+1; 178 179 currentNode->subcomp[(currentNode->n)-1] = 180 componentAlloc(firstComponent, level); 181 } 182 } 182 183 } 183 184 … … 197 198 static void setHighestLevel(const Component *comp) 198 199 { 200 int i = 0; 201 202 if (comp == NULL) { 203 printf("ERROR: setHighestLevel(NULL)\n"); 204 exit(1); 205 } 206 199 207 // If this nodes trace level is the max so far, save it in highest_level 200 208 if (comp->level > highest_level) { … … 203 211 204 212 // Do the same for all children nodes. 205 for (i nt i= 0; i < comp->n; i++) {213 for (i = 0; i < comp->n; i++) { 206 214 setHighestLevel(comp->subcomp[i]); 207 215 } … … 228 236 } 229 237 230 printf("Calling psSetTraceLevel(%s, %d)\n", comp, level);238 //printf("Calling psSetTraceLevel(%s, %d)\n", comp, level); 231 239 // invalidate cache 232 240 strncpy(cachedName, NO_CACHE, CACHE_NAME_LEN); … … 244 252 245 253 // return 0 on success. 254 //printf("Called psSetTraceLevel(%s, %d)\n", comp, level); 246 255 return 0; 247 256 } … … 262 271 The trace level of the "name" component. 263 272 *****************************************************************************/ 264 static int doGetTraceLevel(const Component *comp, // the component to search 265 const char *aname) // the name to find 266 { 267 char name[strlen(aname) + 1]; // need a writeable copy: for strsep() 273 static int doGetTraceLevel(const char *aname) 274 { 275 char name[strlen(aname) + 1]; // need a writeable copy: for strsep() 276 char *pname=name; 277 char *firstComponent = NULL; // first component of name 278 Component *currentNode = croot; 279 int i = 0; 280 281 if (strcmp(".", aname) == 0) { 282 return(croot->level); 283 } 284 285 if (aname[0] != '.') { 286 return(UNKNOWN_TRACE_LEVEL); 287 } 288 268 289 strcpy(name, aname); 269 char *firstComponent; // first component of name 270 char *rest = name; // rest of name 271 int level = -1; 272 int i = 0; 273 274 firstComponent = strsep(&rest, "."); 275 276 // Look for a match for firstComponent in this level's subcomps 277 for (i = 0; i < comp->n; i++) { 278 if (strcmp(comp->subcomp[i]->name, firstComponent) == 0) { 279 if (rest == NULL) { 280 // Cool. We have matched the full name. 281 level = comp->subcomp[i]->level; 282 return (level == UNKNOWN_TRACE_LEVEL) ? comp->level : level; 283 } else { 284 // We have matched this component. Now we must recurse a 285 // level deeper and match the next component. 286 287 return doGetTraceLevel(comp->subcomp[i], rest); 290 pname = &name[1]; 291 while (pname != NULL) { 292 firstComponent = strsep(&pname, "."); 293 for (i = 0; i < currentNode->n; i++) { 294 if (strcmp(currentNode->subcomp[i]->name, firstComponent) == 0) { 295 currentNode = currentNode->subcomp[i]; 296 if (pname == NULL) { 297 return(currentNode->level); 298 } 288 299 } 289 300 } 290 301 } 291 292 // There was no exact match. We return the level of the deepest 293 // component that has matched. 294 return comp->level; 302 return(UNKNOWN_TRACE_LEVEL); 295 303 } 296 304 … … 312 320 int level = -1; 313 321 322 //printf("Calling psGetTraceLevel(%s)\n", name); 314 323 // If the root component tree does not exist, then initialize it. 315 324 if (croot == NULL) { … … 321 330 if (strcmp(name, cachedName) == 0) { 322 331 return cachedLevel; 323 } else if (strcmp(name, croot->name) == 0) {324 return(croot->level);325 332 } 326 333 327 334 // Search the component root tree, determine the trace level. 328 level = doGetTraceLevel( croot,name);335 level = doGetTraceLevel(name); 329 336 330 337 // Save this search info in our depth-one cache. … … 332 339 cachedLevel = level; 333 340 341 //printf("Called psGetTraceLevel(%s) (level was %d)\n", name, level); 334 342 return level; 335 343 } … … 353 361 int i = 0; 354 362 355 printf("CALLING (%d): doPrintTraceLevels(%s, %d)\n", depth, comp->name, depth);356 printf("SHIT: comp->n is %d\n", comp->n);357 printf(" [\n");358 363 if (comp->name[0] == '\0') { 359 364 printf("%*s%-*s %d\n", depth, "", 20 - depth, … … 368 373 } 369 374 } 370 printf(" ]\n");371 372 375 373 376 for (i = 0; i < comp->n; i++) { 374 377 doPrintTraceLevels(comp->subcomp[i], depth+1); 375 378 } 376 printf("CALLED (%d): doPrintTraceLevels()\n", depth);377 379 } 378 380
Note:
See TracChangeset
for help on using the changeset viewer.
