Home | History | Annotate | Download | only in genrb
      1 /*
      2 *******************************************************************************
      3 *
      4 *   Copyright (C) 1998-2008, International Business Machines
      5 *   Corporation and others.  All Rights Reserved.
      6 *
      7 *******************************************************************************
      8 *
      9 * File util.c
     10 *
     11 * Modification History:
     12 *
     13 *   Date        Name        Description
     14 *   06/10/99    stephen     Creation.
     15 *   02/07/08    Spieth      Correct XLIFF generation on EBCDIC platform
     16 *
     17 *******************************************************************************
     18 */
     19 
     20 #include "unicode/putil.h"
     21 #include "rbutil.h"
     22 #include "cmemory.h"
     23 #include "cstring.h"
     24 
     25 
     26 /* go from "/usr/local/include/curses.h" to "/usr/local/include" */
     27 void
     28 get_dirname(char *dirname,
     29             const char *filename)
     30 {
     31   const char *lastSlash = uprv_strrchr(filename, U_FILE_SEP_CHAR) + 1;
     32 
     33   if(lastSlash>filename) {
     34     uprv_strncpy(dirname, filename, (lastSlash - filename));
     35     *(dirname + (lastSlash - filename)) = '\0';
     36   } else {
     37     *dirname = '\0';
     38   }
     39 }
     40 
     41 /* go from "/usr/local/include/curses.h" to "curses" */
     42 void
     43 get_basename(char *basename,
     44              const char *filename)
     45 {
     46   /* strip off any leading directory portions */
     47   const char *lastSlash = uprv_strrchr(filename, U_FILE_SEP_CHAR) + 1;
     48   char *lastDot;
     49 
     50   if(lastSlash>filename) {
     51     uprv_strcpy(basename, lastSlash);
     52   } else {
     53     uprv_strcpy(basename, filename);
     54   }
     55 
     56   /* strip off any suffix */
     57   lastDot = uprv_strrchr(basename, '.');
     58 
     59   if(lastDot != NULL) {
     60     *lastDot = '\0';
     61   }
     62 }
     63 
     64 #define MAX_DIGITS 10
     65 int32_t
     66 itostr(char * buffer, int32_t i, uint32_t radix, int32_t pad)
     67 {
     68     const char digits[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
     69     int32_t length = 0;
     70     int32_t num = 0;
     71     int32_t save = i;
     72     int digit;
     73     int32_t j;
     74     char temp;
     75 
     76     /* if i is negative make it positive */
     77     if(i<0){
     78         i=-i;
     79     }
     80 
     81     do{
     82         digit = (int)(i % radix);
     83         buffer[length++]= digits[digit];
     84         i=i/radix;
     85     } while(i);
     86 
     87     while (length < pad){
     88         buffer[length++] = '0';/*zero padding */
     89     }
     90 
     91     /* if i is negative add the negative sign */
     92     if(save < 0){
     93         buffer[length++]='-';
     94     }
     95 
     96     /* null terminate the buffer */
     97     if(length<MAX_DIGITS){
     98         buffer[length] =  0x0000;
     99     }
    100 
    101     num= (pad>=length) ? pad :length;
    102 
    103 
    104     /* Reverses the string */
    105     for (j = 0; j < (num / 2); j++){
    106         temp = buffer[(length-1) - j];
    107         buffer[(length-1) - j] = buffer[j];
    108         buffer[j] = temp;
    109     }
    110     return length;
    111 }
    112