<commit_before>/**
 * Node.cc
 *
 * Copyright(c) 2001 <NAME>, <NAME>
 * All rights reserved.
 */
#include <stdio.h>
#include "Node.h"
#include "ClassServer.h"
//#include "exceptions.h"
#include "Link.h"
#include "TLB.h"
#include "utils.h"
#include "AtomSpaceDefinitions.h"


Node::Node(Type type, const std::string& name, const TruthValue& tv) throw (InvalidParamException, AssertionException) : Atom(type, std::vector<Handle>(), tv) {

    if (!ClassServer::isAssignableFrom(NODE, type)) {
        throw InvalidParamException(TRACE_INFO, "Node - Invalid node type '%d'.", type);
    }
    
    cassert(TRACE_INFO, &name, "Node - name parameter should not be NULL.");
    
    // If name is empty let it unitialized for saving memory...
    if (!name.empty()) {
        this->name = name;
    }
}

Node::~Node() throw () {
//    fprintf(stdout, "Deleting Node:\n%s\n", this->toString().c_str());
//    fflush(stdout);
}

const std::string& Node::getName() const{
    return name;
}

void Node::setName(const std::string& name) throw (RuntimeException) {
    
    if (atomTable != NULL){
        throw RuntimeException(TRACE_INFO, 
              "Node - Cannot change the name of a node already inserted into an AtomTable.");
    }

    if (!name.empty()) {
        this->name = name;
    } else if (!(this->name.empty())) {
        this->name = name;
    }
}

void Node::merge(Atom* atom) throw (InconsistenceException) {

    if (!equals(atom)){
        throw InconsistenceException(TRACE_INFO, "Node - Different nodes cannot be merged");
    }

    Atom::merge(atom);
}

std::string Node::toShortString() {
    char buf[1024];
    sprintf(buf, "node[%d:%s%s]", type, name.c_str(), (getFlag(HYPOTETHICAL_FLAG)?":h":""));
    return buf;
}

std::string Node::toString() {
    char buf[1024];
    sprintf(buf, "node[%d:%s] (%d,%f)", type, name.c_str(), (int)getAttentionValue().getSTI(), 0.0); //activation here at 0: can be replace with LTI
    return buf;
}

bool Node::equals(Atom *other){
    bool result = false;
    if (other != NULL && (type == other->getType())){
        Node *otherNode = (Node *) other;
        result = !strcmp(name.c_str(), otherNode->getName().c_str());
    }
    return(result);
}

int Node::hashCode(){
    int result = Atom::hashCode();
    result += Util::hash<const char*>()(name.c_str());
    return result;
}

<commit_msg>cleanup several buffer overflows, plus some bizarre conceptions about string allocation.<commit_after>/**
 * Node.cc
 *
 * Copyright(c) 2001 <NAME>, <NAME>
 * All rights reserved.
 */
#include <stdio.h>
#include "Node.h"
#include "ClassServer.h"
//#include "exceptions.h"
#include "Link.h"
#include "TLB.h"
#include "utils.h"
#include "AtomSpaceDefinitions.h"


Node::Node(Type type, 
           const std::string& cname, 
           const TruthValue& tv) 
throw (InvalidParamException, AssertionException) 
    : Atom(type, std::vector<Handle>(), tv) 
{
    if (!ClassServer::isAssignableFrom(NODE, type)) {
        throw InvalidParamException(TRACE_INFO, "Node - Invalid node type '%d'.", type);
    }
    
    cassert(TRACE_INFO, &name, "Node - name parameter should not be NULL.");
    name = cname;
}

Node::~Node() throw ()
{
//    fprintf(stdout, "Deleting Node:\n%s\n", toString().c_str());
//    fflush(stdout);
}

const std::string& Node::getName() const
{
    return name;
}

void Node::setName(const std::string& cname) throw (RuntimeException)
{
    
    if (atomTable != NULL){
        throw RuntimeException(TRACE_INFO, 
              "Node - Cannot change the name of a node already inserted into an AtomTable.");
    }
    name = cname;
}

void Node::merge(Atom* atom) throw (InconsistenceException)
{
    if (!equals(atom)) {
        throw InconsistenceException(TRACE_INFO, "Node - Different nodes cannot be merged");
    }

    Atom::merge(atom);
}

std::string Node::toShortString()
{
#define BUFSZ 1024
    char buf[BUFSZ];
    snprintf(buf, BUFSZ, "node[%d:%s%s]", 
        type, name.c_str(), (getFlag(HYPOTETHICAL_FLAG)?":h":""));
    return buf;
}

std::string Node::toString()
{
    char buf[BUFSZ];
    //activation here at 0: can be replace with LTI
    snprintf(buf, BUFSZ, "node[%d:%s] (%d,%f)", 
        type, name.c_str(), (int)getAttentionValue().getSTI(), 0.0);
    return buf;
}

bool Node::equals(Atom *other)
{
    bool result = false;
    if (other != NULL && (type == other->getType())){
        Node *otherNode = (Node *) other;
        result = !strcmp(name.c_str(), otherNode->getName().c_str());
    }
    return(result);
}

int Node::hashCode()
{
    int result = Atom::hashCode();
    result += Util::hash<const char*>()(name.c_str());
    return result;
}

{'fraction_non_alphanumeric': 0.1720660802402918, 'fraction_numerical': 0.0055782021025531005, 'mean_word_length': 3.427350427350427, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6573894', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1554, 'n_words': 394}