Reading variable-size preferences, Listing 2-9, Reading variable-size preferences 2 – Apple Network Setup User Manual

Page 37

Advertising
background image

C H A P T E R 2

Using Network Setup

Reading and Writing Preferences

37

IMPORTANT

You can derive the C structure for a specific preference type
by removing the “k” from the front of the name and the
“Pref” from the end. For example, the C structure for

kOTCfgTCPDHCPLeaseInfoPref

is

OTCfgTCPDHCPLeaseInfo

. The

preference type constants and preference structures for all
of the Apple-defined preferences are provided in Chapter 4,
“Network Setup Protocol Structures and Data Types.”

Reading Variable-size Preferences

2

The

MyReadFixedSizePref

routine shown in Listing 2-7 also works with variable

size preferences that have a known maximum size that internally includes the
size of the preference. The user-visible name preference
(

kOTCfgUserVisibleNamePref

), which contains a packed Pascal string, is an

example. The maximum length of a Pascal string is 256 bytes, and the first byte
denotes the length of the actual string data. Listing 2-9 shows how to use

MyReadFixedSizePref

to read this type of variable size preference.

Listing 2-9

Reading the user-visible name preference

static OSStatus MyGetEntityUserVisibleName(CfgDatabaseRef dbRef,

const CfgEntityRef *entity,

Str255 name)

{

OSStatus err;

assert(dbRef != nil);

assert(entity != nil);

assert(name != nil);

err = MyReadFixedSizePref(dbRef, entity, kOTCfgUserVisibleNamePref,

name, sizeof(Str255));

return err;

}

If the variable size preference you want to read does not have a known
maximum size and does not store its size internally, you need to know how big
a buffer to allocate before you call

OTCfgGetPrefs

. You can get this information

Advertising