blob: 946400400babfd91595fd43be71a352d6832da30 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001/*
2 * MGtool-1.0
3 * File: toollib.c
4 * Maintainer: Kirk@MorgenGrauen
5 */
6
7/*------------------------------------------*/
8/* the original Xtool is copyrighted by Hyp */
9/*------------------------------------------*/
10
11#include "toollib.h"
12#include <lpctypes.h>
13#include <properties.h>
14/*
15string *old_explode(string str, string del)
16{
17 int s, t;
18 string *strs;
19
20 if(del == "")
21 return ({str});
22 strs=explode(str, del);
23 s=0;
24 t=sizeof(strs)-1;
25 while(s<=t && strs[s++] == "")
26 ;
27 s--;
28 while(t>=0 && strs[t--] == "")
29 ;
30 t++;
31 if(s<=t)
32 return strs[s..t];
33 return 0;
34}
35
36string *explode(string str, string del)
37{
38 return explode(str, del);
39}
40*/
41string *strip_explode(string str, string del)
42{
43 return explode(str, del)-({""});
44}
45
46string strip_string(string str)
47{
48 return implode(strip_explode(str," ")," ");
49}
50
51int string_compare(string a, string b)
52{
53 return a==b?0:(a>b?1:-1);
54}
55
56string cap_string(string str)
57{
58 return capitalize(str);
59}
60
61string short_path(string file)
62{
63 int s;
64 string tmp;
65
66 if(!file)
67 return 0;
68 if(PL)
69 {
70 if(file[0.. 8]=="/players/")
71 {
72 s=sizeof(getuid(PL))+8;
73 return "~"+(file[9.. s]==getuid(PL) ?
74 file[s+1.. <1] : file[9.. <1]);
75 }
76 }
77 if(file[0.. 2]=="/d/")
78 return "+"+file[3.. <1];
79 else
80 return file;
81}
82
83string long_path(string file)
84{
85 return (string)MASTER->make_path_absolute(file);
86}
87
88string *long_get_dir(string pat, int all)
89{
90 int i, s;
91 string str, dir, *file, *tmp;
92
93 if(!pat)
94 return ({});
95 pat=long_path(pat);
96 if(tmp=old_explode(pat, "/"))
97 dir="/"+implode(tmp[0..sizeof(tmp)-2], "/")+"/";
98 else
99 dir="/";
100 s=sizeof(tmp=get_dir(pat));
101 file=({});
102 for(i=0;i<s;i++)
103 {
104 str=dir+tmp[i];
105 if(all||file_size(str)>=0)
106 file+=({str});
107 }
108 return file;
109}
110
111string lit_string(string str)
112{
113 str=string_replace(str, "\\", "\\\\");
114 str=string_replace(str, "\b", "\\b");
115 str=string_replace(str, "\n", "\\n");
116 str=string_replace(str, "\r", "\\r");
117 str=string_replace(str, "\t", "\\t");
118 return string_replace(str, "\"", "\\\"");
119}
120
121string mixed_to_string(mixed mix, int lvl)
122{
123 int i, j, s, t;
124 string str;
125 mixed *keys;
126
127 if(lvl)
128 {
129 switch(typeof(mix))
130 {
131 default:
132 case T_INVALID:
133 return "<invalid>";
134 case T_STRUCT:
135 return to_string(mix);
136 case T_LVALUE:
137 return "&"+mixed_to_string(mix, lvl-1);
138 case T_NUMBER:
139 case T_FLOAT:
140 return to_string(mix);
141 case T_STRING:
142 return "\""+lit_string(mix)+"\"";
143 case T_POINTER:
144 return "({"+implode(map(mix,"mixed_to_string",ME,lvl-1),",")+"})";
145 case T_OBJECT:
146 return "["+short_path(object_name((object)mix))+"]";
147 case T_MAPPING:
148 s=sizeof(keys=m_indices(mix));
149 t=get_type_info(mix, 1);
150 str="([";
151 for(i=0;i<s;i++)
152 {
153 str+=mixed_to_string(keys[i], lvl-1);
154 if(t)
155 {
156 str+=":"+mixed_to_string(mix[keys[i],0], lvl-1);
157 for(j=1;j<t;j++)
158 str+=";"+mixed_to_string(mix[keys[i],j], lvl-1);
159 }
160 if(i<s-1) str+=",";
161 }
162 return str+"])";
163 case T_CLOSURE:
164 case T_SYMBOL:
165 return sprintf("%O", mix);
166 case T_QUOTED_ARRAY:
167 return "'"+mixed_to_string(funcall(lambda(0, mix)), lvl-1);
168 }
169 }
170 return "...";
171}
172
173int is_player(object obj)
174{
175 return is_obj(obj)&&query_once_interactive(obj);
176}
177
178int is_not_player(object obj)
179{
180 return is_obj(obj)&&!is_player(obj);
181}
182
183int round(float val)
184{
185 int tmp;
186
187 tmp=(int)val;
188 // only positive val
189 if( val - tmp >= 0.5 )
190 return tmp+1;
191 return tmp;
192}