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