You are on page 1of 10

Ring Documentation, Release 1.

curl_easy_setopt(curl, CURLOPT_URL, "http://ring-lang.sf.net")

cOutput = curl_easy_perform_silent(curl)

See "Output:" + nl
see cOutput

curl_easy_cleanup(curl)

49.5 Get Stock Data From Yahoo

Example:
Load "libcurl.ring"

### Part 1 --- Get Crumb and Cookie -----------------------------------------

See "Start curl_easy_init(): "+ nl


curl = curl_easy_init() ### >>> HANDLE >>> 01006BD0 CURL 0

curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1)
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt")
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt")
curl_easy_setopt(curl, CURLOPT_URL, "https://finance.yahoo.com/quote/AMZN/history")

### HTML Data >>> STDOUT Window, Use curl_easy_perform_silent >>> String

cOutput = curl_easy_perform_silent(curl) ### GO Get Data >>> String

### Extract Crumb from data


### "CrumbStore":{"crumb":"abcdefghijk"},

if cOutput != NULL

newStr1 = substr(cOutput, substr(cOutput, '"CrumbStore":{"crumb":"' ), 48 )


nPosS = substr(newStr1, ':"' ) ; ### Start of crumb -2
nPosE = substr(newStr1, '"}' ) ; ### End of crumb
nCount = nPosE - nPosS -2 ### size of crumb
myCrumb = substr(newStr1, nPosS +2, nCount)

See "myCrumb.: |"+ myCrumb +"|" +nl

### UniCode "\u002F" replace it with "/"


if substr( myCrumb, "\u002F")
myCrumb = substr( myCrumb, "\u002F", "/")
See "myCrumb2: |"+ myCrumb +"|"+ nl
ok

else
See "No Connectivity to Yahoo. Looking for Cookie and Crumb." +nl +nl
ok

### Part 2 --- Send URL with Crumb, and Cookie -----------------------------------------

49.5. Get Stock Data From Yahoo 443


Ring Documentation, Release 1.6

### Send URL+Crumb to Yahoo to fetch 1st stock history data,

$url = "https://query1.finance.yahoo.com/v7/finance/download/AMZN?period1=1277856000&period2=

curl_easy_setopt(curl, CURLOPT_URL, $url);


cStr = curl_easy_perform_silent(curl)
See cStr

curl_easy_cleanup(curl) ### REMEMBER to CLOSE CURL

Output:
myCrumb.: |sEEeW97mxvN|
Date,Open,High,Low,Close,Adj Close,Volume
2010-07-05,110.650002,117.480003,109.000000,117.260002,117.260002,21000400
2010-07-12,117.809998,124.879997,117.320000,118.489998,118.489998,29407300
2010-07-19,118.379997,121.250000,105.800003,118.870003,118.870003,74252100

49.5. Get Stock Data From Yahoo 444


CHAPTER

FIFTY

USING RINGZIP

In this chapter we will learn about using RingZip

50.1 Create Zip File

Example : Create myfile.zip contains 4 files


load "ziplib.ring"
oZip = zip_openfile("myfile.zip",'w')
zip_addfile(oZip,"test.c")
zip_addfile(oZip,"zip.c")
zip_addfile(oZip,"zip.h")
zip_addfile(oZip,"miniz.h")
zip_close(oZip)

50.2 Extract Zip File

Example : Extract myfile.zip to myfolder folder.


load "ziplib.ring"
zip_extract_allfiles("myfile.zip","myfolder")

50.3 Print Files in Zip file

Example : Print file names in the myfile.zip


load "ziplib.ring"
oZip = zip_openfile("myfile.zip",'r')
for x=1 to zip_filescount(oZip)
see zip_getfilenamebyindex(oZip,x) + nl
next
zip_close(oZip)

50.4 Using RingZip Classes

The RingZip library comes with two classes. The Zip class and the ZipEntry class.

445
Ring Documentation, Release 1.6

Example (1):
load "ziplib.ring"

new Zip {
setFileName("myfile.zip")
open("w")
newEntry() {
open("test.c")
writefile("test.c")
close()
}
close()
}

Example (2):
load "ziplib.ring"

new Zip {
SetFileName("myfile.zip")
Open("w")
AddFile("test.c")
AddFile("zip.c")
AddFile("zip.h")
AddFile("miniz.h")
Close()
}

Example (3):
load "ziplib.ring"

new zip {
SetFileName("myfile.zip")
ExtractAllFiles("myfolder")
}

Example (4):
load "ziplib.ring"

new Zip {
SetFileName("myfile.zip")
Open("r")
see FilesCount()
Close()
}

Example (5):
load "ziplib.ring"

new Zip {
SetFileName("myfile.zip")
Open("r")
for x = 1 to filescount()
See GetFileNameByIndex(x) + nl
next
Close()

50.4. Using RingZip Classes 446


Ring Documentation, Release 1.6

50.5 Zip Class Reference

Methods:
Method Description/Output
SetFileName(cName) Set the Zip file name
GetFileName() Return the Zip file name
Open(cMode) Open File, cMode = “a”, “w” or “r”
Close() Close the Zip File
AddFile(cFileName) Add file to the Zip file
ExtractAllFiles(cFolder) Extract all files from the Zip file
FilesCount() Return files count in the Zip file
GetFileNameByIndex(nIndex) Return file name in the Zip file by file index
NewEntry() Create new ZipEntry object

50.6 ZipEntry Class Reference

Methods:
Method Description/Output
Open(cFileName) Open new Entry
WriteFile(cFileName) Write File to the Entry
WriteString(cString) Write String to the Entry
Close() Close the Entry

50.5. Zip Class Reference 447


CHAPTER

FIFTYONE

GRAPHICS AND 2D GAMES PROGRAMMING USING RINGALLEGRO

In this chapter we will learn how to use the allegro game programming library in our Ring applications.
We have the file gamelib.ring that load the DLL library that contains wrappers for the Allegro functions
Load "allegro.rh"
if iswindows()
LoadLib("ring_allegro.dll")
but ismacosx()
LoadLib("libringallegro.dylib")
else
LoadLib("libringallegro.so")
ok

The file gamelib.ring uses the Load instruction to execute the file allegro.rh which is a ring source code file con-
tains constants to be used in our programs. Then using the function LoadLib() we can load the DLL library
“ring_allegro.dll”.
To write portable code we can change the gamelib.ring to check the platform before loading the DLL/So file.

51.1 Drawing, Animation and Input

The next example uses the Allegro library for drawing, moving objects on the screen and getting input from the
keyboard and the mouse.
Load "gamelib.ring"

al_init()
al_init_image_addon()

display = al_create_display(640,480)

al_show_native_message_box(display, "Hello", "Welcome",


"Using Allegro from the Ring programming language",
"", 0);

al_clear_to_color(al_map_rgb(0,0,255))

BOUNCER_SIZE = 40
bouncer_x = 10
bouncer_y = 20
bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE)
al_set_target_bitmap(bouncer)
al_clear_to_color(al_map_rgb(255,0,255))

448
Ring Documentation, Release 1.6

for x = 1 to 30
bouncer_x += x
bouncer_y += x
al_set_target_bitmap(al_get_backbuffer(display))
al_clear_to_color(al_map_rgb(0,0,0))
al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0)
al_draw_bitmap(bouncer, 200+bouncer_x,200+ bouncer_y, 0)
al_flip_display()
al_rest(0.1)
next

al_clear_to_color(al_map_rgb(255,255,255))
image = al_load_bitmap("man2.jpg")
al_draw_bitmap(image,200,200,0)
al_flip_display()
al_rest(2)

event_queue = al_create_event_queue()
al_register_event_source(event_queue, al_get_display_event_source(display))

ev = al_new_allegro_event()
timeout = al_new_allegro_timeout()
al_init_timeout(timeout, 0.06)

FPS = 60
timer = al_create_timer(1.0 / FPS)
al_register_event_source(event_queue, al_get_timer_event_source(timer))
al_start_timer(timer)
redraw = true

SCREEN_W = 640
SCREEN_H = 480
BOUNCER_SIZE = 32
bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0
bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0
bouncer_dx = -4.0
bouncer_dy = 4.0

al_install_mouse()
al_register_event_source(event_queue, al_get_mouse_event_source())

al_install_keyboard()
al_register_event_source(event_queue, al_get_keyboard_event_source())

KEY_UP = 1
KEY_DOWN = 2
KEY_LEFT = 3
KEY_RIGHT = 4
Key = [false,false,false,false]

while true
al_wait_for_event_until(event_queue, ev, timeout)
switch al_get_allegro_event_type(ev)
on ALLEGRO_EVENT_DISPLAY_CLOSE
exit
on ALLEGRO_EVENT_TIMER

# Animation

51.1. Drawing, Animation and Input 449


Ring Documentation, Release 1.6

if bouncer_x < 0 or bouncer_x > SCREEN_W - BOUNCER_SIZE


bouncer_dx = -bouncer_dx
ok

if bouncer_y < 0 or bouncer_y > SCREEN_H - BOUNCER_SIZE


bouncer_dy = -bouncer_dy
ok

bouncer_x += bouncer_dx
bouncer_y += bouncer_dy

# Keyboard
if key[KEY_UP] and bouncer_y >= 4.0
bouncer_y -= 4.0
ok
if key[KEY_DOWN] and bouncer_y <= SCREEN_H - BOUNCER_SIZE - 4.0
bouncer_y += 4.0
ok
if key[KEY_LEFT] and bouncer_x >= 4.0
bouncer_x -= 4.0
ok
if key[KEY_RIGHT] and bouncer_x <= SCREEN_W - BOUNCER_SIZE - 4.0
bouncer_x += 4.0
ok

redraw = true

on ALLEGRO_EVENT_MOUSE_AXES
bouncer_x = al_get_allegro_event_mouse_x(ev)
bouncer_y = al_get_allegro_event_mouse_y(ev)
on ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY
bouncer_x = al_get_allegro_event_mouse_x(ev)
bouncer_y = al_get_allegro_event_mouse_y(ev)
on ALLEGRO_EVENT_MOUSE_BUTTON_UP
exit
on ALLEGRO_EVENT_KEY_DOWN
switch al_get_allegro_event_keyboard_keycode(ev)
on ALLEGRO_KEY_UP
key[KEY_UP] = true
on ALLEGRO_KEY_DOWN
key[KEY_DOWN] = true
on ALLEGRO_KEY_LEFT
key[KEY_LEFT] = true
on ALLEGRO_KEY_RIGHT
key[KEY_RIGHT] = true
off
on ALLEGRO_EVENT_KEY_UP
switch al_get_allegro_event_keyboard_keycode(ev)
on ALLEGRO_KEY_UP
key[KEY_UP] = false
on ALLEGRO_KEY_DOWN
key[KEY_DOWN] = false
on ALLEGRO_KEY_LEFT
key[KEY_LEFT] = false
on ALLEGRO_KEY_RIGHT
key[KEY_RIGHT] = false
on ALLEGRO_KEY_ESCAPE
exit

51.1. Drawing, Animation and Input 450


Ring Documentation, Release 1.6

off
off
if redraw and al_is_event_queue_empty(event_queue)
redraw = false
al_clear_to_color(al_map_rgb(0,0,0))
al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0)
al_flip_display()
ok
callgc()
end

al_destroy_timer(timer)
al_destroy_allegro_event(ev)
al_destroy_allegro_timeout(timeout)
al_destroy_event_queue(event_queue)
al_destroy_bitmap(bouncer)
al_destroy_bitmap(image)
al_destroy_display(display)

Note: In the previous example we used the function callgc() which is a Ring function to force calling the Garbage
collector inside the While/End loop.

Program Output:
At first the program display a messagebox

51.1. Drawing, Animation and Input 451


Ring Documentation, Release 1.6

Then we see two rectangles are moving on the screen

51.1. Drawing, Animation and Input 452

You might also like