NASutils Knowledge BaseUnofficialPolski

Pascal in 30 lessons

The lessons use Free Pascal. fpc compiles your file into one program that starts by itself and needs no other file.

Start: Compile a program

Programming on the NAS

The 30 lessons

Chapter 1

First steps

Print text, keep values in variables, calculate and read what the user types.

  1. 1Compile a programfpc compiles the source file into a program.
  2. 2Write in columnsWrite prints and stays on the line; WriteLn ends it.
  3. 3Declare variablesvar lists each name with its type: string, Integer, Real or Boolean.
  4. 4Count with numbersdiv and mod work on whole numbers, / gives a fraction.
  5. 5Work with textLength, Copy, Pos and the tools of the unit SysUtils work on text.
  6. 6Ask the userReadLn reads a line of text, or several numbers on one line.

Chapter 2

Decisions and loops

Use conditions to choose what to do and loops to repeat it.

  1. 7Decide with ifif, then and else choose a block; begin and end group several statements.
  2. 8Join conditionsand, or and xor join Boolean values, not turns one round.
  3. 9Choose with casecase compares one value with letters, lists and ranges.
  4. 10Repeat with loopsfor counts up or down, while tests first, repeat tests last.
  5. 11Check what the user typedTryStrToInt reads a whole number from a text and says whether that worked.
  6. 12A loop in a loopThe inner loop prints one row, the outer loop starts the next one.

Chapter 3

Data, functions and the command line

Keep many values together, write functions and read the command line.

  1. 13Keep many valuesLow and High give the first and the last index of an array, Length the number of its elements.
  2. 14Let an array growSetLength changes the size while the program runs, for ... in visits every item.
  3. 15Name a block of linesA const parameter can only be read.
  4. 16Give a value backA function sets Result.
  5. 17Join fields into a recordA record keeps the slot, model and temperature of one disk together.
  6. 18Use your own valuesAn enumeration names the days, a set holds some of them, in asks whether a day is there.

Chapter 4

Files and errors

Read and write files and handle errors such as a missing file.

  1. 19Write and read a fileAssignFile links a variable with a file name.
  2. 20Handle an errortry ... except catches an error, try ... finally always runs the last part.
  3. 21Read the command lineParamCount gives the number of arguments and ParamStr(1) the first one.
  4. 22Split a program into unitsA unit shows its functions in the interface and keeps the code in the implementation.
  5. 23Build a calculatorPut Split, TryStrToFloat and case into one program.
  6. 24Keep lines in a listTStringList sorts, searches and reads name=value pairs.

Chapter 5

Tools for the NAS

Programs that read the state of the NAS and report it.

  1. 25Write a classA class joins data with the methods that may change it; a constructor makes an object and Free ends it.
  2. 26Copy a file with a dateFormatDateTime makes the name and TFileStream copies the file.
  3. 27Walk through foldersFindFirst and FindNext read a folder, and the procedure calls itself for each subfolder.
  4. 28Ask the system for free spacefpStatFS asks the system about the volume of a folder: how many blocks it has and how many are free.
  5. 29Build a notepadA TStringList loads and saves the notes; case chooses add, list or delete.
  6. 30Finish a toolTIniFile reads the limit, RunCommand calls grep and the program saves the list as report.txt.

Projects

Longer programs that draw a menu, play a game, greet you at sign-in or show the state of the NAS. Each page shows the screen as the program drew it on a test NAS.

Write it

Sign in to the NAS over SSH and type these commands. micro opens an empty file; Ctrl+S saves it and Ctrl+Q closes it.

mkdir -p /share/Public/lessons
cd /share/Public/lessons
micro 29-notepad.pas

Run it

fpc makes the program 29-notepad next to the source. The words after its name go to the program.

fpc 29-notepad.pas
./29-notepad add "Buy two disks"
./29-notepad list

The programs to download