John.Wiley.and.Sons.Rapid.Mobile.Enterprise.Development.for.Symbian.OS.An.Introduction.to.OPL.Application.Design.and.Programming.May.2005.eBook-LinG (779881), страница 6
Текст из файла (страница 6)
What happens if we want to come out of the loop in the middleof the loop code? We can use the BREAK command.When your program reaches a BREAK command, it will jump to thefirst line of code after the end of the loop (either the UNTIL or ENDWHcommand). BREAK statements are usually inside an IF statement, as youshouldn’t be using the BREAK statement very often – it’s mentioned herefor completeness only.1.3.7 Decisions, DecisionsIf. .
.Else. . .EndifThe IF...ELSE...ENDIF construct is the third great building block ofcomputing languages.At many points in your program, you’ll have to check something andthen execute different commands depending on the outcome. To checkthings you use the IF command.IF Guess%=1GuessRight:ELSEIF Guess%=2GuessClose:ELSEGuessWrong:ENDIFSUMMARY17Hopefully, you should by now be able to make a good guess (no punintended!) as to what this code is attempting to do.If the variable Guess% equals 1, then go to a procedure calledGuessRight:.
If Guess% equals 2, then go to a procedure calledGuessClose: If Guess% is anything else, then go to a procedure calledGuessWrong:.Note that one of these options has to be chosen. Using ELSE on itsown means that if all the other tests have failed, then do this. It’s a safetynet in some cases, as the program must go somewhere. The last resorthere is to call GuessWrong:.1.4 SummaryThis chapter started by assuming you knew nothing about the innerworkings of a computer. We’ve taken you through the basic principles,both of how a computer works and how different languages came intobeing. We looked at their strengths and weaknesses, and why certainlanguages are a better choice than others.Finally, we looked at the main building blocks of the OPL language:• Using constants to make code easier to read• Storing information using variables– small numbers (short integers)– large numbers (long integers)– numbers with decimal points (floats)– textual information (strings)• Using arrays for large collections of variables• Repeating sections of code by using loops– the DO...UNTIL loop– the WHILE...ENDWH loop• Making decisions– IF...ELSEIF...ENDIF2Introducing the Tools of OPLIn this chapter you will learn:• the files and file extensions that you will need to be familiar with toprogram in OPL• the tools that are available to help you program in OPL, where to findthem, and where to get help in installing them• the process of creating an OPL program, what tools are required ateach stage, and how these tools are used• taking these ideas and creating your very first OPL program!2.1 Parts of OPLLike any computer language, OPL is made up of many different parts thatmust all come together.
We’ve looked at the basic syntax and structure ofthe OPL language in the previous chapter, and here we’ll look at physicalorganization of your code during development. This means file namesand extensions, what they are for, and how they relate to each other.Some of these may be familiar if you have already developed with otherlanguages on Symbian OS.2.1.1 Source Code (.tpl or .opl)Source code is what you will regard as your program. It is a plain text fileof words, numbers, and symbols that you will work on and edit in yourtext editor. This text is then read by a compiler (or, in the case of OPL, a‘translator’) – a program that takes this easily readable code and turns itinto something that a computer can understand and relate to.Writing computer code, even in a high-level language such as OPL,requires you to be precise in your language, and all the commands andstatements must be typed in exactly. The OPL translator will check thatwhat you have typed in follows these rules.20INTRODUCING THE TOOLS OF OPLIt is also possible to write source code directly on your Symbian OSphone using the Program application (if available).
While this is somethingthat is comfortable to do on the Communicator devices entering morethan a few lines of code on a Series 60 or UIQ device will take somededication. Using a bluetooth keyboard such as the Nokia SU-8 W or theThinkOutside Bluetooth stowaway keyboard can be a great help here.Source code written through the Program application on the phone willnot be directly readable on your PC text editor, as there are a few SymbianOS flags and unique identifying numbers embedded in the source codefile because it will be saved as a ‘native’ Symbian OS document.If you enter source code on your phone, it is recommended you giveit the extension .opl. Whereas, the .tpl extension should be used on a PC(the ‘t’ signifies a pure text file, as opposed to one with Symbian OS flagsand UID numbers).If you wish to move source code between your Symbian OS phone andyour PC, you should use the Import text and Export text optionsthat are present in the Program application.2.1.2 Header Files (.tph or .oph)A header file is used to make reading your OPL code easier.
For example,as discussed in the previous chapter, if you use the number 99 constantlyin your program to represent someone’s house number, your header filecan have a line reading something like:CONST KHouseNumber%=99Now, in your code when you want to use 99, you simply write KHouseNumber%.
This makes code a lot easier to read, and also means that ifyou need to change the house number, you only have to change one linein the header file, as opposed to every occurrence of 99 in the sourcecode. Again, the .oph extension applies to headers saved in Symbian OSformat on your phone, with .tph reserved for the plain text versions savedon the PC.The main const.oph is a standard header file and can be included inany OPL program as it is part of the standard package.2.1.3Compiled Code (.opo)Once your source code has been passed through the OPL translator, thiscode is output to another file.
It is now called ‘object code’, to distinguishit from the raw source code. Unless otherwise specified (see below), yourobject code file will have the same filename as the source code file,but a different extension. This extension is .opo. So MyCode.tpl wouldtranslate into MyCode.opo. The .opo file produced will be the sameORGANIZING YOUR PROJECTS21regardless of where it is produced (i.e. either by translating your code onthe phone or on the PC).Once sent to your phone, this .opo file can be selected and run. Youcan do this through the built-in file manager on Communicators or usinga third-party file manager on the UIQ and Series 60 platform – see belowfor some unsupported suggestions.2.1.4Complied Code (.app and .aif)You can also choose to translate your source code so it appears as anapplication, with an icon on the main launcher screen (System screen)of your Symbian OS phone.
This is done by adding the APP...ENDAcommands at the start of your source code. When you translate yoursource code into an .app, an Application Information File (.aif) is alsocreated. This is used by the smartphone to recognize an application anddisplay it on the main System screen, as an icon which can be launched.OPL applications are covered in more detail in Chapter 8.2.1.5Graphics File (.mbm)What is a computer program without pictures, icons, and graphics tolook at? The Symbian standard graphics file is the .mbm, which standsfor Multiple BitMap, and it is just that. It allows you to hold a ‘book’ ofimages and use them in your OPL program. They are created on your PCfrom standard Windows bitmaps. Graphics handling and .mbm files arecovered in more detail in Chapter 6.2.1.6 Installation File (.sis)One of the advantages of Symbian OS is that it is incredibly easy forend-users to install applications.
All the application files are packaged upin a single distribution file with the extension .sis. This file can be runon your PC, and the PC Suite will extract the application files and placethem in the correct locations on the Symbian OS phone.You can also select .sis files that have been downloaded onto theSymbian OS phone, and the installer will extract and place the fileswhere they are required.If you have created your OPL program to be an application, then .sisfiles are the expected distribution method, and you should not providethe files separately.
The creation of .sis files is covered in more detail inChapter 8.2.2 Organizing your ProjectsYour OPL program can be built up of many small files that you will needto keep track of. The system I use is to have a root folder on my PC called22INTRODUCING THE TOOLS OF OPLOPL, and then give each program ‘project’ its own name. Inside that Ihave five folders containing the required files. I’ll be using this structurethroughout this book, but it is not the only way to do it – if you find away that works for you, then go for it.Root:C:\OPL\<project name>\Source\\Source\Archive\\Compiled\\Graphics\\SIS\• Source folderUsed to store the text format source code files.
The Archive subfolderholds previous versions of the code so I can look back if I deletesomething or introduce errors.• CompiledMove any compiled .opo files here for long-term storage.• GraphicsHolds the bitmaps and the resulting .mbm files.• SISAny file that needs to be put in the installation .sis file is moved here.This folder will also contain the script text file that will be needed toassemble the .sis.2.3 Gathering ToolsTo develop in OPL, you will need to have the relevant softwaretools. These are all available over the Internet (some of them areon the accompanying website www.symbian.com/books/rmed/rmedinfo.html ).