blob: 709795dde972b8d4893f68647cfde361131d145d [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>
Zesstra228dec72018-11-08 20:28:20 +010014#include <strings.h>
MG Mud User88f12472016-06-24 23:31:02 +020015
MG Mud User88f12472016-06-24 23:31:02 +020016string *strip_explode(string str, string del)
17{
18 return explode(str, del)-({""});
19}
20
21string strip_string(string str)
22{
Zesstra228dec72018-11-08 20:28:20 +010023 return trim(str, TRIM_BOTH);
MG Mud User88f12472016-06-24 23:31:02 +020024}
25
26int string_compare(string a, string b)
27{
28 return a==b?0:(a>b?1:-1);
29}
30
31string cap_string(string str)
32{
33 return capitalize(str);
34}
35
36string short_path(string file)
37{
38 int s;
MG Mud User88f12472016-06-24 23:31:02 +020039
40 if(!file)
41 return 0;
42 if(PL)
43 {
44 if(file[0.. 8]=="/players/")
45 {
46 s=sizeof(getuid(PL))+8;
Zesstra228dec72018-11-08 20:28:20 +010047 return "~"+(file[9.. s]==getuid(PL) ? file[s+1.. <1] : file[9.. <1]);
MG Mud User88f12472016-06-24 23:31:02 +020048 }
49 }
50 if(file[0.. 2]=="/d/")
51 return "+"+file[3.. <1];
52 else
53 return file;
54}
55
56string long_path(string file)
57{
Zesstra4bf2e1f2018-11-08 20:47:58 +010058 return normalize_path(file, getuid(this_interactive() || PL), 1);
MG Mud User88f12472016-06-24 23:31:02 +020059}
60
61string *long_get_dir(string pat, int all)
62{
63 int i, s;
64 string str, dir, *file, *tmp;
65
66 if(!pat)
67 return ({});
68 pat=long_path(pat);
69 if(tmp=old_explode(pat, "/"))
70 dir="/"+implode(tmp[0..sizeof(tmp)-2], "/")+"/";
71 else
72 dir="/";
73 s=sizeof(tmp=get_dir(pat));
74 file=({});
75 for(i=0;i<s;i++)
76 {
77 str=dir+tmp[i];
78 if(all||file_size(str)>=0)
79 file+=({str});
80 }
81 return file;
82}
83
84string lit_string(string str)
85{
86 str=string_replace(str, "\\", "\\\\");
87 str=string_replace(str, "\b", "\\b");
88 str=string_replace(str, "\n", "\\n");
89 str=string_replace(str, "\r", "\\r");
90 str=string_replace(str, "\t", "\\t");
91 return string_replace(str, "\"", "\\\"");
92}
93
94string mixed_to_string(mixed mix, int lvl)
95{
96 int i, j, s, t;
97 string str;
98 mixed *keys;
99
100 if(lvl)
101 {
102 switch(typeof(mix))
103 {
104 default:
105 case T_INVALID:
106 return "<invalid>";
MG Mud User88f12472016-06-24 23:31:02 +0200107 case T_LVALUE:
108 return "&"+mixed_to_string(mix, lvl-1);
109 case T_NUMBER:
110 case T_FLOAT:
111 return to_string(mix);
112 case T_STRING:
113 return "\""+lit_string(mix)+"\"";
114 case T_POINTER:
115 return "({"+implode(map(mix,"mixed_to_string",ME,lvl-1),",")+"})";
116 case T_OBJECT:
bugfixaf2be4f2020-03-22 19:13:07 +0100117 return "["+short_path(object_name(mix))+"]";
Zesstraddddbf72021-05-14 16:52:16 +0200118 case T_LWOBJECT:
119 return sprintf("%O",mix);
MG Mud User88f12472016-06-24 23:31:02 +0200120 case T_MAPPING:
121 s=sizeof(keys=m_indices(mix));
122 t=get_type_info(mix, 1);
123 str="([";
124 for(i=0;i<s;i++)
125 {
126 str+=mixed_to_string(keys[i], lvl-1);
127 if(t)
128 {
129 str+=":"+mixed_to_string(mix[keys[i],0], lvl-1);
130 for(j=1;j<t;j++)
131 str+=";"+mixed_to_string(mix[keys[i],j], lvl-1);
132 }
133 if(i<s-1) str+=",";
134 }
135 return str+"])";
136 case T_CLOSURE:
137 case T_SYMBOL:
138 return sprintf("%O", mix);
139 case T_QUOTED_ARRAY:
140 return "'"+mixed_to_string(funcall(lambda(0, mix)), lvl-1);
Zesstraa9b533e2020-01-21 11:46:08 +0100141 case T_BYTES:
142 case T_STRUCT:
143 return sprintf("%O",mix);
MG Mud User88f12472016-06-24 23:31:02 +0200144 }
145 }
146 return "...";
147}
148
149int is_player(object obj)
150{
151 return is_obj(obj)&&query_once_interactive(obj);
152}
153
154int is_not_player(object obj)
155{
156 return is_obj(obj)&&!is_player(obj);
157}
158
159int round(float val)
160{
161 int tmp;
162
163 tmp=(int)val;
164 // only positive val
165 if( val - tmp >= 0.5 )
166 return tmp+1;
167 return tmp;
168}