xref: /haiku/src/kits/midi/MidiSettings.cpp (revision 97dfeb96704e5dbc5bec32ad7b21379d0125e031)
1 /*
2  * Copyright 2015 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <MidiSettings.h>
7 
8 #include <File.h>
9 #include <FindDirectory.h>
10 #include <Path.h>
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
16 #define SETTINGS_FILE "midi"
17 
18 namespace BPrivate {
19 
20 status_t
21 read_midi_settings(struct midi_settings* settings)
22 {
23 	if (settings == NULL)
24 		return B_ERROR;
25 
26 	char buffer[B_FILE_NAME_LENGTH + 128];
27 	BPath path;
28 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
29 	if (status != B_OK)
30 		return status;
31 
32 	path.Append("midi");
33 	BFile file(path.Path(), B_READ_ONLY);
34 	if (file.InitCheck() != B_OK
35 			|| file.Read(buffer, sizeof(buffer)) <= 0)
36 		return B_ERROR;
37 
38 	sscanf(buffer, "# Midi Settings\n soundfont = %s\n",
39 		settings->soundfont_file);
40 
41 	return B_OK;
42 }
43 
44 
45 status_t
46 write_midi_settings(struct midi_settings settings)
47 {
48 	char buffer[B_FILE_NAME_LENGTH + 128];
49 	snprintf(buffer, sizeof(buffer), "# Midi Settings\n soundfont = %s\n",
50 			settings.soundfont_file);
51 
52 	BPath path;
53 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
54 	if (status != B_OK)
55 		return status;
56 
57 	path.Append(SETTINGS_FILE);
58 	BFile file(path.Path(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
59 
60 	size_t bufferSize = strlen(buffer);
61 	if (file.InitCheck() != B_OK
62 		|| file.Write(buffer, bufferSize) != (ssize_t)bufferSize)
63 		return B_ERROR;
64 
65 	return B_OK;
66 }
67 
68 }
69