Power Status (was Re: Because LC can't do two things at once.)

Peter M. Brigham pmbrig at gmail.com
Fri Mar 6 07:55:50 EST 2015


On Mar 5, 2015, at 2:13 PM, Mark Wieder wrote:

> Peter Brigham <pmbrig at ...> writes:
> 
>> 
>> Double-check me on this.
>> 
> 
> No, you're still checking the enclosing directory instead of the file. Grab
> "/sys/class/power_supply/BAT0/status"
> and the same for BAT1.

Right, I realized that last yesterday before I saw your reply. I've removed the terminal slashes on those URLs.

> ...and the caseSwitch function confuses me a bit:
> 
> I don't think the 'if tMatch = "*"' part is going to do anything useful here
> (do you really expect the contents of the status file to be "*"?
> 
> and there's no processing for the case where tCheckValue is *not* among the
> items of tMatch *and* tMatch is not "*" (will return empty).

See the comments at the start of the caseSwitch function, and look at how the script handles "*" -- it indicates the default case, if none of the previous matchValues match the checkValue.

I suppose you could argue that using an if-else-then control structure instead of caseSwitch() would be less opaque. I happen to like the compactness of using Ken Ray's function for situations like this. De gustibus non est disputandum.

function caseSwitch
   -- does a quick inline switch/case
   -- param 1 is checkValue
   -- params 2+ are in the form matchValue(s)=returnValue
   --    separate multiple matchValues with commas
   --    and enclose each matchValue=returnValue pair in quotes
   -- if checkValue matches one or more items in matchValue(s),
   --    returns returnValue
   -- note that checkValue should NOT be enclosed in quotes
   -- use a matchValue of "*" to specify a default value,
   --    to be returned if no matches found in the list
   --    if the default is "*=*" then no match returns the original checkValue
   --    if no match and no default value specified, then returns empty
   -- usage:
   --    put caseSwitch(len(tZip),"5=zip","10=zip+4","*=not a zip code") \
      --             into zipCodeType
   -- from Ken Ray, use-LC list, originally named stsSwitch()
   -- revised by Peter M. Brigham, pmbrig at gmail.com
   --    to allow for an empty checkValue, eg "=empty input"
   
   put param(1) into tCheckValue
   set the itemDel to "="
   put "" into tDefault
   repeat with x = 2 to the paramCount
      put param(x) into tCheck
      put item 1 of tCheck into tMatch
      put item 2 of tCheck into tRetVal
      replace "," with "=" in tMatch
      if tCheckValue = empty and tMatch = empty then return tRetVal
      if tCheckValue is among the items of tMatch then return tRetVal
      if tMatch = "*" then
         if tRetVal = "*" then
            put tCheckValue into tDefault
         else
            put tRetVal into tDefault
         end if
      end if
   end repeat
   return tDefault
end caseSwitch

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig





More information about the Use-livecode mailing list