<commit_before>/*
 * Copyright 2008 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "condor_common.h"

#include "ClassAdReevaluator.h"

#include "condor_classad.h"
#include "string_list.h"
#include "MyString.h"
#include "condor_debug.h"

bool
classad_reevaluate(ClassAd *ad, const ClassAd *context)
{
	StringList *reevaluate_attrs;
	MyString stmp;
	char *ptmp, *atmp, *ntmp;
	ExprTree *etmp;
	int itmp;
	float ftmp;
	
	if (!ad->LookupString("REEVALUATE_ATTRIBUTES", &ptmp)) {
		dprintf(D_FULLDEBUG,
				"classad_reevaluate: REEVALUATE_ATTRIBUTES not defined, skipping\n");

		return true;
	}

	reevaluate_attrs = new StringList(ptmp);
	if (!reevaluate_attrs) {
		dprintf(D_ALWAYS,
				"classad_reevaluate: Failed to parse REEVALUATE_ATTRS: %s\n",
				ptmp);

		goto FAIL;
	}

	free(ptmp);

	reevaluate_attrs->rewind();
	while (NULL != (atmp = reevaluate_attrs->next())) {
		stmp.sprintf("REEVALUATE_%s_EXPR", atmp);

		dprintf(D_FULLDEBUG,
				"classad_reevaluate: Attempting reevaluate %s with %s\n",
				atmp, stmp.GetCStr());

		etmp = ad->Lookup(atmp);
		if (!etmp) {
			dprintf(D_ALWAYS,
					"classad_reevaluate: %s does not exist in ad, returning\n",
					atmp);

			goto FAIL;
		}

		switch (etmp->RArg()->MyType()) {
		case LX_STRING:
			if (!ad->EvalString(stmp.GetCStr(), context, &ntmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to evaluate %s as a String\n",
						ptmp);

				goto FAIL;
			}

			if (!ad->Assign(atmp, ntmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to assign new value %s to %s\n",
						ntmp, atmp);

				goto FAIL;
			}

			dprintf(D_FULLDEBUG,
					"classad_reevaluate: Updated %s to %s\n",
					atmp, ntmp);

			free(ntmp);

			break;
		case LX_INTEGER:
			if (!ad->EvalInteger(stmp.GetCStr(), context, itmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to evaluate %s as an Integer\n",
						ptmp);

				goto FAIL;
			}

			if (!ad->Assign(atmp, itmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to assign new value %d to %s\n",
						itmp, atmp);

				goto FAIL;
			}

			dprintf(D_FULLDEBUG,
					"classad_reevaluate: Updated %s to %d\n",
					atmp, itmp);

			break;
		case LX_FLOAT:
			if (!ad->EvalFloat(stmp.GetCStr(), context, ftmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to evaluate %s as a Float\n",
						ptmp);

				goto FAIL;
			}

			if (!ad->Assign(atmp, ftmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to assign new value %f to %s\n",
						ftmp, atmp);

				goto FAIL;
			}

			dprintf(D_FULLDEBUG,
					"classad_reevaluate: Updated %s to %f\n",
					atmp, ftmp);

			break;
		case LX_BOOL:
			if (!ad->EvalBool(stmp.GetCStr(), context, itmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to evaluate %s as a Bool\n",
						ptmp);

				goto FAIL;
			}

			if (!ad->Assign(atmp, (itmp ? true : false))) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to assign new value %d to %s\n",
						itmp, atmp);

				goto FAIL;
			}

			dprintf(D_FULLDEBUG,
					"classad_reevaluate: Updated %s to %d\n",
					atmp, itmp);

			break;
		default:
			dprintf(D_ALWAYS,
					"classad_reevaluate: %s has unsupported type %d\n, cannot reevaluate\n",
					atmp, etmp->MyType());
		}
	}

	delete reevaluate_attrs;

	return true;

 FAIL:<commit_msg>CID 1824: ptmp used where stmp should be<commit_after>/*
 * Copyright 2008 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "condor_common.h"

#include "ClassAdReevaluator.h"

#include "condor_classad.h"
#include "string_list.h"
#include "MyString.h"
#include "condor_debug.h"

bool
classad_reevaluate(ClassAd *ad, const ClassAd *context)
{
	StringList *reevaluate_attrs;
	MyString stmp;
	char *ptmp, *atmp, *ntmp;
	ExprTree *etmp;
	int itmp;
	float ftmp;
	
	if (!ad->LookupString("REEVALUATE_ATTRIBUTES", &ptmp)) {
		dprintf(D_FULLDEBUG,
				"classad_reevaluate: REEVALUATE_ATTRIBUTES not defined, skipping\n");

		return true;
	}

	reevaluate_attrs = new StringList(ptmp);
	if (!reevaluate_attrs) {
		dprintf(D_ALWAYS,
				"classad_reevaluate: Failed to parse REEVALUATE_ATTRS: %s\n",
				ptmp);

		goto FAIL;
	}

	free(ptmp);

	reevaluate_attrs->rewind();
	while (NULL != (atmp = reevaluate_attrs->next())) {
		stmp.sprintf("REEVALUATE_%s_EXPR", atmp);

		dprintf(D_FULLDEBUG,
				"classad_reevaluate: Attempting reevaluate %s with %s\n",
				atmp, stmp.GetCStr());

		etmp = ad->Lookup(atmp);
		if (!etmp) {
			dprintf(D_ALWAYS,
					"classad_reevaluate: %s does not exist in ad, returning\n",
					atmp);

			goto FAIL;
		}

		switch (etmp->RArg()->MyType()) {
		case LX_STRING:
			if (!ad->EvalString(stmp.GetCStr(), context, &ntmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to evaluate %s as a String\n",
						stmp.GetCStr());

				goto FAIL;
			}

			if (!ad->Assign(atmp, ntmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to assign new value %s to %s\n",
						ntmp, atmp);

				goto FAIL;
			}

			dprintf(D_FULLDEBUG,
					"classad_reevaluate: Updated %s to %s\n",
					atmp, ntmp);

			free(ntmp);

			break;
		case LX_INTEGER:
			if (!ad->EvalInteger(stmp.GetCStr(), context, itmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to evaluate %s as an Integer\n",
						stmp.GetCStr());

				goto FAIL;
			}

			if (!ad->Assign(atmp, itmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to assign new value %d to %s\n",
						itmp, atmp);

				goto FAIL;
			}

			dprintf(D_FULLDEBUG,
					"classad_reevaluate: Updated %s to %d\n",
					atmp, itmp);

			break;
		case LX_FLOAT:
			if (!ad->EvalFloat(stmp.GetCStr(), context, ftmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to evaluate %s as a Float\n",
						stmp.GetCStr());

				goto FAIL;
			}

			if (!ad->Assign(atmp, ftmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to assign new value %f to %s\n",
						ftmp, atmp);

				goto FAIL;
			}

			dprintf(D_FULLDEBUG,
					"classad_reevaluate: Updated %s to %f\n",
					atmp, ftmp);

			break;
		case LX_BOOL:
			if (!ad->EvalBool(stmp.GetCStr(), context, itmp)) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to evaluate %s as a Bool\n",
						stmp.GetCStr());

				goto FAIL;
			}

			if (!ad->Assign(atmp, (itmp ? true : false))) {
				dprintf(D_ALWAYS,
						"classad_reevaluate: Failed to assign new value %d to %s\n",
						itmp, atmp);

				goto FAIL;
			}

			dprintf(D_FULLDEBUG,
					"classad_reevaluate: Updated %s to %d\n",
					atmp, itmp);

			break;
		default:
			dprintf(D_ALWAYS,
					"classad_reevaluate: %s has unsupported type %d\n, cannot reevaluate\n",
					atmp, etmp->MyType());
		}
	}

	delete reevaluate_attrs;

	return true;

 FAIL:
{'fraction_non_alphanumeric': 0.12807945311492325, 'fraction_numerical': 0.002579646588417387, 'mean_word_length': 3.232532751091703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7238182', 'n_tokens_mistral': 3560, 'n_tokens_neox': 2880, 'n_words': 754}