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) + 1;
34
35 if(lastSlash>filename) {
36 uprv_strncpy(dirname, filename, (lastSlash - filename));
37 *(dirname + (lastSlash - filename)) = '\0';
38 } else {
39 *dirname = '\0';
40 }
41 }
42
43 /* go from "/usr/local/include/curses.h" to "curses" */
44 void
get_basename(char * basename,const char * filename)45 get_basename(char *basename,
46 const char *filename)
47 {
48 /* strip off any leading directory portions */
49 const char *lastSlash = uprv_strrchr(filename, U_FILE_SEP_CHAR) + 1;
50 char *lastDot;
51
52 if(lastSlash>filename) {
53 uprv_strcpy(basename, lastSlash);
54 } else {
55 uprv_strcpy(basename, filename);
56 }
57
58 /* strip off any suffix */
59 lastDot = uprv_strrchr(basename, '.');
60
61 if(lastDot != NULL) {
62 *lastDot = '\0';
63 }
64 }
65
66 #define MAX_DIGITS 10
67 int32_t
itostr(char * buffer,int32_t i,uint32_t radix,int32_t pad)68 itostr(char * buffer, int32_t i, uint32_t radix, int32_t pad)
69 {
70 const char digits[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
71 int32_t length = 0;
72 int32_t num = 0;
73 int32_t save = i;
74 int digit;
75 int32_t j;
76 char temp;
77
78 /* if i is negative make it positive */
79 if(i<0){
80 i=-i;
81 }
82
83 do{
84 digit = (int)(i % radix);
85 buffer[length++]= digits[digit];
86 i=i/radix;
87 } while(i);
88
89 while (length < pad){
90 buffer[length++] = '0';/*zero padding */
91 }
92
93 /* if i is negative add the negative sign */
94 if(save < 0){
95 buffer[length++]='-';
96 }
97
98 /* null terminate the buffer */
99 if(length<MAX_DIGITS){
100 buffer[length] = 0x0000;
101 }
102
103 num= (pad>=length) ? pad :length;
104
105
106 /* Reverses the string */
107 for (j = 0; j < (num / 2); j++){
108 temp = buffer[(length-1) - j];
109 buffer[(length-1) - j] = buffer[j];
110 buffer[j] = temp;
111 }
112 return length;
113 }
114