// ================================================================= //
//                                                                   //
//   File      : SEC_bonddef.cxx                                     //
//   Purpose   :                                                     //
//                                                                   //
//   Coded by Ralf Westram (coder@reallysoft.de) in September 2007   //
//   Institute of Microbiology (Technical University Munich)         //
//   http://www.arb-home.de/                                         //
//                                                                   //
// ================================================================= //

#include "SEC_bonddef.hxx"
#include "SEC_defs.hxx"

#include <arbdbt.h>

#include <cctype>

GB_ERROR SEC_bond_def::fill_translation_table(const char *from, const char *to) {
    int i;

    for (i = 0; i<256; ++i) {
        edit4_to_secedit[i] = i;
    }

    GB_ERROR error = NULp;
    for (i = 0; from[i] && !error; ++i) {
        if (!to[i]) {
            error = "translate-into is too short (requires one char for each listed EDIT4 helix symbol)";
        }
        else {
            char& e2s = edit4_to_secedit[safeCharIndex(from[i])];

            bool alreadyChanged = e2s != from[i];
            if (alreadyChanged) {
                error = GBS_global_string("Duplicate translation for helix symbol '%c' (into '%c' and '%c')",
                                          from[i], e2s, to[i]);
            }
            else if (from[i] == to[i]) {
                error = GBS_global_string("Useless translation from '%c' to '%c'", from[i], to[i]);
            }
            else {
                e2s = to[i];
            }
        }
    }

    if (!from[i] && to[i] && !error) {
        error = "translate-into is too long (requires exactly one char for each listed EDIT4 helix symbol)";
    }

    return error;
}



