michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 1998-2008, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * michael@0: * File util.c michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 06/10/99 stephen Creation. michael@0: * 02/07/08 Spieth Correct XLIFF generation on EBCDIC platform michael@0: * michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #include "unicode/putil.h" michael@0: #include "rbutil.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: michael@0: michael@0: /* go from "/usr/local/include/curses.h" to "/usr/local/include" */ michael@0: void michael@0: get_dirname(char *dirname, michael@0: const char *filename) michael@0: { michael@0: const char *lastSlash = uprv_strrchr(filename, U_FILE_SEP_CHAR) + 1; michael@0: michael@0: if(lastSlash>filename) { michael@0: uprv_strncpy(dirname, filename, (lastSlash - filename)); michael@0: *(dirname + (lastSlash - filename)) = '\0'; michael@0: } else { michael@0: *dirname = '\0'; michael@0: } michael@0: } michael@0: michael@0: /* go from "/usr/local/include/curses.h" to "curses" */ michael@0: void michael@0: get_basename(char *basename, michael@0: const char *filename) michael@0: { michael@0: /* strip off any leading directory portions */ michael@0: const char *lastSlash = uprv_strrchr(filename, U_FILE_SEP_CHAR) + 1; michael@0: char *lastDot; michael@0: michael@0: if(lastSlash>filename) { michael@0: uprv_strcpy(basename, lastSlash); michael@0: } else { michael@0: uprv_strcpy(basename, filename); michael@0: } michael@0: michael@0: /* strip off any suffix */ michael@0: lastDot = uprv_strrchr(basename, '.'); michael@0: michael@0: if(lastDot != NULL) { michael@0: *lastDot = '\0'; michael@0: } michael@0: } michael@0: michael@0: #define MAX_DIGITS 10 michael@0: int32_t michael@0: itostr(char * buffer, int32_t i, uint32_t radix, int32_t pad) michael@0: { michael@0: const char digits[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; michael@0: int32_t length = 0; michael@0: int32_t num = 0; michael@0: int32_t save = i; michael@0: int digit; michael@0: int32_t j; michael@0: char temp; michael@0: michael@0: /* if i is negative make it positive */ michael@0: if(i<0){ michael@0: i=-i; michael@0: } michael@0: michael@0: do{ michael@0: digit = (int)(i % radix); michael@0: buffer[length++]= digits[digit]; michael@0: i=i/radix; michael@0: } while(i); michael@0: michael@0: while (length < pad){ michael@0: buffer[length++] = '0';/*zero padding */ michael@0: } michael@0: michael@0: /* if i is negative add the negative sign */ michael@0: if(save < 0){ michael@0: buffer[length++]='-'; michael@0: } michael@0: michael@0: /* null terminate the buffer */ michael@0: if(length=length) ? pad :length; michael@0: michael@0: michael@0: /* Reverses the string */ michael@0: for (j = 0; j < (num / 2); j++){ michael@0: temp = buffer[(length-1) - j]; michael@0: buffer[(length-1) - j] = buffer[j]; michael@0: buffer[j] = temp; michael@0: } michael@0: return length; michael@0: }