LCOV - code coverage report
Current view: top level - tools - h36.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 89 127 70.1 %
Date: 2024-10-11 08:09:47 Functions: 9 12 75.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2018-2023 The plumed team
       3             :    (see the PEOPLE file at the root of the distribution for a list of names)
       4             : 
       5             :    See http://www.plumed.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       8             : 
       9             :    plumed is free software: you can redistribute it and/or modify
      10             :    it under the terms of the GNU Lesser General Public License as published by
      11             :    the Free Software Foundation, either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    plumed is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU Lesser General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU Lesser General Public License
      20             :    along with plumed.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #include "h36.h"
      23             : #include <vector>
      24             : #include "Exception.h"
      25             : 
      26             : namespace PLMD {
      27             : 
      28             : /// Tiny namespace for hybrid36 format.
      29             : /// This namespace includes freely available tools for h36 format.
      30             : namespace h36 {
      31             : 
      32             : 
      33             : /*! C port of the hy36encode() and hy36decode() functions in the
      34             :     hybrid_36.py Python prototype/reference implementation.
      35             :     See the Python script for more information.
      36             : 
      37             :     This file has no external dependencies, NOT even standard C headers.
      38             :     Optionally, use hybrid_36_c.h, or simply copy the declarations
      39             :     into your code.
      40             : 
      41             :     This file is unrestricted Open Source (cctbx.sf.net).
      42             :     Please send corrections and enhancements to cctbx@cci.lbl.gov .
      43             : 
      44             :     See also: http://cci.lbl.gov/hybrid_36/
      45             : 
      46             :     Ralf W. Grosse-Kunstleve, Feb 2007.
      47             :  */
      48             : 
      49             : /* The following #include may be commented out.
      50             :    It is here only to enforce consistency of the declarations
      51             :    and the definitions.
      52             :  */
      53             : // #include <iotbx/pdb/hybrid_36_c.h>
      54             : 
      55             : /* All static functions below are implementation details
      56             :    (and not accessible from other translation units).
      57             :  */
      58             : 
      59             : static
      60             : const char*
      61      111591 : digits_upper() { return "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
      62             : 
      63             : static
      64             : const char*
      65        7813 : digits_lower() { return "0123456789abcdefghijklmnopqrstuvwxyz"; }
      66             : 
      67             : static
      68             : const char*
      69           0 : value_out_of_range() { return "value out of range."; }
      70             : 
      71             : static
      72           2 : const char* invalid_number_literal() { return "invalid number literal."; }
      73             : 
      74             : static
      75           0 : const char* unsupported_width() { return "unsupported width."; }
      76             : 
      77             : static
      78             : void
      79           0 : fill_with_stars(unsigned width, char* result)
      80             : {
      81           0 :   while (width) {
      82           0 :     *result++ = '*';
      83           0 :     width--;
      84             :   }
      85           0 :   *result = '\0';
      86           0 : }
      87             : 
      88             : static
      89             : void
      90      103780 : encode_pure(
      91             :   const char* digits,
      92             :   unsigned digits_size,
      93             :   unsigned width,
      94             :   int value,
      95             :   char* result)
      96             : {
      97             :   char buf[16];
      98             :   int rest;
      99             :   unsigned i, j;
     100             :   i = 0;
     101             :   j = 0;
     102      103780 :   if (value < 0) {
     103             :     j = 1;
     104           0 :     value = -value;
     105             :   }
     106             :   while (1) {
     107      493492 :     rest = value / digits_size;
     108      493492 :     buf[i++] = digits[value - rest * digits_size];
     109      493492 :     if (rest == 0) break;
     110             :     value = rest;
     111             :   }
     112      103780 :   if (j) buf[i++] = '-';
     113      127352 :   for(j=i; j<width; j++) *result++ = ' ';
     114      597272 :   while (i != 0) *result++ = buf[--i];
     115      103780 :   *result = '\0';
     116      103780 : }
     117             : 
     118             : static
     119             : const char*
     120      460690 : decode_pure(
     121             :   const int* digits_values,
     122             :   unsigned digits_size,
     123             :   const char* s,
     124             :   unsigned s_size,
     125             :   int* result)
     126             : {
     127             :   int si, dv;
     128             :   int have_minus = 0;
     129             :   int have_non_blank = 0;
     130             :   int value = 0;
     131             :   unsigned i = 0;
     132     2537114 :   for(; i<s_size; i++) {
     133     2076425 :     si = s[i];
     134     2076425 :     if (si < 0 || si > 127) {
     135           0 :       *result = 0;
     136           0 :       return invalid_number_literal();
     137             :     }
     138     2076425 :     if (si == ' ') {
     139      768606 :       if (!have_non_blank) continue;
     140           0 :       value *= digits_size;
     141             :     }
     142     1307819 :     else if (si == '-') {
     143           0 :       if (have_non_blank) {
     144           0 :         *result = 0;
     145           0 :         return invalid_number_literal();
     146             :       }
     147             :       have_non_blank = 1;
     148             :       have_minus = 1;
     149           0 :       continue;
     150             :     }
     151             :     else {
     152             :       have_non_blank = 1;
     153     1307819 :       dv = digits_values[si];
     154     1307819 :       if (dv < 0 || dv >= digits_size) {
     155           1 :         *result = 0;
     156           1 :         return invalid_number_literal();
     157             :       }
     158     1307818 :       value *= digits_size;
     159     1307818 :       value += dv;
     160             :     }
     161             :   }
     162      460689 :   if (have_minus) value = -value;
     163      460689 :   *result = value;
     164      460689 :   return 0;
     165             : }
     166             : 
     167             : /*! hybrid-36 encoder: converts integer value to string result
     168             : 
     169             :       width: must be 4 (e.g. for residue sequence numbers)
     170             :                   or 5 (e.g. for atom serial numbers)
     171             : 
     172             :       value: integer value to be converted
     173             : 
     174             :       result: pointer to char array of size width+1 or greater
     175             :               on return result is null-terminated
     176             : 
     177             :       return value: pointer to error message, if any,
     178             :                     or 0 on success
     179             : 
     180             :     Example usage (from C++):
     181             :       char result[4+1];
     182             :       const char* errmsg = hy36encode(4, 12345, result);
     183             :       if (errmsg) throw std::runtime_error(errmsg);
     184             :  */
     185             : const char*
     186      103780 : hy36encode(unsigned width, int value, char* result)
     187             : {
     188             :   int i = value;
     189      103780 :   if (width == 4U) {
     190        1836 :     if (i >= -999) {
     191        1836 :       if (i < 10000) {
     192        1835 :         encode_pure(digits_upper(), 10U, 4U, i, result);
     193        1835 :         return 0;
     194             :       }
     195           1 :       i -= 10000;
     196           1 :       if (i < 1213056 /* 26*36**3 */) {
     197           1 :         i += 466560 /* 10*36**3 */;
     198           1 :         encode_pure(digits_upper(), 36U, 0U, i, result);
     199           1 :         return 0;
     200             :       }
     201           0 :       i -= 1213056;
     202           0 :       if (i < 1213056) {
     203           0 :         i += 466560;
     204           0 :         encode_pure(digits_lower(), 36U, 0U, i, result);
     205           0 :         return 0;
     206             :       }
     207             :     }
     208             :   }
     209      101944 :   else if (width == 5U) {
     210      101944 :     if (i >= -9999) {
     211      101944 :       if (i < 100000) {
     212      101835 :         encode_pure(digits_upper(), 10U, 5U, i, result);
     213      101835 :         return 0;
     214             :       }
     215         109 :       i -= 100000;
     216         109 :       if (i < 43670016 /* 26*36**4 */) {
     217         108 :         i += 16796160 /* 10*36**4 */;
     218         108 :         encode_pure(digits_upper(), 36U, 0U, i, result);
     219         108 :         return 0;
     220             :       }
     221           1 :       i -= 43670016;
     222           1 :       if (i < 43670016) {
     223           1 :         i += 16796160;
     224           1 :         encode_pure(digits_lower(), 36U, 0U, i, result);
     225           1 :         return 0;
     226             :       }
     227             :     }
     228             :   }
     229             :   else {
     230           0 :     fill_with_stars(width, result);
     231           0 :     return unsupported_width();
     232             :   }
     233           0 :   fill_with_stars(width, result);
     234           0 :   return value_out_of_range();
     235             : }
     236             : 
     237             : /*! hybrid-36 decoder: converts string s to integer result
     238             : 
     239             :       width: must be 4 (e.g. for residue sequence numbers)
     240             :                   or 5 (e.g. for atom serial numbers)
     241             : 
     242             :       s: string to be converted
     243             :          does not have to be null-terminated
     244             : 
     245             :       s_size: size of s
     246             :               must be equal to width, or an error message is
     247             :               returned otherwise
     248             : 
     249             :       result: integer holding the conversion result
     250             : 
     251             :       return value: pointer to error message, if any,
     252             :                     or 0 on success
     253             : 
     254             :     Example usage (from C++):
     255             :       int result;
     256             :       const char* errmsg = hy36decode(width, "A1T5", 4, &result);
     257             :       if (errmsg) throw std::runtime_error(errmsg);
     258             :  */
     259             : const char*
     260      460690 : hy36decode(unsigned width, const char* s, unsigned s_size, int* result)
     261             : {
     262         217 :   static const std::vector<int> digits_values_upper_vector([]() {
     263         217 :     std::vector<int> ret(128U,-1);
     264        8029 :     for(unsigned i=0; i<36U; i++) {
     265        7812 :       int di = digits_upper()[i];
     266        7812 :       if (di < 0 || di > 127) {
     267           0 :         plumed_error()<<"internal error hy36decode: integer value out of range";
     268             :       }
     269        7812 :       ret[di] = i;
     270             :     }
     271         217 :     return ret;
     272      460690 :   }());
     273      460690 :   static const int* digits_values_upper=digits_values_upper_vector.data();
     274         217 :   static const std::vector<int> digits_values_lower_vector([]() {
     275         217 :     std::vector<int> ret(128U,-1);
     276        8029 :     for(unsigned i=0; i<36U; i++) {
     277        7812 :       int di = digits_lower()[i];
     278        7812 :       if (di < 0 || di > 127) {
     279           0 :         plumed_error()<<"internal error hy36decode: integer value out of range";
     280             :       }
     281        7812 :       ret[di] = i;
     282             :     }
     283         217 :     return ret;
     284      460690 :   }());
     285      460690 :   static const int* digits_values_lower=digits_values_lower_vector.data();
     286             :   int di;
     287             :   const char* errmsg;
     288      460690 :   if (s_size == width) {
     289      460690 :     di = s[0];
     290      460690 :     if (di >= 0 && di <= 127) {
     291      460690 :       if (digits_values_upper[di] >= 10) {
     292           6 :         errmsg = decode_pure(digits_values_upper, 36U, s, s_size, result);
     293           6 :         if (errmsg == 0) {
     294             :           /* result - 10*36**(width-1) + 10**width */
     295           5 :           if      (width == 4U) (*result) -= 456560;
     296           2 :           else if (width == 5U) (*result) -= 16696160;
     297             :           else {
     298           0 :             *result = 0;
     299           0 :             return unsupported_width();
     300             :           }
     301           5 :           return 0;
     302             :         }
     303             :       }
     304      460684 :       else if (digits_values_lower[di] >= 10) {
     305           0 :         errmsg = decode_pure(digits_values_lower, 36U, s, s_size, result);
     306           0 :         if (errmsg == 0) {
     307             :           /* result + 16*36**(width-1) + 10**width */
     308           0 :           if      (width == 4U) (*result) += 756496;
     309           0 :           else if (width == 5U) (*result) += 26973856;
     310             :           else {
     311           0 :             *result = 0;
     312           0 :             return unsupported_width();
     313             :           }
     314           0 :           return 0;
     315             :         }
     316             :       }
     317             :       else {
     318      460684 :         errmsg = decode_pure(digits_values_upper, 10U, s, s_size, result);
     319      460684 :         if (errmsg) return errmsg;
     320      460684 :         if (!(width == 4U || width == 5U)) {
     321           0 :           *result = 0;
     322           0 :           return unsupported_width();
     323             :         }
     324             :         return 0;
     325             :       }
     326             :     }
     327             :   }
     328           1 :   *result = 0;
     329           1 :   return invalid_number_literal();
     330             : }
     331             : 
     332             : }
     333             : 
     334             : }
     335             : 

Generated by: LCOV version 1.15