1 /* 2 * Copyright (C) 2009 Tommi Maekitalo 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License as 6 * published by the Free Software Foundation; either version 2 of the 7 * License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, but 10 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied 11 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and 12 * NON-INFRINGEMENT. See the GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 * 18 */ 19 20 #include <sstream> 21 #include <stdlib.h> 22 23 namespace zim 24 { envValue(const char * env,unsigned def)25 unsigned envValue(const char* env, unsigned def) 26 { 27 const char* v = ::getenv(env); 28 if (v) 29 { 30 std::istringstream s(v); 31 s >> def; 32 } 33 return def; 34 } 35 envMemSize(const char * env,unsigned def)36 unsigned envMemSize(const char* env, unsigned def) 37 { 38 const char* v = ::getenv(env); 39 if (v) 40 { 41 char unit = '\0'; 42 std::istringstream s(v); 43 s >> def >> unit; 44 45 switch (unit) 46 { 47 case 'k': 48 case 'K': def *= 1024; break; 49 case 'm': 50 case 'M': def *= 1024 * 1024; break; 51 case 'g': 52 case 'G': def *= 1024 * 1024 * 1024; break; 53 } 54 } 55 return def; 56 } 57 } 58 59