View/Edit #13:_move_file _move!_file _copy_file _copy!_file

Switch to edit mode.

Name(s): _move_file _move!_file _copy_file _copy!_file
1:   "Move or copy a file.  Called by @move or @copy."
2:   "Returns {dest_obj, dest_fname, 'file'|'prop'|'verb'} if move/copy was successful."
3:   set_task_perms(caller_perms())
4:   {object, fname, iobjstr} = args
5:   if (iobjstr[1] == "!")
6:     "@move/@copy me!foo to !bar"
7:     to = {object, iobjstr[2..$], "file"}
8:   elseif (to = $code_utils:parse_fileref(iobjstr))
9:     "@move/@copy me!foo to here!bar"
10:    if ($command_utils:object_match_failed(tobj = player:my_match_object(to[1]), to[1]))
11:      return
12:    endif
13:    to = {tobj, to[2], "file"}
14:  elseif (iobjstr[1] == ".")
15:    "@move/@copy me!foo to .bar"
16:    to = {object, iobjstr[2..$], "prop"}
17:  elseif (to = $code_utils:parse_propref(iobjstr))
18:    "@move/@copy me!foo to here.bar"
19:    if ($command_utils:object_match_failed(tobj = player:my_match_object(to[1]), to[1]))
20:      return
21:    endif
22:    to = {tobj, to[2], "prop"}
23:  elseif (iobjstr[1] == ":")
24:    "@move/@copy me.foo to :bar"
25:    to = {object, iobjstr[2..$], "verb"}
26:  elseif (to = $code_utils:parse_verbref(iobjstr))
27:    "@move/@copy me.foo to here:bar"
28:    if ($command_utils:object_match_failed(tobj = player:my_match_object(to[1]), to[1]))
29:      return
30:    endif
31:    to = {tobj, to[2], "verb"}
32:  else
33:    "@move/@copy me!foo to here"
34:    iobj = player:my_match_object(iobjstr)
35:    if ($command_utils:object_match_failed(iobj, iobjstr))
36:      return
37:    endif
38:    to = {iobj, fname, "file"}
39:  endif
40:  mode = verb[2..5]
41:  mode in {"move", "copy"} || raise(E_VERBNF, "I'm paranoid.")
42:  "----------------------------------"
43:  "All required info has been parsed."
44:  "----------------------------------"
45:  if (to[3] == "verb")
46:    player:tell("Sorry, unable to automatically ", mode, " files to verbs.")
47:    player:tell("For security reasons it is safer to copy and paste them yourself.")
48:    return
49:  elseif (to[3] == "file")
50:    if (verb == "_" + mode + "_file" && to[1]:fileexists(to[2]))
51:      player:tell(to[1], "!", to[2], " already exists.  Either @rmfile it, or use @", mode, "! to overwrite it.")
52:      return E_NACC
53:    endif
54:    result = `object:("file" + mode)(fname, to[1], to[2]) ! ANY'
55:    if (result == E_INVIND)
56:      player:sub_tell("%1 has no file called %2.", object, fname)
57:      return
58:    elseif (typeof(result) == $ERR)
59:      player:tell("Error: ", result)
60:      return
61:    endif
62:    player:sub_tell("%1!%2 %5 to %3!%4.", object, fname, to[1], to[2], mode == "move" ? "moved" | "copied")
63:  elseif (to[3] == "prop")
64:    try
65:      data = object:fileread(fname)
66:    except v (ANY)
67:      player:tell("Reading ", object, "!", fname, " --> ", v[1])
68:      return
69:    endtry
70:    typeof(data) == $LIST || raise(E_INVARG, "This error can't happen!")
71:    if (data == {})
72:      data = ""
73:    elseif (length(data) == 1)
74:      "Single line, attempt to parse it as a value."
75:      result = $string_utils:to_value(data[1])
76:      if (result[1])
77:        data = result[2]
78:      else
79:        "Could not parse as a value.  Leave it as a line of text."
80:        data = data[1]
81:      endif
82:    else
83:      "Multi-line text, leave it as such."
84:    endif
85:    if (!to[1]:has_property(to[2]))
86:      info = {caller_perms(), index(object:fileaccess(fname), "r") ? "rc" | "c"}
87:      if (E_INVARG == (e = `add_property(@to[1..2], data, info) ! ANY'))
88:        player:tell("Property is already defined on one or more descendents.")
89:        player:tell("Try @check-prop ", to[1], ".", to[2])
90:        return
91:      elseif ($ERR == typeof(e))
92:        player:tell("Adding ", to[1], ".", to[2], " --> ", e)
93:        return
94:      endif
95:    elseif (to[2] in $code_utils.builtin_props)
96:      player:tell("Can't set builtin properties this way.")
97:      player:tell("Use @rename for .name, @move/@eject for .location/.contents, and @chmod for .r, .w, and .f")
98:      return
99:    elseif (verb == "_" + mode + "_file")
100:     player:tell(to[1], ".", to[2], " already exists.  Either @rmprop it, or use @", mode, "! to overwrite it.")
101:     return E_NACC
102:   endif
103:   try
104:     to[1].(to[2]) = data
105:   except v (ANY)
106:     player:tell("Writing ", to[1], ".", to[2], " --> ", v[1])
107:     return
108:   endtry
109:   player:tell(to[1], ".", to[2], " value set.")
110:   if (mode == "move")
111:     "The file has been copied to the prop.  Now delete the original."
112:     if ($ERR == typeof(e = `object:filedelete(fname) ! ANY'))
113:       player:tell("Deleting ", object, ".", fname, " --> ", e)
114:       return
115:     endif
116:     player:tell(object, "!", fname, " removed.")
117:   endif
118: else
119:   raise(E_VARNF, "This error can't happen!")
120: endif
121: return to
122: "Last modified by Dax (#789) on Tue May  3 15:38:52 2005 MDT."

Verb arguments: this none this
Verb perms: Readable eXecutable NOT-Debug
Verb owned by: Slacker (#55)

You are not logged in.

[home | help | who | search | setup | code]