1 /*****************************************************************************
2  * Copyright (c) 2019 FrontISTR Commons
3  * This software is released under the MIT License, see LICENSE.txt
4  *****************************************************************************/
5 /*
6   hecd_util ver.1.0
7 */
8 
9 #include "hecd_util.h"
10 
11 namespace hecd_util {
12 
cleanup_token(char * s)13 void cleanup_token(char *s) {
14   char buff[256];
15   cleanup_token(s, buff);
16   strcpy(s, buff);
17 }
18 
cleanup_token(char * src,char * dest)19 void cleanup_token(char *src, char *dest) {
20 #define is_skip_char(x) \
21   (x == ' ' || x == '=' || x == '\t' || x == '\r' || x == '\n')
22   char *s = src;
23 
24   while (*s && is_skip_char(*s)) s++;
25 
26   char *d = dest;
27 
28   while (*s && !is_skip_char(*s)) {
29     *d = *s;
30     d++;
31     s++;
32   }
33 
34   *d = 0;
35 }
36 
toupper(char * s)37 void toupper(char *s) {
38   while (*s) {
39     *s = (char)::toupper(*s);
40     s++;
41   }
42 }
43 
toupper(const char * src,char * dest)44 void toupper(const char *src, char *dest) {
45   char *s = (char *)src;
46 
47   while (*s) {
48     *dest = (char)::toupper(*s);
49     s++;
50     dest++;
51   }
52 
53   *dest = 0;
54 }
55 
tolower(char * s)56 void tolower(char *s) {
57   while (*s) {
58     *s = (char)::tolower(*s);
59     s++;
60   }
61 }
62 
tolower(const char * src,char * dest)63 void tolower(const char *src, char *dest) {
64   char *s = (char *)src;
65 
66   while (*s) {
67     *dest = (char)::tolower(*s);
68     s++;
69     dest++;
70   }
71 
72   *dest = 0;
73 }
74 
remove_cr(char * s)75 void remove_cr(char *s) {
76   while (*s) {
77     if (*s == '\r' || *s == '\n') {
78       *s = 0;
79       return;
80     }
81 
82     s++;
83   }
84 }
85 
86 // note)
87 //  I/O of HEC-MW does not support '1e+2' formated number.
88 //  Then ftos converts '1e+2' to '1.0e+2'
89 
ftos(double x,char * s)90 void ftos(double x, char *s) {
91   char buff[256];
92   sprintf(buff, "%.10lg", x);
93   char *p     = buff;
94   bool fg_dot = false;
95 
96   while (*p) {
97     if (*p == '.') {
98       fg_dot = true;
99 
100     } else if (*p == 'e' || *p == 'E') {
101       if (!fg_dot) {
102         *s = '.';
103         s++;
104         *s = '0';
105         s++;
106       }
107     }
108 
109     *s = *p;
110     p++;
111     s++;
112   }
113 
114   *s = 0;
115 }
116 
117 }  // end of namespace hecd_util
118