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