Microsoft Small Basic

Program Listing: ZSF735
' Get List of Stars from Wikipedia 0.1
' Copyright © 2016 Nonki Takahashi. The MIT License.
' Last update 2016-01-06
'
GraphicsWindow.Title = "Get List of Stars from Wikipedia 0.1"
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
WQ = Text.GetCharacter(34)
LT = "<"
symbol = "1=♑;2=♒;3=♓;4=♈;5=♉;6=♊;7=♋;8=♌;9=♍;10=♎;11=♏;12=♐;"
Not = "False=True;True=False;"
site = "http://en.wikipedia.org/wiki/List_of_stars_in_"
Form()
InitConstellation()
SelectConstellation()
sp[2] = " "
page = consName
While Text.IsSubText(page, " ")
p = Text.GetIndexOf(page, " ")
page = Text.GetSubText(page, 1, p - 1) + "_" + Text.GetSubTextToEnd(page, p + 1)
EndWhile
outbuf = "Sub InitStars" + CRLF
outbuf = outbuf + sp[2] + "' Initialize stars in " + consName + CRLF
outbuf = outbuf + sp[2] + "' index: Flamsteed designation" + CRLF
outbuf = outbuf + sp[2] + "' RA (Right ascension), Dec (Declination), "
outbuf = outbuf + "Mag (Apparent magnitude)" + CRLF
block = ""
buf = Network.GetWebPageContents(site + page)
p = 1
param = "tag=table;class=wikitable sortable;"
FindTag()
eod = "False"
buf = tag
p = 1
nStars = 0
len = Text.GetLength(buf)
state = 0
While Not[eod]
param = "tag=tr;"
FindTag()
Stack.PushValue("local", p)
Stack.PushValue("local", buf)
p = 1
buf = tag
col = 0
If tag = "" Then
eod = "True"
EndIf
param = "tag=th;"
FindTag()
If tag = "" Then
p = 1
While Not[eod]
If state = 0 Then
state = 1
EndIf
param = "tag=td;"
FindTag()
If tag = "" Then
eod = "True"
Else
col = col + 1
StripTags()
ConvertText()
If col = colName Then
' ex. γ1 And
name = txt
ElseIf col = colF Then
' ex. 57
fd = txt
ElseIf col = colRA Then
' ex. 02h 03m 53.92s
ra = Text.Append(Text.GetSubText(txt, 1, 2), " ")
ra = Text.Append(ra, Text.GetSubText(txt, 5, 2)) + " "
ra = ra + Text.GetSubText(txt, 9, 5)
ElseIf col = colDec Then
' ex. +42° 19′ 47.5″
dec = Text.Append(Text.GetSubText(txt, 1, 3), " ")
dec = Text.Append(dec, Text.GetSubText(txt, 6, 2)) + " "
dec = dec + Text.GetSubText(txt, 10, 4)
If Text.GetSubText(dec, 1, 1) = "−" Then
dec = "-" + Text.GetSubTextToEnd(dec, 2)
EndIf
ElseIf col = colVis Then
' ex. 2.10
mag = txt
eod = "True"
EndIf
EndIf
EndWhile
If fd <> "" Then
outbuf = outbuf + sp[2] + "star[" + fd + "] = " + WQ
outbuf = outbuf + "name=" + name + ";"
outbuf = outbuf + "ra=" + ra + ";"
outbuf = outbuf + "dec=" + dec + ";"
outbuf = outbuf + "mag=" + mag+ ";" + WQ + CRLF
Controls.SetTextBoxText(tbox, outbuf)
EndIf
Else
If state = 0 Then
p = 1
While Not[eod]
param = "tag=th;"
FindTag()
If tag = "" Then
eod = "True"
Else
col = col + 1
StripTags()
ConvertText()
If txt = "Name" Then
colName = col
ElseIf txt = "F" Then
colF = col
ElseIf txt = "RA" Then
colRA = col
ElseIf txt = "Dec" Then
colDec = col
ElseIf Text.StartsWith(txt,"vis.") Then
colVis = col
eod = "True"
EndIf
EndIf
EndWhile
ElseIf state = 1 Then
state = 2
EndIf
EndIf
buf = Stack.PopValue("local")
p = Stack.PopValue("local")
If len < p Or state = 2 Then
eod = "True"
Else
eod = "False"
EndIf
nStars = nStars + 1
EndWhile
outbuf = outbuf + "EndSub"
Controls.SetTextBoxText(tbox, outbuf)
Sub Form
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BrushColor = "Black"
tbox = Controls.AddMultiLineTextBox(0, 0)
Controls.SetSize(tbox, gw, gh)
EndSub
Sub StripTags
' param tag
' return txt
Stack.PushValue("local", p)
Stack.PushValue("local", len)
p = 1
len = Text.GetLength(tag)
txt = ""
While p <= len
pTag = Text.GetIndexOf(Text.GetSubTextToEnd(tag, p), "<")
If 0 < pTag Then
txt = txt + Text.GetSubText(tag, p, pTag - 1)
p = p + pTag - 1
pTag = Text.GetIndexOf(Text.GetSubTextToEnd(tag, p), ">")
p = p + pTag
Else
txt = txt + Text.GetSubTextToEnd(tag, p)
p = len + 1
EndIf
EndWhile
len = Stack.PopValue("local")
p = Stack.PopValue("local")
EndSub
Sub ConvertText
' convert &*; to unicode character
' param txt
' return txt
While Text.IsSubText(txt, "&")
c = Text.GetIndexOf(txt, "&")
l = Text.GetIndexOf(Text.GetSubTextToEnd(txt, c), ";")
kw = Text.GetSubText(txt, c + 1, l - 2)
If Text.StartsWith(kw, "#") Then
txtMid = Text.GetCharacter(Text.GetSubTextToEnd(kw, 2))
ElseIf kw = "quot" Then
txtMid = Text.GetCharacter(34)
Else
txtMid = ""
EndIf
txtLeft = Text.GetSubText(txt, 1, c - 1)
txtRight = Text.GetSubTextToEnd(txt, c + l)
txt = Text.Append(txtLeft, Text.Append(txtMid, txtRight))
EndWhile
EndSub
Sub FindTag
' find tag from html buffer
' param["tag"] - tag name
' param["class"] - class name
' param p - pointer for buffer
' param buf - html buffer
' return tag - found tag
Stack.PushValue("local", len)
pSave = p
tag = ""
findNext = "True"
While findNext
findNext = "False" ' tag may be not found
pTag = Text.GetIndexOf(Text.GetSubTextToEnd(buf, p), "<" + param["tag"])
If 0 < pTag Then
lTag = Text.GetLength(param["tag"]) + 1
pTag = p + pTag - 1
len = Text.GetIndexOf(Text.GetSubTextToEnd(buf, pTag), "/" + param["tag"] + ">")
If param["class"] = "" Then
len = len + lTag
tag = Text.GetSubText(buf, pTag, len)
findNext = "False" ' found the tag
ElseIf 0 < len Then
findNext = "True" ' tag may have different class
len = len + lTag
attr = "class=" + WQ + param["class"] + WQ
pAttr = pTag + lTag + 1
lAttr = Text.GetLength(attr)
If Text.GetSubText(buf, pAttr, lAttr) = attr Then
tag = Text.GetSubText(buf, pTag, len)
findNext = "False" ' found the tag
EndIf
EndIf
p = pTag + len
EndIf
EndWhile
If tag = "" Then
p = pSave
EndIf
len = Stack.PopValue("local")
EndSub
Sub GetAttrAndText
' get attributes and text from given tag
' param tag - given tag
' return attr[] - array of attributes in the tag
' return txt - text in the tag
Stack.PushValue("local", len)
pTag = Text.GetIndexOf(tag, " ") + 1
pEnd = Text.GetIndexOf(tag, ">")
attr = ""
While pTag <= pEnd
pEq = Text.GetIndexOf(Text.GetSubTextToEnd(tag, pTag), "=")
If 0 < pEq Then
pEq = pTag + pEq - 1
If Text.GetSubText(tag, pEq + 1, 1) = WQ Then
pWQ = Text.GetIndexOf(Text.GetSubTextToEnd(tag, pEq + 2), WQ)
If 0 < pWQ Then
pWQ = pEq + 2 + pWQ - 1
attr[Text.GetSubText(tag, pTag, pEq - pTag)] = Text.GetSubText(tag, pEq + 2, pWQ - pEq - 2)
pTag = pWQ + 2
EndIf
EndIf
Else
pTag = pEnd + 1
EndIf
EndWhile
len = Text.GetLength(tag)
txt = ""
While pTag <= len
pL = Text.GetIndexOf(Text.GetSubTextToEnd(tag, pTag), "<")
If pL = 0 Then
txt = Text.Append(txt, Text.GetSubTextToEnd(tag, pTag))
pTag = len + 1
Else
pL = pTag + pL - 1
txt = Text.Append(txt, Text.GetSubText(tag, pTag, pL - pTag))
pR = Text.GetIndexOf(Text.GetSubTextToEnd(tag, pTag), ">")
If 0 < pR Then
pTag = pTag + pR
Else
pTag = len + 1
EndIf
EndIf
EndWhile
len = Stack.PopValue("local")
EndSub
Sub InitConstellation
cons = "1=Andromeda;2=Antlia;3=Apus;4=Aquarius;5=Aquila;6=Ara;"
cons = cons + "7=Aries;8=Auriga;9=Boötes;10=Caelum;11=Camelopardalis;"
cons = cons + "12=Cancer;13=Canes Venatici;14=Canis Major;"
cons = cons + "15=Canis Minor;16=Capricornus;17=Carina;18=Cassiopeia;"
cons = cons + "19=Centaurus;20=Cepheus;21=Cetus;22=Chamaeleon;"
cons = cons + "23=Circinus;24=Columba;25=Coma Berenices;"
cons = cons + "26=Corona Australis;27=Corona Borealis;28=Corvus;"
cons = cons + "29=Crater;30=Crux;31=Cygnus;32=Delphinus;33=Dorado;"
cons = cons + "34=Draco;35=Equuleus;36=Eridanus;37=Fornax;38=Gemini;"
cons = cons + "39=Grus;40=Hercules;41=Horologium;42=Hydra;43=Hydrus;"
cons = cons + "44=Indus;45=Lacerta;46=Leo;47=Leo Minor;48=Lepus;"
cons = cons + "49=Libra;50=Lupus;51=Lynx;52=Lyra;53=Mensa;"
cons = cons + "54=Microscopium;55=Monoceros;56=Musca;57=Norma;"
cons = cons + "58=Octans;59=Ophiuchus;60=Orion;61=Pavo;62=Pegasus;"
cons = cons + "63=Perseus;64=Phoenix;65=Pictor;66=Pisces;"
cons = cons + "67=Piscis Austrinus;68=Puppis;69=Pyxis;70=Reticulum;"
cons = cons + "71=Sagitta;72=Sagittarius;73=Scorpius;74=Sculptor;"
cons = cons + "75=Scutum;76=Serpens;77=Sextans;78=Taurus;"
cons = cons + "79=Telescopium;80=Triangulum;81=Triangulum Australe;"
cons = cons + "82=Tucana;83=Ursa Major;84=Ursa Minor;85=Vela;"
cons = cons + "86=Virgo;87=Volans;88=Vulpecula;"
nCons = Array.GetItemCount(cons)
EndSub
Sub SelectConstellation
GraphicsWindow.BrushColor = "Black"
popup = Shapes.AddRectangle(gw, gh)
Shapes.SetOpacity(popup, 30)
param = ""
For i = 1 To nCons
param["caption"] = cons[i]
param["top"] = 10 + Math.Remainder(i - 1, 20) * 20
param["left"] = 10 + Math.Floor((i - 1) / 20) * 110
Controls_AddRadioButton()
radio[i] = return
EndFor
selected = radio[1]
lastSelected = selected
ok = Controls.AddButton("OK", gw - 50, gh - 40)
GraphicsWindow.MouseDown = OnMouseDown
Controls.ButtonClicked = OnButtonClicked
buttonClicked = "False"
While Not[buttonClicked]
Program.Delay(200)
EndWhile
Controls.Remove(ok)
Controls_GetSelectedRadioButton()
param["radioButton"] = return
Controls_GetRadioButtonCaption()
consName = return
param = ""
For i = 1 To nCons
param["radioButton"] = radio[i]
Controls_RemoveRadioButton()
EndFor
Shapes.Remove(popup)
EndSub
Sub OnButtonClicked
buttonClicked = "True"
EndSub
Sub OnMouseDown
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
For iRadioButton = 1 To radioNum
x0 = radioObj[iRadioButton]["x0"]
x1 = radioObj[iRadioButton]["x1"]
y0 = radioObj[iRadioButton]["y0"]
y1 = radioObj[iRadioButton]["y1"]
If x0 <= mx And mx <= x1 And y0 <= my And my <= y1 Then
If Not[radioObj[iRadioButton]["selected"]] Then
For i = 1 To radioNum
If i = iRadioButton Then
Shapes.ShowShape(radioObj[i]["mark"])
radioObj[i]["selected"] = "True"
Else
Shapes.HideShape(radioObj[i]["mark"])
radioObj[i]["selected"] = "False"
EndIf
EndFor
EndIf
EndIf
EndFor
EndSub
Sub Controls_AddRadioButton
' Controls | Add Radio Button
' param["left"] - the x co-ordinate of the radio button
' param["top"] - the y co-ordinate of the radio button
' param["caption"] - caption for the radio button
' returns return - the radio button that was just added to the Graphics Window.
radioColor = GraphicsWindow.BrushColor
radioSize = GraphicsWindow.FontSize
radioFont = GraphicsWindow.FontName
radioPW = GraphicsWindow.PenWidth
radioPC = GraphicsWindow.PenColor
radioNum = radioNum + 1
radioX0 = param["left"]
radioX1 = radioX0 + radioSize + 1
radioY0 = param["top"] + 1
radioY1 = radioY0 + radioSize + 1
GraphicsWindow.PenWidth = 1
GraphicsWindow.PenColor = "Gray"
GraphicsWindow.BrushColor = "White"
radioButton = Shapes.AddEllipse(radioSize, radioSize)
radioObj[radioNum]["button"] = radioButton
GraphicsWindow.PenColor = radioPC
Shapes.Move(radioButton, radioX0, radioY0)
radioObj[radioNum]["x0"] = radioX0
radioObj[radioNum]["x1"] = radioX1
radioObj[radioNum]["y0"] = radioY0
radioObj[radioNum]["y1"] = radioY1
GraphicsWindow.BrushColor = "CadetBlue"
GraphicsWindow.PenWidth = 0
radioMark = Shapes.AddEllipse(radioSize / 2, radioSize / 2)
GraphicsWindow.PenWidth = radioPW
Shapes.Move(radioMark, radioX0 + radioSize / 4, radioY0 + radioSize / 4)
radioObj[radioNum]["mark"] = radioMark
If radioNum = 1 Then
radioObj[radioNum]["selected"] = "True"
Else
Shapes.HideShape(radioMark)
radioObj[radioNum]["selected"] = "False"
EndIf
GraphicsWindow.FontName = radioFont
GraphicsWindow.BrushColor = radioColor
radioObj[radioNum]["text"] = param["caption"]
radioCaption = Shapes.AddText(param["caption"])
Shapes.Move(radioCaption, param["left"] + radioSize * 1.5 , param["top"])
radioObj[radioNum]["caption"] = radioCaption
return = "RadioButton" + radioNum
EndSub
Sub Controls_GetRadioButtonCaption
' Controls | Get Radio Button Caption
' param["radioButton"] - radio button to remove
' return - radio button caption
iRadioButton = Text.GetSubTextToEnd(param["radioButton"], 12)
return = radioObj[iRadioButton]["text"]
EndSub
Sub Controls_GetSelectedRadioButton
' Controls | Get Selected Radio Button
' return - selected radio button
For iRadioButton = 1 To radioNum
If radioObj[iRadioButton]["selected"] Then
return = "RadioButton" + iRadioButton
iRadioButton = radioNum ' break
EndIf
EndFor
EndSub
Sub Controls_RemoveRadioButton
' Controls | Remove Radio Button
' param["radioButton"] - radio button to remove
iRadioButton = Text.GetSubTextToEnd(param["radioButton"], 12)
Shapes.Remove(radioObj[iRadioButton]["button"])
Shapes.Remove(radioObj[iRadioButton]["mark"])
Shapes.Remove(radioObj[iRadioButton]["caption"])
EndSub