END_TEST START_TEST(test_appendEscapedXML) { std::string escaped; appendEscapedXML("command > log.txt 2>&1", escaped); fail_unless(escaped == "command > log.txt 2>&1"); escaped.clear(); appendEscapedXML("do not alter this", escaped); fail_unless(escaped == "do not alter this"); escaped.clear(); appendEscapedXML("don't use \" for emphasis, it's < grammatically correct", escaped); fail_unless(escaped == "don't use " for emphasis, it's < grammatically correct"); }
/* * attr_to_str * * @param ds - the dynamic string we're printing the pbs_attribute into * @param aindex - the pbs_attribute's index * @param attr - the pbs_attribute * @param XML - boolean telling us whether to print XML or not */ int attr_to_str( std::string& ds, /* O */ attribute_def *at_def, /* I */ pbs_attribute attr, /* I */ bool XML) /* I */ { /* used to print numbers and chars as strings */ char local_buf[MAXLINE]; if ((attr.at_flags & ATR_VFLAG_SET) == FALSE) return(NO_ATTR_DATA); switch (at_def->at_type) { case ATR_TYPE_LONG: snprintf(local_buf, sizeof(local_buf), "%ld", attr.at_val.at_long); ds += local_buf; break; case ATR_TYPE_CHAR: sprintf(local_buf, "%c", attr.at_val.at_char); ds += local_buf; break; case ATR_TYPE_STR: if (attr.at_val.at_str == NULL) return(NO_ATTR_DATA); if (strlen(attr.at_val.at_str) == 0) return(NO_ATTR_DATA); if (XML == true) appendEscapedXML(attr.at_val.at_str,ds); else ds += attr.at_val.at_str; break; case ATR_TYPE_ARST: case ATR_TYPE_ACL: { int j; struct array_strings *arst = attr.at_val.at_arst; if (arst == NULL) return(NO_ATTR_DATA); /* concatenate all of the array strings into one string */ for (j = 0; j < arst->as_usedptr; j++) { if (j > 0) ds += ","; if (XML == true) appendEscapedXML(arst->as_string[j],ds); else ds += arst->as_string[j]; } } break; case ATR_TYPE_SIZE: size_to_str(attr.at_val.at_size, local_buf, sizeof(local_buf)); ds += local_buf; break; case ATR_TYPE_RESC: { resource *current = (resource *)GET_NEXT(attr.at_val.at_list); if (current == NULL) return(NO_ATTR_DATA); /* print all of the resources */ while (current != NULL) { /* there are only 3 resource types used */ switch (current->rs_value.at_type) { case ATR_TYPE_LONG: ds += "\t\t<"; ds += current->rs_defin->rs_name; ds += ">"; snprintf(local_buf, sizeof(local_buf), "%ld", current->rs_value.at_val.at_long); ds += local_buf; ds += "</"; ds += current->rs_defin->rs_name; ds += ">"; break; case ATR_TYPE_STR: /* Patch provided by Martin Siegert to fix seg-fault * when current->rs_value.at_val.at_str is NULL * Bugzilla bug 101 */ if (current->rs_value.at_val.at_str == NULL) break; if (strlen(current->rs_value.at_val.at_str) == 0) break; ds += "\t\t<"; ds += current->rs_defin->rs_name; ds += ">"; if (XML == true) appendEscapedXML(current->rs_value.at_val.at_str,ds); else ds += current->rs_value.at_val.at_str; ds += "</"; ds += current->rs_defin->rs_name; ds += ">"; break; case ATR_TYPE_SIZE: ds += "\t\t<"; ds += current->rs_defin->rs_name; ds += ">"; size_to_str(current->rs_value.at_val.at_size, local_buf, sizeof(local_buf)); ds += local_buf; ds += "</"; ds += current->rs_defin->rs_name; ds += ">"; break; } current = (resource *)GET_NEXT(current->rs_link); ds += "\n"; } } break; case ATR_TYPE_TV: /* Record seconds and milliseconds */ sprintf(local_buf, "%ld.%ld", attr.at_val.at_timeval.tv_sec, attr.at_val.at_timeval.tv_usec); ds += local_buf; break; /* NYI */ case ATR_TYPE_LIST: case ATR_TYPE_LL: case ATR_TYPE_SHORT: case ATR_TYPE_JINFOP: break; } /* END switch pbs_attribute type */ return(PBSE_NONE); } /* END attr_to_str */
/* * attr_to_str * * @param ds - the dynamic string we're printing the pbs_attribute into * @param aindex - the pbs_attribute's index * @param attr - the pbs_attribute * @param XML - boolean telling us whether to print XML or not */ int attr_to_str( std::string& ds, /* O */ attribute_def *at_def, /* I */ pbs_attribute attr, /* I */ bool XML) /* I */ { /* used to print numbers and chars as strings */ char local_buf[MAXLINE]; if ((attr.at_flags & ATR_VFLAG_SET) == FALSE) return(NO_ATTR_DATA); switch (at_def->at_type) { case ATR_TYPE_BOOL: snprintf(local_buf, sizeof(local_buf), attr.at_val.at_bool ? "true" : "false"); ds += local_buf; break; case ATR_TYPE_LONG: snprintf(local_buf, sizeof(local_buf), "%ld", attr.at_val.at_long); ds += local_buf; break; case ATR_TYPE_CHAR: sprintf(local_buf, "%c", attr.at_val.at_char); ds += local_buf; break; case ATR_TYPE_STR: if (attr.at_val.at_str == NULL) return(NO_ATTR_DATA); if (strlen(attr.at_val.at_str) == 0) return(NO_ATTR_DATA); if (XML == true) appendEscapedXML(attr.at_val.at_str,ds); else ds += attr.at_val.at_str; break; case ATR_TYPE_ARST: case ATR_TYPE_ACL: { int j; struct array_strings *arst = attr.at_val.at_arst; if (arst == NULL) return(NO_ATTR_DATA); /* concatenate all of the array strings into one string */ for (j = 0; j < arst->as_usedptr; j++) { if (j > 0) ds += ","; if (XML == true) appendEscapedXML(arst->as_string[j],ds); else ds += arst->as_string[j]; } } break; case ATR_TYPE_SIZE: size_to_str(attr.at_val.at_size, local_buf, sizeof(local_buf)); ds += local_buf; break; case ATR_TYPE_RESC: { if (attr.at_val.at_ptr == NULL) return(NO_ATTR_DATA); std::vector<resource> *resources = (std::vector<resource> *)attr.at_val.at_ptr; // print all of the resources for (size_t i = 0; i < resources->size(); i++) { resource &r = resources->at(i); /* there are only 3 resource types used */ switch (r.rs_value.at_type) { case ATR_TYPE_LONG: ds += "\t\t<"; ds += r.rs_defin->rs_name; ds += ">"; snprintf(local_buf, sizeof(local_buf), "%ld", r.rs_value.at_val.at_long); ds += local_buf; ds += "</"; ds += r.rs_defin->rs_name; ds += ">"; break; case ATR_TYPE_STR: /* Patch provided by Martin Siegert to fix seg-fault * when r.rs_value.at_val.at_str is NULL * Bugzilla bug 101 */ if (r.rs_value.at_val.at_str == NULL) break; if (strlen(r.rs_value.at_val.at_str) == 0) break; ds += "\t\t<"; ds += r.rs_defin->rs_name; ds += ">"; if (XML == true) appendEscapedXML(r.rs_value.at_val.at_str,ds); else ds += r.rs_value.at_val.at_str; ds += "</"; ds += r.rs_defin->rs_name; ds += ">"; break; case ATR_TYPE_SIZE: ds += "\t\t<"; ds += r.rs_defin->rs_name; ds += ">"; size_to_str(r.rs_value.at_val.at_size, local_buf, sizeof(local_buf)); ds += local_buf; ds += "</"; ds += r.rs_defin->rs_name; ds += ">"; break; } ds += "\n"; } // END for each resource } break; case ATR_TYPE_ATTR_REQ_INFO: { std::vector<std::string> names, values; attr_req_info *cr = (attr_req_info *)attr.at_val.at_ptr; if (cr == NULL) break; if (!strcmp(ATTR_req_infomin, at_def->at_name)) cr->get_min_values(names, values); else if (!strcmp(ATTR_req_infomax, at_def->at_name)) cr->get_max_values(names, values); else if (!strcmp(ATTR_req_infodefault, at_def->at_name)) cr->get_default_values(names, values); else { /* something's not right */ return(PBSE_BAD_PARAMETER); } for (unsigned int it = 0; it < names.size(); it++) { ds += "\n\t\t<"; ds += names[it].c_str(); ds += ">"; ds += values[it].c_str(); ds += "</"; ds += names[it].c_str(); ds += ">"; } ds += "\n"; } break; case ATR_TYPE_TV: /* Record seconds and milliseconds */ sprintf(local_buf, "%ld.%ld", attr.at_val.at_timeval.tv_sec, attr.at_val.at_timeval.tv_usec); ds += local_buf; break; /* NYI */ case ATR_TYPE_LIST: case ATR_TYPE_LL: case ATR_TYPE_SHORT: case ATR_TYPE_JINFOP: break; } /* END switch pbs_attribute type */ return(PBSE_NONE); } /* END attr_to_str */