attribute ...

Prev Next

Function Names

attribute read, attribute write, attribute modify, attribute delete, attribute remove, attribute listing

Description

The functions perform the following:

attribute read Reads a resident attribute from a file
attribute write Writes attribute. If file is missing, then new file (incl. directory path) is created. If attribute is missing, then new attribute name will be added.
attribute modify Updates attribute if file is existing and the attribute name is also available. Returns false otherwise.
attribute delete Deletes the attribute entry. Will refuse if sub-attributes still exist.
attribute remove Deletes the attribute entry as well as sub-attributes.
attribute listing Lists all attribute names

Note: No exceptions are assert in following cases: Directory not found, file not found, attribute name not found, cannot delete attribute name because nested attributes exist and must be deleted first.

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled

Parameter count

attribute delete: 3; all other functions: 4

Parameters

No.TypeDescription
1
input
string Path name

Directory path to file where the attributes are stored.

2
input
string File name

File name where the attributes are stored.

3
input
set or string Attribute name(s)

A hierarchical structure of attribute names is supported by putting the subsequent names into one set. Empty set is not allowed. Except for attribute listing, at least one attribute name must be provided.

  • Use a string to specify one value
  • Use set to specify multiple values
  • No values can be provided by using an empty set {}

4
output
valid types Attribute value

Applicable to attribute read and attribute listing: Attribute value is written into specified destination (variable or table entry).

Alt. 4
input
valid types Attribute value

Applicable to attribute write and attribute modify: Input parameter providing the value to be written.

Return value

TypeDescription
boolean Result

true if successful, otherwise false.

Exceptions

Invalid path and/or file name (e.g. invalid symbols inside)
File locked

Examples


       my file[] = "My attributes.json";
       attribute write ( "", my file[], { Name, First Name }, Nic );  // Use 'write' in 1st call so it creates the JSON file
       attribute write ( "", my file[], { Name, Middle Initial }, M. );
       attribute modify( "", my file[], { Name, Middle Initial }, N. );
       attribute write ( "", my file[], { Name, Last Name },  Nicholas );
       attribute write ( "", my file[], { Name, Graduated at MIT }, true  );
       attribute write ( "", my file[], { Name, Birth Date },  date( "2000-02-03")  );

       attribute listing( "", my file[], Name, listing[] );
       echo("List all attribute names behind 'Name': ", listing[] );

       for all( listing[], attribute name[] )
       {
               attribute read( "", my file[], {Name, attribute name[]}, attribute value[] );
               echo("    ", attribute name[], ": ", attribute value[] );
       }

       echo(new line, "Take a quick look at the JSON file:" );
       table load( t, my file[], TEXT );
       table list (t);

       echo("Try deleting some items");

       res[1] = attribute delete( "", my file[], {Name, Birth Date });
       res[2] = attribute delete( "", my file[], {Name, Graduated at Harvard } );
       res[3] = attribute delete( "", my file[],  Name );

       echo("Success: Delete birth date: ", res[1],";  Harvard U.: ", res[2], ";  Name: ", res[3] );

       for all( listing[], attribute name[] )
       {
               attribute read( "", my file[], {Name, attribute name[]}, attribute value[] );
               echo("    ", attribute name[], ": ", attribute value[] );
       }

       echo("Try removing the Name");

       attribute remove( "", my file[], Name );

       echo(new line, "Take a quick look at the empty JSON file:" );
       table load( t, my file[], TEXT );
       table list (t);
       file delete( my file[] );

Output

List all attribute names behind 'Name': {'Birth Date','First Name','Graduated at MIT','Last Name','Middle Initial'}
    Birth Date: 2000-02-03
    First Name: Nic
    Graduated at MIT: true
    Last Name: Nicholas
    Middle Initial: N.

Take a quick look at the JSON file:
    0 : {                               
    1 :   "Name" : {                    
    2 :     "Birth Date" : "2000-02-03",
    3 :     "First Name" : "Nic",       
    4 :     "Graduated at MIT" : true,  
    5 :     "Last Name" : "Nicholas",   
    6 :     "Middle Initial" : "N."     
    7 :   }                             
    8 : }                               

Try deleting some items
Success: Delete birth date: true;  Harvard U.: false;  Name: false
    Birth Date:
    First Name: Nic
    Graduated at MIT: true
    Last Name: Nicholas
    Middle Initial: N.
Try removing the Name

Take a quick look at the empty JSON file:
    0 : []

Try it yourself: Open LIB_Function_attribute_read.b4p in B4P_Examples.zip. Decompress before use.

See also

variable load
variable save