Utils.cpp 5.36 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
Claudio Valerio's avatar
Claudio Valerio committed
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
25
 */
26

27

Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

30
#include <QtGlobal>
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
31
#include <QString>
32 33 34 35 36 37 38 39 40
#include "Config.h"
#include "Utils.h"
#include "Exception.h"
#include <iostream>
#include <cmath>
#include <sstream>
#include <fstream>
#include <string.h>

41 42
#include "core/memcheck.h"

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
using namespace merge_lib;

int Utils::stringToInt(const std::string & str) //throw ConvertException
{
   //skip zeros
   unsigned int lastZero = 0;str.find_last_of("0");
   while(str[lastZero++] == '0')
   {
      if(lastZero == str.size())
      {
         return 0;
      }
   }
   //if lastZero = 1, then 0 is not first symbol
   lastZero--;
   if((str.size() > 1) && (lastZero != 0))
   {
      //all number is zero, for ex. 00000000
      std::string copy = str;
      const std::string & cutedStr = copy.erase(0, lastZero);
      return _stringToInt(cutedStr) ;
   }
   else
   {
      return _stringToInt(str);
   }
}

double Utils::stringToDouble(const std::string & s )
{
   std::istringstream i(s);
   double x;
   if (!(i >> x))
      //TODO or throw exception? Think about!
      return 0;
   return x;
} 

std::string Utils::uIntToStr(unsigned int integer)
{
83
    return std::string(QString::number(integer).toLatin1());
84 85 86 87
}

std::string Utils::doubleToStr(double doubleValue)
{
88
    return std::string(QString::number(doubleValue).toLatin1());
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
}

int Utils::_stringToInt(const std::string & str) //throw ConvertException
{
   int intValue = atoi(str.c_str());

   if(((intValue == 0) && (str.size() > 1)) || // for ex string = xxx and integer = 0
      ((intValue == 0) && (str[0] != '0')))
   {
      throw Exception("Internal error");
   }


   if((intValue != 0) && (static_cast<unsigned int>(std::log10(static_cast<double>(intValue))) + 1) != str.size())         //for ex. string = 5x  and integer = 5)
   {
      throw Exception("Internal error");
   }
   return intValue;
}
bool Utils::doubleEquals(const double left, const double right, const double epsilon)
{
   return ( fabs (left - right) < epsilon);
}

double Utils::normalizeValue(double &val, const double epsilon )
{
115
    Q_UNUSED(epsilon);
116 117 118 119 120 121 122 123 124 125 126 127 128
   if( Utils::doubleEquals(val,0))
   {
      val = 0;
   }
   return val;
}

unsigned long Utils::getFileSize(const char * fileName)
{
   std::ifstream pdfFile;
   pdfFile.open (fileName, std::ios::binary );
   if (pdfFile.fail())
   {
Claudio Valerio's avatar
Claudio Valerio committed
129
      std::stringstream errorMessage("File ");
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
      errorMessage << fileName << " is absent" << "\0";
      throw Exception(errorMessage);
   }
   // get length of file:
   pdfFile.seekg (0, std::ios::end);
   unsigned long length = pdfFile.tellg();
   pdfFile.close();
   return length;
}

#ifdef DEBUG_VERBOSE

#define TO_HEX_CHAR(char_c) (char_c)>9?'A'+(char_c)-10:'0'+(char_c);

static void hex_dump_one_line(int         numberChars_n,
                              const char *input_p,
                              char       *output_p)
{
   int  i;
   char* to_hex_p;
   char* to_char_p;
   char char_c;

   /* Set pointer to the begining of hexadecimal area */
   to_hex_p=output_p;

   /* Set pointer to the begining of textual area */
   to_char_p=output_p+56;

   /* Write spaces between hexadecimal and textual areas */
   memset(output_p+50,' ',6);

   /* some kind of delimeter */
   *(output_p+53) = '#';

   /* Print out the hex area */
   for (i = 0 ; i < 16 ; i++)
   {
      /* Two spaces beetwen "four columns" */
      if (!(i&3))
      {
         *to_hex_p++=' ';
      }

      /* One space between columns */
      *to_hex_p++=' ';

      if (i < numberChars_n)
      {
         /* Print out byte in hexadecimal form */
         *to_hex_p++=TO_HEX_CHAR((input_p[i]>>4)&0xF);
         *to_hex_p++=TO_HEX_CHAR(input_p[i]&0xF);

         /* Output the char */
         char_c = input_p[i]&0xFF;

         if ( char_c<0x20 || char_c>0x7E )
         {
            char_c = '.';
         }
         *to_char_p=char_c;
         to_char_p++;
      }
      else
      {
         *to_hex_p++=' ';
         *to_hex_p++=' ';
         *to_char_p++=' ';
      }
   } /* for */
}

void trace_buffer(const void *buf, int len)
{
   char dump[160];
   int line_n = len/16;
   int rest_n = len- line_n*16;
   int i;
   memset(dump,0,160);
   printf("  length:%d\n",len);
   for (i = 0;i<line_n;i++)
   {
      hex_dump_one_line(16,
         (char*)buf+(i*16),
         dump);
      printf("%s\n",dump);;
   }
   if ( rest_n)
   {
      hex_dump_one_line(rest_n,
         (char*)buf+(line_n*16),
         dump);
      printf("%s\n",dump);
   }
}
#endif