PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/camerb-OCR-function/thirdParty/st.ahk

https://github.com/camerb/AHKs
AutoHotKey | 141 lines | 69 code | 12 blank | 60 comment | 0 complexity | b52645f137d01f1602e14dcedc1a19a3 MD5 | raw file
  1. ; ST - Stack Library by Banane
  2. ; http://www.autohotkey.com/forum/viewtopic.php?t=54153
  3. ;-----------------------------------------------------------------------------------------------------------------------------
  4. ;Function: ST_Dim
  5. ;Parameters: Stack = Specify a new variable
  6. ;Description: Declares the specified variable as a stack
  7. ;-----------------------------------------------------------------------------------------------------------------------------
  8. ST_Dim(ByRef Stack) {
  9. If (ST_IsValid(Stack,1) = 1) {
  10. If (ST_Debug() = 1)
  11. MsgBox, 48, ST Error, Specified Stack is already declared.
  12. Return 0
  13. }
  14. Stack :="[]"
  15. }
  16. ;-----------------------------------------------------------------------------------------------------------------------------
  17. ;Function: ST_Undim
  18. ;Parameters: Stack = Declared Stack
  19. ;Description: Deletes all contents from the stack variable
  20. ;-----------------------------------------------------------------------------------------------------------------------------
  21. ST_Undim(ByRef Stack) {
  22. If (ST_IsValid(Stack) = 0)
  23. Return 0
  24. Stack := ""
  25. }
  26. ;-----------------------------------------------------------------------------------------------------------------------------
  27. ;Function: ST_Del
  28. ;Parameters: Stack = Declared Stack
  29. ;Description: Deletes all entrys from the stack
  30. ;-----------------------------------------------------------------------------------------------------------------------------
  31. ST_Del(ByRef Stack) {
  32. If (ST_IsValid(Stack) = 0)
  33. Return 0
  34. Stack := "[]"
  35. }
  36. ;-----------------------------------------------------------------------------------------------------------------------------
  37. ;Function: ST_Push
  38. ;Parameters: Stack = Declared Stack
  39. Value = Entry's value
  40. ;Description: Adds an entry to the stack
  41. ;-----------------------------------------------------------------------------------------------------------------------------
  42. ST_Push(ByRef Stack,Value) {
  43. ;Check if it's a valid stack
  44. If (ST_IsValid(Stack) = 0)
  45. Return 0
  46. ;Add the value
  47. Stack := "[" . ST_Convert(Value) . "|" . SubStr(Stack,2,StrLen(Stack) - 2) . "]"
  48. }
  49. ;-----------------------------------------------------------------------------------------------------------------------------
  50. ;Function: ST_Pop
  51. ;Parameters: Stack = Declared Stack
  52. ;Description: Removes and returns the newest entrys contents
  53. ;-----------------------------------------------------------------------------------------------------------------------------
  54. ST_Pop(ByRef Stack) {
  55. ;Check if it's a valid stack
  56. If (ST_IsValid(Stack) = 0)
  57. Return 0
  58. Else If (ST_Len(Stack) < 1)
  59. Return 1
  60. ;Get first entry
  61. Value := SubStr(Stack,2,InStr(Stack,"|") - 2)
  62. ;Remove from stack
  63. Stack := "[" . SubStr(Stack,InStr(Stack,"|") + 1,StrLen(Stack))
  64. Return ST_Convert(Value,1)
  65. }
  66. ;-----------------------------------------------------------------------------------------------------------------------------
  67. ;Function: ST_Peek
  68. ;Parameters: Stack = Declared Stack
  69. ;Description: Returns the newest entrys contents without removing
  70. ;-----------------------------------------------------------------------------------------------------------------------------
  71. ST_Peek(ByRef Stack) {
  72. ;Check if it's a valid stack
  73. If (ST_IsValid(Stack) = 0)
  74. Return 0
  75. Else If (ST_Len(Stack) < 1)
  76. Return 1
  77. ;Return first entry
  78. Return ST_Convert(SubStr(Stack,2,InStr(Stack,"|") - 2),1)
  79. }
  80. ;-----------------------------------------------------------------------------------------------------------------------------
  81. ;Function: ST_Len
  82. ;Parameters: Stack = Declared Stack
  83. ;Description: Returns the count of entrys
  84. ;-----------------------------------------------------------------------------------------------------------------------------
  85. ST_Len(ByRef Stack) {
  86. ;Check if it's a valid stack
  87. If (ST_IsValid(Stack) = 0)
  88. Return 0
  89. ;Check how many entrys are in it
  90. Loop, Parse, Stack
  91. If (A_LoopField = "|")
  92. Length ++
  93. ;If Stack doesn't have any entrys
  94. If (ST_Debug() = 1 and Length = "")
  95. MsgBox, 48, ST Error, Specified Stack doesn't have any entrys.
  96. ;Return the length
  97. Return Length
  98. }
  99. ;-----------------------------------------------------------------------------------------------------------------------------
  100. ;Function: ST_Debug
  101. ;Parameters: OnOff = Boolean - 1 to activate, 0 to deactivate
  102. ;Description: Actiavtes error messages
  103. ;-----------------------------------------------------------------------------------------------------------------------------
  104. ST_Debug(OnOff="") {
  105. static Debug
  106. ;Change Debug State
  107. Debug := (OnOff = "") ? Debug : OnOff
  108. ;Return Debug State
  109. Return Debug
  110. }
  111. ;=============================================================================================================================
  112. ;Internal Functions
  113. ;=============================================================================================================================
  114. ST_Convert(Value,Mode=0) {
  115. ;Mode 0 = Convert to Stack Format
  116. ;Mode 1 = Convert from Stack Format
  117. If (Mode = 0)
  118. StringReplace, Value, Value, |, <'D'>, 1 ;Replace Delimiter
  119. Else
  120. StringReplace, Value, Value, <'D'>, |, 1 ;Change to Delimiter
  121. Return Value
  122. }
  123. ST_IsValid(ByRef Stack,Dim=0) {
  124. If (SubStr(Stack,1,1) = "[" and SubStr(Stack,StrLen(Stack),1) = "]")
  125. Return 1
  126. If (ST_Debug() = 1 and Dim = 0)
  127. MsgBox, 48, ST Error, Specified Stack isn't valid.
  128. Return 0
  129. }