blob: 4c83ace15964b3bca2938ed94f23ada07d6286a2 [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>";
108 case T_STRUCT:
109 return to_string(mix);
110 case T_LVALUE:
111 return "&"+mixed_to_string(mix, lvl-1);
112 case T_NUMBER:
113 case T_FLOAT:
114 return to_string(mix);
115 case T_STRING:
116 return "\""+lit_string(mix)+"\"";
117 case T_POINTER:
118 return "({"+implode(map(mix,"mixed_to_string",ME,lvl-1),",")+"})";
119 case T_OBJECT:
120 return "["+short_path(object_name((object)mix))+"]";
121 case T_MAPPING:
122 s=sizeof(keys=m_indices(mix));
123 t=get_type_info(mix, 1);
124 str="([";
125 for(i=0;i<s;i++)
126 {
127 str+=mixed_to_string(keys[i], lvl-1);
128 if(t)
129 {
130 str+=":"+mixed_to_string(mix[keys[i],0], lvl-1);
131 for(j=1;j<t;j++)
132 str+=";"+mixed_to_string(mix[keys[i],j], lvl-1);
133 }
134 if(i<s-1) str+=",";
135 }
136 return str+"])";
137 case T_CLOSURE:
138 case T_SYMBOL:
139 return sprintf("%O", mix);
140 case T_QUOTED_ARRAY:
141 return "'"+mixed_to_string(funcall(lambda(0, mix)), lvl-1);
142 }
143 }
144 return "...";
145}
146
147int is_player(object obj)
148{
149 return is_obj(obj)&&query_once_interactive(obj);
150}
151
152int is_not_player(object obj)
153{
154 return is_obj(obj)&&!is_player(obj);
155}
156
157int round(float val)
158{
159 int tmp;
160
161 tmp=(int)val;
162 // only positive val
163 if( val - tmp >= 0.5 )
164 return tmp+1;
165 return tmp;
166}