parse_bytes.c revision 55682
1238104Sdes/* 2238104Sdes * Copyright (c) 1999 Kungliga Tekniska H�gskolan 3238104Sdes * (Royal Institute of Technology, Stockholm, Sweden). 4238104Sdes * All rights reserved. 5238104Sdes * 6238104Sdes * Redistribution and use in source and binary forms, with or without 7238104Sdes * modification, are permitted provided that the following conditions 8238104Sdes * are met: 9238104Sdes * 10238104Sdes * 1. Redistributions of source code must retain the above copyright 11238104Sdes * notice, this list of conditions and the following disclaimer. 12238104Sdes * 13238104Sdes * 2. Redistributions in binary form must reproduce the above copyright 14238104Sdes * notice, this list of conditions and the following disclaimer in the 15238104Sdes * documentation and/or other materials provided with the distribution. 16238104Sdes * 17238104Sdes * 3. Neither the name of the Institute nor the names of its contributors 18238104Sdes * may be used to endorse or promote products derived from this software 19238104Sdes * without specific prior written permission. 20238104Sdes * 21238104Sdes * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22238104Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23238104Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24238104Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25238104Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26238104Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27238104Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28238104Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29238104Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30238104Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31238104Sdes * SUCH DAMAGE. 32238104Sdes */ 33238104Sdes 34238104Sdes#ifdef HAVE_CONFIG_H 35238104Sdes#include <config.h> 36238104SdesRCSID("$Id: parse_bytes.c,v 1.2 1999/12/02 16:58:51 joda Exp $"); 37238104Sdes#endif 38238104Sdes 39238104Sdes#include <parse_units.h> 40238104Sdes#include "parse_bytes.h" 41238104Sdes 42238104Sdesstatic units bytes_units[] = { 43238104Sdes { "gigabyte", 1024 * 1024 * 1024 }, 44238104Sdes { "gbyte", 1024 * 1024 * 1024 }, 45238104Sdes { "GB", 1024 * 1024 * 1024 }, 46238104Sdes { "megabyte", 1024 * 1024 }, 47238104Sdes { "mbyte", 1024 * 1024 }, 48238104Sdes { "MB", 1024 * 1024 }, 49238104Sdes { "kilobyte", 1024 }, 50238104Sdes { "KB", 1024 }, 51238104Sdes { "byte", 1 }, 52238104Sdes { NULL, 0 } 53238104Sdes}; 54238104Sdes 55238104Sdesstatic units bytes_short_units[] = { 56238104Sdes { "GB", 1024 * 1024 * 1024 }, 57238104Sdes { "MB", 1024 * 1024 }, 58238104Sdes { "KB", 1024 }, 59238104Sdes { NULL, 0 } 60238104Sdes}; 61238104Sdes 62238104Sdesint 63238104Sdesparse_bytes (const char *s, const char *def_unit) 64238104Sdes{ 65238104Sdes return parse_units (s, bytes_units, def_unit); 66238104Sdes} 67238104Sdes 68238104Sdessize_t 69238104Sdesunparse_bytes (int t, char *s, size_t len) 70238104Sdes{ 71238104Sdes return unparse_units (t, bytes_units, s, len); 72238104Sdes} 73238104Sdes 74238104Sdessize_t 75238104Sdesunparse_bytes_short (int t, char *s, size_t len) 76238104Sdes{ 77238104Sdes return unparse_units_approx (t, bytes_short_units, s, len); 78238104Sdes} 79238104Sdes