2 * Copyright 2003-2004 Waldemar Kornewald. All rights reserved.
3 * Copyright 2017 Haiku, Inc. All rights reserved.
4 * Distributed under the terms of the MIT License.
8 #include "MessageDriverSettingsUtils.h"
11 #include <settings_tools.h>
19 static bool AddParameters(const BMessage
& message
, driver_settings
*to
);
23 FindMessageParameter(const char *name
, const BMessage
& message
, BMessage
*save
,
26 // XXX: this should be removed when we can replace BMessage with something better
28 int32 index
= startIndex
? *startIndex
: 0;
29 for(; message
.FindMessage(MDSU_PARAMETERS
, index
, save
) == B_OK
; index
++) {
30 if(save
->FindString(MDSU_NAME
, &string
) == B_OK
31 && string
.ICompare(name
) == 0) {
44 AddValues(const BMessage
& message
, driver_parameter
*parameter
)
47 for(int32 index
= 0; message
.FindString(MDSU_VALUES
, index
, &value
) == B_OK
;
49 if(!add_driver_parameter_value(value
, parameter
))
58 AddParameters(const BMessage
& message
, driver_parameter
*to
)
63 return AddParameters(message
,
64 reinterpret_cast<driver_settings
*>(&to
->parameter_count
));
70 AddParameters(const BMessage
& message
, driver_settings
*to
)
74 driver_parameter
*parameter
;
75 for(int32 index
= 0; message
.FindMessage(MDSU_PARAMETERS
, index
,
76 ¤t
) == B_OK
; index
++) {
77 name
= current
.FindString(MDSU_NAME
);
78 parameter
= new_driver_parameter(name
);
79 if(!AddValues(current
, parameter
))
82 AddParameters(current
, parameter
);
83 add_driver_parameter(parameter
, to
);
91 MessageToDriverSettings(const BMessage
& message
)
93 driver_settings
*settings
= new_driver_settings();
95 if(!AddParameters(message
, settings
)) {
96 free_driver_settings(settings
);
106 AddParameter(const driver_parameter
*parameter
, BMessage
*message
)
108 if(!parameter
|| !message
)
112 message
->AddString(MDSU_NAME
, parameter
->name
);
116 for(int32 index
= 0; index
< parameter
->value_count
; index
++)
117 if(parameter
->values
[index
])
118 message
->AddString(MDSU_VALUES
, parameter
->values
[index
]);
120 for(int32 index
= 0; index
< parameter
->parameter_count
; index
++) {
121 BMessage parameterMessage
;
122 AddParameter(¶meter
->parameters
[index
], ¶meterMessage
);
123 message
->AddMessage(MDSU_PARAMETERS
, ¶meterMessage
);
131 ReadMessageDriverSettings(const char *name
, BMessage
*message
)
133 if(!name
|| !message
)
136 void *handle
= load_driver_settings(name
);
139 const driver_settings
*settings
= get_driver_settings(handle
);
141 unload_driver_settings(handle
);
145 for(int32 index
= 0; index
< settings
->parameter_count
; index
++) {
147 AddParameter(&settings
->parameters
[index
], ¶meter
);
148 message
->AddMessage(MDSU_PARAMETERS
, ¶meter
);
151 unload_driver_settings(handle
);
159 EscapeWord(BString
& word
)
161 word
.ReplaceAll("\\", "\\\\");
162 word
.ReplaceAll("#", "\\#");
163 word
.ReplaceAll("\"", "\\\"");
164 word
.ReplaceAll("\'", "\\\'");
170 WriteParameter(BFile
& file
, const BMessage
& parameter
, int32 level
)
173 if(parameter
.FindString(MDSU_NAME
, &name
) != B_OK
|| !name
)
176 BString line
, word(name
);
178 bool needsEscaping
= word
.FindFirst(' ') >= 0;
179 line
.SetTo('\t', level
);
186 for(int32 index
= 0; parameter
.FindString(MDSU_VALUES
, index
, &name
) == B_OK
;
192 needsEscaping
= word
.FindFirst(' ') >= 0;
201 int32 parameterCount
;
202 parameter
.GetInfo(MDSU_PARAMETERS
, &type
, ¶meterCount
);
204 if(parameterCount
> 0)
208 file
.Write(line
.String(), line
.Length());
210 if(parameterCount
> 0) {
211 BMessage subParameter
;
212 for(int32 index
= 0; parameter
.FindMessage(MDSU_PARAMETERS
, index
,
213 &subParameter
) == B_OK
; index
++)
214 WriteParameter(file
, subParameter
, level
+ 1);
216 line
.SetTo('\t', level
);
218 file
.Write(line
.String(), line
.Length());
226 WriteMessageDriverSettings(BFile
& file
, const BMessage
& message
)
228 if(file
.InitCheck() != B_OK
|| !file
.IsWritable())
232 file
.Seek(0, SEEK_SET
);
235 for(int32 index
= 0; message
.FindMessage(MDSU_PARAMETERS
, index
, ¶meter
) == B_OK
;
239 WriteParameter(file
, parameter
, 0);