-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.erl
72 lines (55 loc) · 1.64 KB
/
util.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
%% @author M Wright <[email protected]>
%% @copyright Wright, MJ 2014-2015
%% @version 1.3
%% @title Utillity Module
%% @doc A Utility module with important functions
%% @end
-module(util).
-export([strp/1,connectintro/0,usercmdatom/1,addinspaces/1,addinchar/2]).
-include("recs.hrl").
%%MOTD
connectintro()->
Result = file:consult("data.conf"),
case Result of
{ok, Message}->
case lists:keysearch(cotd, 1, Message) of
{value, Recmotd}->Cotd = aCol:addcol({red,Recmotd#cotd.title})++Recmotd#cotd.text, Cotd;
{badmatch, false}-> Cotd = "Welcome to a basic MUD server \r\n Enjoy the stay!", Cotd%better a message than no message
end;
{error, Error} -> {fail, Error}
end.
%%Command parsing
usercmdatom(Input)->
case string:len(Input) of
0->{error, empty};%%no input string
_->splitstring(Input)
end.
splitstring(Input)->
case string:str(Input, " ") of
0->cmdstrip2(Input,"\r\n\t>");
_Pos->cmdstrip(Input,"\r\n\t> ")
end.
%%addin spaces
addinspaces([])-> "\r\n";
addinspaces([H|T])-> H++" "++addinspaces(T).
%%addin char
addinchar([],_)-> "\r\n";
addinchar([H|T],Char)-> H++Char++addinchar(T,Char).
%%stripping strings
cmdstrip(String,Chars)->
case string:tokens(String, Chars) of
[] -> {error, empty};
[Stripped]->{ok, Stripped};
[Stripped|_Rest]->{ok, Stripped,_Rest}
end.
cmdstrip2([],_Chars)-> {error, empty};
cmdstrip2(String,Chars)->
case string:tokens(String, Chars) of
[] -> {error, empty};
[Stripped]->{ok, Stripped}
end.
strp(String) ->
strp(String, "\r\n\t> ").
strp(String, Chars) ->
[Stripped|_Rest] = string:tokens(String, Chars),
Stripped.