Author Topic: How to retrieve path, name or extension strings for files of arbitrary size?  (Read 2012 times)

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
The command
Code: [Select]
COPY - ~infile~ ~outfile~offers an easy way to access different parts of input and output filepath strings, such as file extension, name or path.

Unfortunately, it doesn't work with larger files where you have to use COPY_LARGE instead. And ACTION_BASH_FOR requires both a path and a regular expression to produce results.

Is there another way to set the respective variables (SOURCE_FILESPEC, SOURCE_FILE, SOURCE_RES, SOURCE_EXT, ...) for files of any size?

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
I can make COPY_LARGE set the variables, if that's what you are after. It's probably just a case of COPY_LARGE being less developed, because it's less frequently used.

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
Yes, that would help me greatly. Either this or a separate action/patch function that generates variables such as returned by COPY or BASH_FOR from a (valid) file path.
« Last Edit: July 31, 2015, 02:44:31 PM by Argent77 »

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Done. (As COPY_LARGE.)
« Last Edit: August 01, 2015, 06:01:31 AM by Wisp »

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
The variables are generated successfully, although I have to use action commands to access their content. However, it looks like the backup option '-' doesn't work yet for COPY_LARGE. Since COPY_LARGE doesn't allow patching anyway, it just needs to return the appropriate SOURCE_* and DEST_* variables without doing anything else. Is it possible to add this feature? It would save me lots of needless copy and delete operations.

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
It sounds like we're getting into the realm of side-effects, so perhaps it would be better if I wrote something more straight to the point. Something like (RES|EXT|DIRECTORY)_OF_FILESPEC perhaps?

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
Yeah, that's probably a much better approach.

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
(DIRECTORY|FILE|RES|EXT)_OF_FILESPEC are done. Details of the implementation are still open for revisions (and will be until 239). Size is not yet done and will be implemented as a value.


Code: [Select]
DEFINE_ACTION_FUNCTION DIRECTORY_OF_FILESPEC
  STR_VAR
    filespec = ""
  RET
    directory
BEGIN
  OUTER_INNER_PATCH_SAVE filespec "%filespec%" BEGIN
    REPLACE_TEXTUALLY EXACT_MATCH "\" "/"
  END
  OUTER_SET length = STRING_LENGTH "%filespec%"
  OUTER_SET slash = RINDEX ("/" "%filespec%")
  ACTION_IF length > 0 AND slash > 0 BEGIN
    LAF SUBSTRING
      INT_VAR
        start = 0
        length = slash
      STR_VAR
        string = EVAL "%filespec%"
      RET
        directory = substring
    END
  END ELSE ACTION_IF length = 0 BEGIN
    WARN ~WARNING: DIRECTORY_OF_FILESPEC got a filespec of 0 length~
    OUTER_SPRINT directory ""
  END ELSE ACTION_IF NOT slash > 0 BEGIN
    WARN ~WARNING: DIRECTORY_OF_FILESPEC cannot find a directory in filespec %filespec%~
    OUTER_SPRINT directory ""
  END
END

DEFINE_ACTION_FUNCTION FILE_OF_FILESPEC
  STR_VAR
    filespec = ""
  RET
    file
BEGIN
  OUTER_INNER_PATCH_SAVE filespec "%filespec%" BEGIN
    REPLACE_TEXTUALLY EXACT_MATCH "\" "/"
  END
  OUTER_SET length = STRING_LENGTH "%filespec%"
  OUTER_SET slash = RINDEX ("/" "%filespec%")
  ACTION_IF length > 0 BEGIN
    ACTION_IF slash >= 0 BEGIN
      LAF SUBSTRING
        INT_VAR
          start = slash + 1
          length = length - slash - 1
        STR_VAR
          string = EVAL "%filespec%"
        RET
          file = substring
      END
    END ELSE ACTION_IF slash < 0 BEGIN
      OUTER_SPRINT file "%filespec%"
    END
  END ELSE BEGIN
    WARN ~WARNING: FILE_OF_FILESPEC got a filespec of 0 length~
    OUTER_SPRINT file ""
  END
END

DEFINE_ACTION_FUNCTION RES_OF_FILESPEC
  STR_VAR
    filespec = ""
  RET
    res
BEGIN
  OUTER_INNER_PATCH_SAVE filespec "%filespec%" BEGIN
    REPLACE_TEXTUALLY EXACT_MATCH "\" "/"
  END
  OUTER_SET length = STRING_LENGTH "%filespec%"
  OUTER_SET slash = RINDEX ("/" "%filespec%")
  OUTER_SET dot = RINDEX ("\." "%filespec%")
  ACTION_IF length > 0 AND dot > 0 BEGIN
    ACTION_IF slash >= 0 BEGIN
      LAF SUBSTRING
        INT_VAR
          start = slash + 1
          length = dot - slash - 1
        STR_VAR
          string = EVAL "%filespec%"
        RET
          res = substring
      END
    END ELSE ACTION_IF slash < 0 BEGIN
      LAF SUBSTRING
        INT_VAR
          start = 0
          length = dot
        STR_VAR
          string = EVAL "%filespec%"
        RET
          res = substring
      END
    END
  END ELSE ACTION_IF length = 0 BEGIN
    WARN ~WARNING: RES_OF_FILESPEC got a filespec of 0 length~
    OUTER_SPRINT res ""
  END ELSE ACTION_IF NOT dot > 0 BEGIN
    WARN ~WARNING RES_OF_FILESPEC cannot find a res in filespec %filespec%~
    OUTER_SPRINT res ""
  END
END

DEFINE_ACTION_FUNCTION EXT_OF_FILESPEC
  STR_VAR
    filespec = ""
  RET
    ext
BEGIN
  OUTER_SET length = STRING_LENGTH "%filespec%"
  OUTER_SET dot = RINDEX ("\." "%filespec%")
  ACTION_IF length > 0 AND dot > 0 BEGIN
    LAF SUBSTRING
      INT_VAR
        start = dot + 1
        length = length - dot - 1
      STR_VAR
        string = EVAL "%filespec%"
      RET
        ext = substring
    END
  END ELSE ACTION_IF length = 0 BEGIN
    WARN ~WARNING: EXT_OF_FILESPEC got a filespec of 0 length~
    OUTER_SPRINT ext ""
  END ELSE ACTION_IF NOT dot > 0 BEGIN
    WARN ~WARNING: EXT_OF_FILESPEC cannot find a file extension in filespec %filespec%~
    OUTER_SPRINT ext ""
  END
END

DEFINE_PATCH_FUNCTION DIRECTORY_OF_FILESPEC
  STR_VAR
    filespec = ""
  RET
    directory
BEGIN
  INNER_ACTION BEGIN
    LAF DIRECTORY_OF_FILESPEC
      STR_VAR
        filespec
      RET
        directory
    END
  END
END

DEFINE_PATCH_FUNCTION FILE_OF_FILESPEC
  STR_VAR
    filespec = ""
  RET
    file
BEGIN
  INNER_ACTION BEGIN
    LAF FILE_OF_FILESPEC
      STR_VAR
        filespec
      RET
        file
    END
  END
END

DEFINE_PATCH_FUNCTION RES_OF_FILESPEC
  STR_VAR
    filespec = ""
  RET
    res
BEGIN
  INNER_ACTION BEGIN
    LAF RES_OF_FILESPEC
      STR_VAR
        filespec
      RET
        res
    END
  END
END

DEFINE_PATCH_FUNCTION EXT_OF_FILESPEC
  STR_VAR
    filespec = ""
  RET
    ext
BEGIN
  INNER_ACTION BEGIN
    LAF EXT_OF_FILESPEC
      STR_VAR
        filespec
      RET
        ext
    END
  END
END
« Last Edit: August 02, 2015, 03:36:15 PM by Wisp »

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
The new functions are working great in most situations. However, I have found a few edge cases where path strings are not yet handled properly and produce errors.

1. Filenames without extension (e.g. "mymod/myfolder/myfile") produce the following errors in
RES_OF_FILESPEC: Failure("Uninitialised return value: res")
EXT_OF_FILESPEC: Failure("Uninitialised return value: ext")

2. Path strings ending with path-separator characters (e.g. "mymod/myfolder/") produce the following error in
RES_OF_FILESPEC: ERROR: SUBSTRING: invalid argument: length

3. Path strings ending with path-separator characters and parent folder name has extension (e.g. "mymod/myfolder.ext/")
EXT_OF_FILESPEC: No error, but returns "ext/" as result

I'd suggest to treat filenames as empty strings if the path string contains trailing path-separator characters.

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
It should be possible to handle these cases. Thanks.

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Any preferences for what RES_OF_FILESPEC should return for extensionless files? An empty string or whatever's behind the last slash?
« Last Edit: August 08, 2015, 12:49:57 PM by Wisp »

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
I would prefer anything behind the last slash to avoid ambiguities, unless you're planning to add a check for the existence of the specified path.

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
The *_OF_FILESPEC functions in WeiDU v238.01 (Windows version) are working great. All the bugs mentioned in one of my previous posts seem to be fixed.

I have found another edge case though where I'm not sure how to handle it correctly. Using a path such as "mymod/myfolder/.ext" returns ".ext" for RES_OF_FILESPEC and an empty string for EXT_OF_FILESPEC. Should ".ext" returned by RES_* or rather by EXT_*?

DIRECTORY_EXISTS is working fine as well. I couldn't provoke any kind of errors yet. It even works for NTFS junction points.

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
My view is that dotfiles are not file extensions with no file name. Their filename include a dot and they have no extension.

 

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Name: Email:
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:
What color is grass?:
What is the seventh word in this sentence?:
What is five minus two (use the full word)?: