AutoHotkey Community

It is currently May 27th, 2012, 2:53 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: September 30th, 2009, 9:32 am 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
requires newest update of autohotkey_l
still probably has bugs
version .1 alpha
i know there is lots of room for improvement, and im working on it.
:D

Test Script:
Code:
o := Options("","wp+100 h200 xp-20 yp+250 xs+200 ys-30 xm+10 ym-3 vMyProgress")          ; create options object
o.wp := "+400"
o.LastFound := "True"    ; Setting an Option to "True" (the string)  outputs +Option
o.ToolWindow := "True"
o.Caption := "False"    ; Setting Option to "False" (the string)  outputs -Option
;o.Color := "Blue"
MsgBox % "current options:`n" .  o.ToParam()

Esc::ExitApp



Code:
; options.ahkl
; requires AutoHotkey_L
; by ahklerner
;version .1 alpha
; This object  handles a gui's or control's "Options"
; intended use is by control and gui object... but can be modified to suit other purposes
;

Options_Opt(ByRef sOpts,sName,sVal="¬"){
   if (sVal = "¬"){ ; get the option
      if RegExMatch(sOpts, sName . "=(\+?-?[a-zA-Z0-9\s]*)¬",Match)
         return Match1
      else
         return ""
   }
   if RegExMatch(sOpts, sName . "=(\+?-?[a-zA-Z0-9\s]*)¬",Match)
      sOpts := RegExReplace(sOpts,sName . "=(\+?-?[a-zA-Z0-9\s]*)¬",sName . "=" . sVal . "¬")
   else if (sOpts = "¬")
      sOpts := sName . "=" . sVal . "¬"
   else
      sOpts .= sName . "=" . sVal . "¬"
   return sOpts
}


Options_GetVal(obj, name){
   if name = Parent
      return obj.Parent
   sOpts := obj.ToStr
   return Options_Opt(sOpts,name)
}

Options_SetVal(obj, name, val){
;MsgBox % "options:SetVal`nname:" . name . "`nval:" . val
   if name = Parent
      return obj.Parent := val

   if name = ToStr
      return obj.ToStr := val
   sOpts := obj.ToStr
   ret := Options_Opt(sOpts,name,val)
   obj.ToStr := sOpts
   return ret
}

Options_Import(obj,prm0){
   options := obj
   Loop, Parse, prm0, %A_Space%
      {
         if RegExMatch(A_LoopField,"wp(-[a-zA-Z0-9\s]*)",Match)
            out .= "wp=" . Match1 . "¬"
            , options.wp := Match1
         else if RegExMatch(A_LoopField,"hp(-[a-zA-Z0-9\s]*)",Match)
            out .= "hp=" . Match1 . "¬"
            , options.hp := Match1
         else if RegExMatch(A_LoopField,"wp(\+[a-zA-Z0-9\s]*)",Match)
            out .= "wp=" . Match1 . "¬"
            , options.wp := Match1
         else if RegExMatch(A_LoopField,"hp(\+[a-zA-Z0-9\s]*)",Match)
            out .= "hp=" . Match1 . "¬"
            , options.hp := Match1

         else if RegExMatch(A_LoopField,"xp(-[a-zA-Z0-9\s]*)",Match)
            out .= "xp=" . Match1 . "¬"
            , options.xp := Match1
         else if RegExMatch(A_LoopField,"yp(-[a-zA-Z0-9\s]*)",Match)
            out .= "yp=" . Match1 . "¬"
            , options.yp := Match1
         else if RegExMatch(A_LoopField,"xp(\+[a-zA-Z0-9\s]*)",Match)
            out .= "xp=" . Match1 . "¬"
            , options.xp := Match1
         else if RegExMatch(A_LoopField,"yp(\+[a-zA-Z0-9\s]*)",Match)
            out .= "yp=" . Match1 . "¬"
            , options.yp := Match1

         else if RegExMatch(A_LoopField,"xm(-[a-zA-Z0-9\s]*)",Match)
            out .= "xm=" . Match1 . "¬"
            , options.xm := Match1
         else if RegExMatch(A_LoopField,"xm(\+[a-zA-Z0-9\s]*)",Match)
            out .= "xm=" . Match1 . "¬"
            , options.xm := Match1

         else if RegExMatch(A_LoopField,"ym(-[a-zA-Z0-9\s]*)",Match)
            out .= "ym=" . Match1 . "¬"
            , options.ym := Match1
         else if RegExMatch(A_LoopField,"ym(\+[a-zA-Z0-9\s]*)",Match)
            out .= "ym=" . Match1 . "¬"
            , options.ym := Match1


         else if RegExMatch(A_LoopField,"xs(-[a-zA-Z0-9\s]*)",Match)
            out .= "xs=" . Match1 . "¬"
            , options.xs := Match1
         else if RegExMatch(A_LoopField,"xs(\+[a-zA-Z0-9\s]*)",Match)
            out .= "xs=" . Match1 . "¬"
            , options.xs := Match1

         else if RegExMatch(A_LoopField,"ys(-[a-zA-Z0-9\s]*)",Match)
            out .= "ys=" . Match1 . "¬"
            , options.ys := Match1
         else if RegExMatch(A_LoopField,"ys(\+[a-zA-Z0-9\s]*)",Match)
            out .= "ys=" . Match1 . "¬"
            , options.ys := Match1


         else if RegExMatch(A_LoopField,"ym",Match)
            out .= "ym=True¬"
            , options.ym := "True"
         else if RegExMatch(A_LoopField,"xm",Match)
            out .= "xm=True¬"
            , options.xm := "True"
         else if RegExMatch(A_LoopField,"xs",Match)
            out .= "xs=True¬"
            , options.xs := "True"
         else if RegExMatch(A_LoopField,"ys",Match)
            out .= "ys=True¬"
            , options.ys := "True"

         else if RegExMatch(A_LoopField,"v([a-zA-Z0-9\s]*)",Match){
            Match1 = %Match1%
            ;MsgBox Var Imported
            out .= "v" . "=" . Match1 . "¬"
            options.Parent.v := Match1
            obj.Parent[Match1] := options.Parent
         }
         
         else if RegExMatch(A_LoopField,"g([a-zA-Z0-9\s]*)",Match)
            out .= "g" . "=" . Match1 . "¬"
            , options.g := Match1
         else if RegExMatch(A_LoopField,"w([a-zA-Z0-9\s]*)",Match)
            out .= "w" . "=" . Match1 . "¬"
            , options.w := Match1
         else if RegExMatch(A_LoopField,"h([a-zA-Z0-9\s]*)",Match)
            out .= "h" . "=" . Match1 . "¬"
            , options.h := Match1
         else if RegExMatch(A_LoopField,"x([a-zA-Z0-9\s]*)",Match)
            out .= "x" . "=" . Match1 . "¬"
            , options.x := Match1
         else if RegExMatch(A_LoopField,"y([a-zA-Z0-9\s]*)",Match)
            out .= "y" . "=" . Match1 . "¬"
            , options.y := Match1
         else if RegExMatch(A_LoopField,"\+([a-zA-Z0-9\s]*)",Match)
            out .= Match1 . "=True¬"
            , options[Match1] := "True"
         else if RegExMatch(A_LoopField,"\-([a-zA-Z0-9\s]*)",Match)
            out .= Match1 . "=False¬"
            , options[Match1] := "False"
      }

}

Options_ToParam(obj){
   StringOpt := obj.ToStr
   Loop, Parse, StringOpt, ¬
   {
      RegExMatch(A_LoopField,"([a-zA-Z0-9\s]*)=",name)
      OptName = %name1%
      RegExMatch(A_LoopField,"=(\+?-?[a-zA-Z0-9\s]*)",val)
      OptVal = %val1%
      if OptVal = False
         RetVal .= "-" . OptName . " "
      else if OptVal = True
         RetVal .= "+" . OptName . " "
      else
         RetVal .= OptName . OptVal . " "
   }
   return RetVal
}

Options_SetParent(obj,parentObj){
}

Options(parent="",param="¬"){
   static COBase
   If !COBase
      COBase := Object(      "__Get"      , "Options_GetVal"  ;    default getter
                     ,   "__Set"      , "Options_SetVal"  ;    default setter
                     ,   "ToParam"   , "Options_ToParam" ;    .ToParam()
                     ,   "Import"   , "Options_Import") ;    .Import(sOptions)
;                     ,   "SetParent"   , "Options_SetParent") ;    .SetParent(parentObj)

   obj := Object("ToStr", " ", "Parent"," ","base", COBase)
   if (param != "¬"){
      obj.Import(param)
   }
   return obj
}



_________________
Image
ʞɔпɟ əɥʇ ʇɐɥʍ


Last edited by ahklerner on September 30th, 2009, 9:11 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 11:12 am 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
updated

_________________
Image
ʞɔпɟ əɥʇ ʇɐɥʍ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 9:12 pm 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
updated.......i think it handles all control options now.

_________________
Image
ʞɔпɟ əɥʇ ʇɐɥʍ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 28th, 2009, 5:01 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
Some comments:
  • I just noticed your big RegEx if..else if ladder, and it made me cringe.
  • The lack of ^anchor causes false positives such as the "w" in "+ToolWindow".
  • Most or all options are not case-sensitive, but your regexes are.
  • Your regexes include \s, for no apparent reason. It will not match any spaces (since the outer parsing loop excludes them), tabs should be equivalent to spaces and other whitespace characters probably aren't valid.
  • Options such as "xs +ys -xm" (whether this example is valid or not) come out as "+xs +ys +ym".
  • "\+?-?" is used in a few regexes; it should probably be "[+-]?" (i.e. allow either one or neither, but not both.)
  • "[a-zA-Z0-9\s]" is used even with options that allow only numbers or allow additional characters. (For instance, v_#var or g(practically-anything).) Although this isn't much of a problem, I would suggest using \w as it is easier on the eyes. It excludes some less common valid characters (for v and g), but includes one of the most common symbols: _.

Anyway, here's my take on the parsing loop in Options_Import:
Code:
Options_Import(obj,prm0){
   options := obj
   Loop, Parse, prm0, %A_Space%%A_Tab%
      {
         if RegExMatch(A_LoopField,"i)^([xy][mps]?|[wh]p?)(\+?-?\d\w*)$",Match)
            options[Match1] := Match2
 
         else if RegExMatch(A_LoopField,"i)^v(\w+)$",Match)
            options.Parent.v := Match1
            , obj.Parent[Match1] := options.Parent
 
         else if RegExMatch(A_LoopField,"^\+?(\w+)$",Match)
            options[Match1] := "True"
         else if RegExMatch(A_LoopField,"^\-(\w+)$",Match)
            options[Match1] := "False"
      }
}
It should perform better in addition to being much more maintainable and readable. It also allows a few options/values that didn't work before, but still doesn't handle all valid cases. Note the \d\w* part of the first regex: I think it should include all but the rarest valid options and exclude some false positives (like "wrap" and "hidden"). You could write a more complex regex to ensure it really is a number, but I suppose it wouldn't be worth it.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 9 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group