c programming

profileQ87h
assignment_-_2014_1.docx

6

Task 1

The first thing the program is required to do is load in the household configuration file, with the following filename:

housecfg.dat

If the file is not found, or there are any errors in reading the file data, the program should report this and then terminate.

The data contained in the file should be loaded into an array of structures. The array should be declared in the main function, although the file load should have a function of its own. Note that it is recommended to use dynamic memory allocation for this task, as static memory allocation will result in a loss of marks.

You may assume the file will follow the following format:

[Number of household devices]

[Unique Device ID]

[Device Name]

[Device Location]

[Device State]

[Unique Device ID]

[Device Name]

[Device Location]

[Device State]

The … represents repeated intermediate devices.

The fields are described as follows:

[Number of household devices] – This is the total number of devices stored in the file

[Unique Device ID] – This is an integer identifier for the current device, useful for storing specific information about the device’s state.

[Device Name] – This is a string specifying the name of the device. For example, this may be “TV”.

[Device Location] – This is a string specifying the room of the house that the device is located in. For example, “Lounge” or “Dining”.

[Device State] – This is an integer specifying the current state of the device. Note that for this assignment, we will only use two states; on and off. The on state is represented by a 1. The off state is represented by a 0.

Below is an example of a household configuration file:

2

0

TV

Lounge

0

1

Computer

Study

1

N.B. If the sample file is copied from windows into putty, the file newline formatting may be incorrect. Use the following command to change the format of the file from windows to Unix

dos2unix <filename>

Task 2

Once the house configuration file is successfully loaded, the program should present the user with the following menu:

1. Display All House Devices

2. Change State of a Device

3. Save Current Home State as New Profile

4. Load a Profile

5. Exit

Once the selected option is completed (excluding option 5), the menu should be displayed to the user again, so that the user can select further options. Each option corresponds to a task described in greater detail below.

Task 3

Implement the Display All House Devices menuoption. The program should display the total number of devices, followed by a list of all devices in the house (in any order) as well as all their attributes. The format of each item should be as follows:

Number of Devices in House: [Number of household devices]

---------------------------------------------------------

ID: [Unique Device ID]

Name: [Device Name]

Room: [Device Location]

State: [Device State]

---------------------------------------------------------

---------------------------------------------------------

ID: [Unique Device ID]

Name: [Device Name]

Room: [Device Location]

State: [Device State]

---------------------------------------------------------

Where [Unique Device ID], [Device Name], [Device Location], and [Device State] have the same meaning as described in Task 1.

An example output is shown below:

Number of Devices in House: 2

---------------------------------------------------------

ID: 0

Name: TV

Room: Lounge

State: 0

---------------------------------------------------------

ID: 1

Name: Computer

Room: Study

State: 1

---------------------------------------------------------

Task 4

Implement the Change State of Device menu option. The program should ask the user to type in an integer corresponding to a Device ID. The program should then find the device with a matching ID and, if one is found, prompt the user to enter a new state for the device (either a 0 or a 1). The matching device is then updated accordingly to the new state value.

An example of the program execution is shown below:

Enter a Device ID: 0

Enter a new state for the device (0,1): 1

Alternatively, if the device is not found, the program would execute as follows:

Enter a Device ID: 123456789

Error! Device does not exist!

Task 5

Implement the Save Current Home State as New Profile menu option. The program should ask the user for an output filename to save to, and then write the current device information to file. You may format this data however you wish, so long as the Load a Profile menu option described in Task 6 works with your chosen format.

For maximum marks, it is recommended to write this data in the most efficient way possible. This may be as a plaintext file or as a binary file, and you may choose whichever device attributes to write to the file as you deem necessary.

Task 6

Implement the Load a Profile menu option. Recall that this MUST be compatible with the method in which you saved a profile in Task 5. The program should ask the user for the name of a file to open, and should hence update the current device collection to the state saved in the profile.

Note that you may assume every device in the current collection is represented in the profile data file. Therefore, you may simply overwrite the entire device collection in memory with that stored in the profile data file. However, this solution is not guaranteed to yield maximum marks unless the correct output format is chosen in Task 5.

Task 7

Implement the Exit menu option. The program should save the current household device collection to file by overwriting the original file

housecfg.dat

Note that the output format should be the same as the initial input format, so that this file can be re-loaded again on successive launches of the HEMS program.

Once the data has been saved to file, the program should clear any memory necessary and, and exit appropriately.

Bonus Task 1

The current Device State property uses a Boolean system to represent if the device is on or off. Modify this (including the ChangeState of a Devicefunction) so that the state can include a range of other options. For example, a TV may be turned on and on a particular channel, or a light may be dimmed to 60% brightness.

Because a device may be in two or more specific states at one time (e.g. a TV is simultaneously in the on state, the Digital TV state, and the Channel 9 state while watching channel 9 from a digital input), we wish to be able to represent many possible states for each device.

Note that this should not be accomplished by introducing multiple state variables in the device struct. It is recommended to find an alternative way of representing this information using bit flags. That is, use each bit in the integer to represent the condition of a ‘state’.

You may assume that the user knows which bit corresponds to the different possible states of a device. The prompt for changing the state of a device should ask for an integer index between 1 and 32 (inclusive), and the bit at the index specified should be toggled to the opposite of its current state.

E.g.

Initial state of device:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

32nd bit 1st bit

Program prompt and user input:

Please enter the index of the state to be toggled: 7

Final state of device:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0

32nd bit 1st bit

Bonus Task 2

Extend the Display All House Devices menu option to allow for displaying the devices grouped by different categories. Now when the user selects this menu option, the program should display a further submenu to the user as shows below:

1. Display All Devices

2. Display Devices Grouped by Room

3. Display Devices Grouped by Name

Where Display All Devices used the same existing functionality from Task 3.

Display Devices Grouped by Room will search the collection of devices and, for each unique room attribute, display all of the devices contained in that room. For example, all devices in the Lounge would be grouped together. An example output of this is shown below:

Location: [Device Location]

---------------------------------------------------------

ID: [Unique Device ID]

Name: [Device Name]

State: [Device State]

------------------------

------------------------

ID: [Unique Device ID]

Name: [Device Name]

State: [Device State]

---------------------------------------------------------

Location: [Device Location]

---------------------------------------------------------

---------------------------------------------------------

Display Devices Grouped by Name will search the collection of devices and, for each unique device name, display all of the devices that share that name. For example, all TVs would be grouped together. An example output of this is shown below:

Device Name: [Device Name]

---------------------------------------------------------

ID: [Unique Device ID]

Room: [Device Location]

State: [Device State]

------------------------

------------------------

ID: [Unique Device ID]

Room: [Device Location]

State: [Device State]

---------------------------------------------------------

Device Name: [Device Name]

---------------------------------------------------------

---------------------------------------------------------

Bonus Task 3

Add an additional menu option, Remove Device from Household, that prompts the user for a Device ID and deletes the corresponding device (if any) from the household device data. In particular, ensure that the memory used by the removed device is cleared or freed as necessary, and that doing so does not negatively affect the other functions of the HEMS program (specifically, ensure that the Exit function writes the modified device set to file correctly).

Note that you do not need to be concerned about loading old Profiles that may contain devices that have been deleted. We will assume that when a device is deleted, all existing profiles for the house are now out of date, and will no longer be used by the system.

An example of the program execution is shown below:

Enter a Device ID: 1

The Device “Computer” was removed from the household.

Alternatively, if the device is not found, the program would execute as follows:

Enter a Device ID: 123456789

Error! Device does not exist!