Changeset 530


Ignore:
Timestamp:
07/18/16 04:16:11 (8 years ago)
Author:
Maciej Komosinski
Message:

Strict parsing of genotype format prefix

Location:
cpp/frams/genetics
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/geno.cpp

    r522 r530  
    2929        owner = 0;
    3030        f0gen = 0;
     31        isvalid = -1;
     32        name = genname;
     33        txt = comment;
     34        setGeneAndFormat(genstring,genformat);
     35}
     36
     37void Geno::setGeneAndFormat(const SString& genstring, char genformat)
     38{
    3139        mapinshift = 0;
    3240        mapoutshift = 0;
    33         isvalid = -1;
    3441        SString gencopy(genstring);
    3542        if (genformat == -1)
     
    4350                        case '/':
    4451                                genformat = genstring.charAt(2);
    45                                 if ((genformat == '\0') || isspace(genformat))
    46                                         genformat = INVALID_FORMAT;
    4752                                if ((end = genstring.indexOf('\n')) >= 0)
    4853                                {
     54                                        mapinshift = end + 1;
     55                                        gencopy = genstring.substr(end + 1);
     56                                        if ((end>0) && (genstring[end-1]=='\r')) end--;
    4957                                        error_end = end;
    5058                                        if (end != 3) genformat = INVALID_FORMAT;
    51                                         gencopy = genstring.substr(end + 1);
    52                                         mapinshift = end + 1;
    5359                                }
    5460                                else
     
    6167                        case '*':
    6268                                genformat = genstring.charAt(2);
    63                                 if ((genformat == '\0') || isspace(genformat))
    64                                         genformat = INVALID_FORMAT;
    6569                                if ((end = genstring.indexOf("*/")) >= 0)
    6670                                {
     
    7882                                break;
    7983                        }
     84                        if (!isalnum(genformat)) genformat=INVALID_FORMAT;
    8085                        if (genformat == INVALID_FORMAT)
    8186                        {
     
    8893                                        cut = genstring.substr(0, error_end);
    8994                                int lf = cut.indexOf('\n');
    90                                 if (lf >= 0) cut = cut.substr(0, lf);
    91                                 logPrintf("Geno", "init", LOG_ERROR, "Invalid genotype format declaration: '%s'%s", cut.c_str(), genname.len() ? SString::sprintf(" in '%s'", genname.c_str()).c_str() : "");
     95                                if (lf >= 0) { if ((lf>0)&&(cut[lf-1]=='\r')) lf--; cut = cut.substr(0, lf); }
     96                                sstringQuote(cut);
     97                                logPrintf("Geno", "init", LOG_ERROR, "Invalid genotype format declaration: '%s'%s", cut.c_str(), name.len() ? SString::sprintf(" in '%s'", name.c_str()).c_str() : "");
    9298                        }
    9399
    94100                }
    95101        }
    96 
    97102        gen = gencopy;
     103        multiline = (strchr(gen.c_str(), '\n') != 0);
    98104        format = genformat;
    99         name = genname;
    100         txt = comment;
    101         multiline = (strchr(gen.c_str(), '\n') != 0);
     105        freeF0();
     106        isvalid = -1;
    102107        // mapoutshift...?
    103108}
     
    146151}
    147152
    148 void Geno::setGene(const SString& g, char newformat)
     153void Geno::setGeneOnly(const SString& g)
    149154{
    150155        gen = g;
    151156        isvalid = -1;
    152157        freeF0();
    153         if (newformat >= 0) format = newformat;
    154158}
    155159
     
    170174}
    171175
    172 SString Geno::toString(void) const
    173 {
    174         SString out;
    175         int comment = 0;
    176         if ((format != '1') || (comment = (txt.len() || name.len())))
    177         {
    178                 if (multiline)
    179                         out += "//";
    180                 else
    181                         out += "/*";
    182                 out += format;
    183                 if (comment)
    184                 {
    185                         if (txt.len()) { out += ";"; out += txt; }
    186                         if (name.len()){ out += ";"; out += name; }
    187                 }
    188                 if (multiline)
    189                         out += "\n";
    190                 else
    191                         out += "*/";
    192         }
    193         out += gen;
    194         return out;
    195 }
    196 
    197 SString Geno::shortString(void) const
     176SString Geno::getGeneAndFormat(void) const
    198177{
    199178        SString out;
  • cpp/frams/genetics/geno.h

    r522 r530  
    5555
    5656public:
    57         static const int INVALID_FORMAT = '!';
     57        static const char INVALID_FORMAT = '!';
    5858        typedef SListTempl<GenoValidator*> Validators;
    5959
     
    8585
    8686        /// return string representation, with format comment at the beginning
    87         SString toString(void) const;
    88         SString shortString(void) const;
     87        SString getGeneAndFormat(void) const;
    8988
    9089        void setString(const SString& genewithcomments);
    9190
    92         /** @param newformat=-1 -> don't change */
    93         void setGene(const SString& g, char newformat = -1);
     91        /** @param genformat=-1 -> take from genstring comment (like the constructor) else specify the valid format in genformat and pure gene in genstring */
     92        void setGeneAndFormat(const SString& genstring, char genformat=-1);
     93        /** g must be pure gene, without format. for the standard behavior use setGeneAndFormat() */
     94        void setGeneOnly(const SString& g);
    9495        SString getGene(void) const;
    9596
Note: See TracChangeset for help on using the changeset viewer.