PtokaX forum

Development Section => Your Developing Problems => Topic started by: Shurlock on 26 September, 2004, 20:54:50

Title: How to find the backslash in a string??
Post by: Shurlock on 26 September, 2004, 20:54:50
Hi all,

Need some help here.
I'm trying to find the last backslash in a string.

Example:
string is: C:\foldername\subfolder\filename.txt

I would like to find the position within the string where the last backslash is found.

Anyone that can help me out with this?

Thanks in advance.  :D
Title:
Post by: nErBoS on 26 September, 2004, 22:32:13
Hi,

Is a aproach to your question...

string = "C:\\foldername\\subfolder\\filename.txt"

local len1,len2 = 1,strlen(string)
while len1 ~= len2+1 do
if (strsub(string,len1,len1) == "\\") then
printf("FOUND: \\. POS: "..len1)
end
len1 = len1 + 1
end

In a string \\ = \, because \ is use to other things.

Best regards, nErBoS
Title:
Post by: NotRabidWombat on 26 September, 2004, 22:45:36
local sTest = "C:\\foldername\\subfolder\\filename.txt"
local iPos = strfind(sTest, "\\[^\\]*$");
print("Position: " .. iPos);

-NotRabidWombat
Title:
Post by: Shurlock on 27 September, 2004, 00:01:44
Thanks guys, but.... I think my problem has just become even worse!

Here's the problem:
from within the client (DC++) I am using a 'User Command' stating the following instruction: !filewarn %[nick] %[file]

I would like to use the variable '%[file]' to detect that 'last backslash'.
But.... how do I get to the point of changing that variable result to contain a double  backslash?

The meaning of all this is:
- the present function will warn users of an undesired file
- I would like to warn users of the undesired folder in which that file was found (mostly, any folder containing a PORN file, will contain more of those files)

I believe this function to be very usefull for other hub-owners, as the result of a warning works a LOT better than just plain kicking the user.

Hope this is clear. If not, I will try to go into detail a bit further.

In any case: thanks for the help!
Title:
Post by: Herodes on 27 September, 2004, 00:12:05
file = gsub(file, string.char(92), string.char(92)..string.char(92)) ...

possible?
Title:
Post by: Shurlock on 27 September, 2004, 00:21:18
Thanks for the suggestion Herodes, but.....
I think not. The result in mainchat (which I capture of course) from the function mentioned is the following:

*** I have warned user [nickname]  about FILE: filmer\Bilar\Trafikpolisens Klipp\Are You Fucking Crazy.mpg

The problem is that, should I want to warn about the folder that contains the mentioned file, I would have to eliminate the 'filename' part.

Searching for DOUBLE 'char(92)' will not have a result, as the original presented string does not contain  two such characters.   X(

In other words: I wish the 'User commands' of the client would also provide the varaiable %[folder] and not just %[file]. But..... since the client does not provide it, I must search for other possibilities.   :rolleyes:
Title:
Post by: bastya_elvtars on 27 September, 2004, 00:26:07
path=string.gsub(filename_with_path,".[^"..string.char(92).."]$","")

this possible?
Title:
Post by: Shurlock on 27 September, 2004, 00:29:14
I'd be eternally greatfull if that works!

But.... I also wish to know "what I'm doing here".  8)

If it's not too much to ask: What would this do?

Is the solution you wrote:
path=string.gsub(filename_with_path,".[^"..string.char(92).."]$","")
acceptable in LUA?

And - if so - would this tell me the FIRST or the LAST position of a backslash?
Whichever, that would be a less problem. A loop would do what I want it to.

(PS: sorry for the re-edits!!!)
Title:
Post by: BottledHate on 27 September, 2004, 01:46:51
here's a function i made awhile ago.. maybe it can help with what you are tryingto do.. i'm not sure...

--//-------------------------
--//VERIFY FILE (make)
--//-------------------------
--///verifyFile()\\\
--//  make file and path(s)
--//  can used to verify a save/load file at start to prevent read/write errors
--//  when the verify funtion is used no changes are made to the file if it exists. a blank file with no data is created otherwise.
--//  optional returns: 1 file exists.. 2 on success new file made returns.. 0 on fail
--//        ...fails if path/filename is invalid to winblows.
--//  strip the function, put in main or at top of script to verify/make your global file at start.
--//  paths may be absolute: "c:\\path\\to\\a dir\\file.txt"
--//  relative to current dir: "scripts/save data/savefile.txt"
--//  UNC(network) paths use "\\\\pcname\\share\\this folder\\file.txt" --only if proper rights exist.. ;)

--//LUA5 / BCDC / DCDM
         function verifyFile(file) --//(make file)
            local f,e = io.open(file, "a+" )
            if f then f:close() return 1
            else
               local _,_,path = string.find(file, "(.+[/_\\]).+$")
               if path ~= nil then os.execute("mkdir ".."\""..gsub(path, "/", "\\").."\"") end
               f,e = io.open( file, "a+" )
               if f then f:close() return 2
               else return 0 end
            end
         end
         
--//LUA4 / PTOKAX
         function verifyFile(file)--//(make file)
            local f,e = openfile( file, "a+" )
            if f then closefile(f) return 1
            else
               local _,_,path = strfind(file, "(.+[/_\\]).+$")
               if path ~= nil then execute("mkdir ".."\""..gsub(path, "/", "\\").."\"") end
               f,e = openfile( file, "a+" )
               if f then closefile(f) return 2
               else return 0 end
            end
         end
         
--//verifFile() EXAMPLE.
         MySaveFile = "scripts/data/thisbot/settings.txt"
         x = verfyFile(MySaveFile)
         if x = 0 then
            --fail..path/filename is invalid to winblows
         elseif x = 1
            --file exists.
         elseif x = 2
            --file or path did not exist.. now they do.
         end


-BH
Title:
Post by: NotRabidWombat on 27 September, 2004, 04:49:17
Shurlock,

The backslash is an escape character in Lua (and many other programming languages). It *escapes* other characters so they can be used in a string. A possible usage:
local sTest = "This is a \" test";

The backslash allows the developer to add a double quote into a string without the compiler considering that double quote the end of the string.

Since the backslash is used this way, you must actually escape a backslash if you want to represent it in a string, hence "\\".

Try a simple test like:
SendToAll("tester" , "This \\  is \" my \' escape \\ test");

Now that you've actually asked your final question, I would recommend:
function DataArrival(oUser, sData)
   -- do some basic user authentication (operator, admin, nick, etc)
   local flag, flag, sNickname, sFolder, sFile =
      strfind(sData, "^%b<>%s+!filewarn%s+(%S+)%s+(.*\\)([^\\]+)%s*$");
      -- regex voodoo ;-)
   if( flag ~= nil ) then
      -- you have sNickname, sFolder, sFile in here
   end
end

By the way, 92 is the ascii value of \. That's why you were seeing people recommend strchar(92) instead of \\. More information on ascii values: http://www.asciitable.com/

-NotRabidWombat
Title:
Post by: Shurlock on 27 September, 2004, 16:59:55
GOT IT !!  

Thanks people, with the hints and help I finally came up with something that 'does the job'.

The function below will split the data within the variable  %[file] (which is being sent by the User command !folderwarn %[nick] %[file] )  into seperate foldername and the filename.

The faulty user is being sent a message to remove the 'FOLDER' as it contains files like 'FILE'

Hope it will be of use to someone else as well.

---------------------------------------
function FolderWarn(curUser, vUser, filename)
   local chrtr
   local folder, file = ""
   for i=1,strlen(filename) do
      chrtr = strsub(filename, i, i)
      if chrtr == "\\" then
         folder = strsub(filename, 1, i)
         file = strsub(filename, i+1, strlen(filename))
      end
   end
   local gmt = ("\n"..date("%d").."-"..date("%m").."-"..date("%y").." "..date("%H")..":"..date("%M").."")
   local Mess = "\r\n\r\n\t---<>--------------------------------------------------------------------<>---"
   Mess = Mess.."\r\n\tWarning: do NOT get kicked!"
   Mess = Mess.."\r\n\tPlease remove the following FOLDER from your share: "
   Mess = Mess.."\r\n\r\n\t\t"..folder
   Mess = Mess.."\r\n\r\n\tas it contains files that will get you kicked, such as:"
   Mess = Mess.."\r\n\t\t"..file
   Mess = Mess.."\r\n\t---<>--------------------------------------------------------------------<>---"
   Mess = Mess.."\r\n\tAfter removing, do not forget to enter the command in the mainchat:\r\n\t\t\t /refesh"
   Mess = Mess.."\r\n\r\n\r\n\tPlease respond to this message using the command:"
   Mess = Mess.."\r\n\t!ops [your response]      (Where [your response] is your reaction to this warning!)"
   SendPmToNick(vUser, curUser.sName, Mess)
   SendToOps(curUser.sName, "*** I have warned user "..vUser.."  about FOLDER: \r\n"..folder)
   appendto("Logs/foldwarn.txt")
   write(gmt,"  ", curUser.sName,"  ", vUser,"  ", folder)
   writeto()
   return 1
end

---------------------------------------