blob: fdf40b8a98906806179468c4e89d13108712a686 [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;
39 string tmp;
40
41 if(!file)
42 return 0;
43 if(PL)
44 {
45 if(file[0.. 8]=="/players/")
46 {
47 s=sizeof(getuid(PL))+8;
Zesstra228dec72018-11-08 20:28:20 +010048 return "~"+(file[9.. s]==getuid(PL) ? file[s+1.. <1] : file[9.. <1]);
MG Mud User88f12472016-06-24 23:31:02 +020049 }
50 }
51 if(file[0.. 2]=="/d/")
52 return "+"+file[3.. <1];
53 else
54 return file;
55}
56
57string long_path(string file)
58{
Zesstra4bf2e1f2018-11-08 20:47:58 +010059 return normalize_path(file, getuid(this_interactive() || PL), 1);
MG Mud User88f12472016-06-24 23:31:02 +020060}
61
62string *long_get_dir(string pat, int all)
63{
64 int i, s;
65 string str, dir, *file, *tmp;
66
67 if(!pat)
68 return ({});
69 pat=long_path(pat);
70 if(tmp=old_explode(pat, "/"))
71 dir="/"+implode(tmp[0..sizeof(tmp)-2], "/")+"/";
72 else
73 dir="/";
74 s=sizeof(tmp=get_dir(pat));
75 file=({});
76 for(i=0;i<s;i++)
77 {
78 str=dir+tmp[i];
79 if(all||file_size(str)>=0)
80 file+=({str});
81 }
82 return file;
83}
84
85string lit_string(string str)
86{
87 str=string_replace(str, "\\", "\\\\");
88 str=string_replace(str, "\b", "\\b");
89 str=string_replace(str, "\n", "\\n");
90 str=string_replace(str, "\r", "\\r");
91 str=string_replace(str, "\t", "\\t");
92 return string_replace(str, "\"", "\\\"");
93}
94
95string mixed_to_string(mixed mix, int lvl)
96{
97 int i, j, s, t;
98 string str;
99 mixed *keys;
100
101 if(lvl)
102 {
103 switch(typeof(mix))
104 {
105 default:
106 case T_INVALID:
107 return "<invalid>";
MG Mud User88f12472016-06-24 23:31:02 +0200108 case T_LVALUE:
109 return "&"+mixed_to_string(mix, lvl-1);
110 case T_NUMBER:
111 case T_FLOAT:
112 return to_string(mix);
113 case T_STRING:
114 return "\""+lit_string(mix)+"\"";
115 case T_POINTER:
116 return "({"+implode(map(mix,"mixed_to_string",ME,lvl-1),",")+"})";
117 case T_OBJECT:
118 return "["+short_path(object_name((object)mix))+"]";
119 case T_MAPPING:
120 s=sizeof(keys=m_indices(mix));
121 t=get_type_info(mix, 1);
122 str="([";
123 for(i=0;i<s;i++)
124 {
125 str+=mixed_to_string(keys[i], lvl-1);
126 if(t)
127 {
128 str+=":"+mixed_to_string(mix[keys[i],0], lvl-1);
129 for(j=1;j<t;j++)
130 str+=";"+mixed_to_string(mix[keys[i],j], lvl-1);
131 }
132 if(i<s-1) str+=",";
133 }
134 return str+"])";
135 case T_CLOSURE:
136 case T_SYMBOL:
137 return sprintf("%O", mix);
138 case T_QUOTED_ARRAY:
139 return "'"+mixed_to_string(funcall(lambda(0, mix)), lvl-1);
Zesstraa9b533e2020-01-21 11:46:08 +0100140 case T_BYTES:
141 case T_STRUCT:
142 return sprintf("%O",mix);
MG Mud User88f12472016-06-24 23:31:02 +0200143 }
144 }
145 return "...";
146}
147
148int is_player(object obj)
149{
150 return is_obj(obj)&&query_once_interactive(obj);
151}
152
153int is_not_player(object obj)
154{
155 return is_obj(obj)&&!is_player(obj);
156}
157
158int round(float val)
159{
160 int tmp;
161
162 tmp=(int)val;
163 // only positive val
164 if( val - tmp >= 0.5 )
165 return tmp+1;
166 return tmp;
167}