-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoded-message.h
More file actions
76 lines (61 loc) · 4.28 KB
/
Copy pathcoded-message.h
File metadata and controls
76 lines (61 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2017 Matthew A. Kucenski
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CODED_MESSAGE_H_
#define CODED_MESSAGE_H_
//#define _DEBUG_ 1
#include <sys/types.h>
#include <iostream>
using namespace std;
// The goal of this code is to simplify the process matching flagged bits and their associated message and details.
// Given a multi-bit value and defined table such as the following, you can find which bits are set and return the
// details.
//
// static coded_message_t USNJRNL_SOURCES[] = {
// {"USN_SOURCE_AUXILIARY_DATA" 0x00000002, "The operation adds a private data..."},
// {"USN_SOURCE_DATA_MANAGEMENT" 0x00000001, "The operation provides information..."},
// {"USN_SOURCE_REPLICATION_MANAGEMENT" 0x00000004, "The operation is modifying a file..."},
// {"USN_SOURCE_CLIENT_REPLICATION_MANAGEMENT" 0x00000008, "The operation is modifying a file..."}
// };
//
// Typically this would be accomplished by various '#define's and the following:
// 'if (flags & single_flag > 0) { cout << single_flag_msg }'.
//
// Instead, this can be accomplished by:
// 'getMessage(single_flag, USNJRNL_SOURCES, sizeof(USNJRNL_SOURCES));'
//
// Alternatively, you can request a comma-separated list of all messages values for a given set of codes as follows:
// 'getMessages(flags, USNJRNL_SOURCES, sizeof(USNJRNL_SOURCES));'
typedef struct {
string strMessage;
u_int32_t idCode;
string strDetails;
string strShort;
} coded_message_t;
// Given a code value, find the corresponding message.
inline string getMessage(u_int32_t idCode, coded_message_t* table, u_int32_t size) { for (int i=0; i<size/sizeof(table[0]); i++) { if (table[i].idCode == idCode) { return table[i].strMessage; } } return ""; }
// Given a code value, find the corresponding short message.
inline string getShort(u_int32_t idCode, coded_message_t* table, u_int32_t size) { for (int i=0; i<size/sizeof(table[0]); i++) { if (table[i].idCode == idCode) { return table[i].strShort; } } return ""; }
// Given multiples code values, return a comma-separated list of messages.
inline string getMessages(u_int32_t codes, coded_message_t* table, u_int32_t size) { string rv; bool first=true; for (int i=0; i<size/sizeof(table[0]); i++) { if ((table[i].idCode & codes) > 0) { rv += (first ? "" : ", ") + table[i].strMessage; first=false; } } return rv; }
// Given multiples code values, return a comma-separated list of short messages.
inline string getShorts(u_int32_t codes, coded_message_t* table, u_int32_t size) { string rv; bool first=true; for (int i=0; i<size/sizeof(table[0]); i++) { if ((table[i].idCode & codes) > 0) { rv += (first ? "" : ", ") + table[i].strShort; first=false; } } return rv; }
// Given a message string, return the code value.
inline u_int32_t getCode(string strMessage, coded_message_t* table, u_int32_t size) { for (int i=0; i<size/sizeof(table[0]); i++) { if (table[i].strMessage == strMessage) { return table[i].idCode; } } return 0; }
// Given a message string, return the details string.
inline string getDetails(string strMessage, coded_message_t* table, u_int32_t size) { for (int i=0; i<size/sizeof(table[0]); i++) { if (table[i].strMessage == strMessage) { return table[i].strDetails; } } return ""; }
// Given a code value, return the details string.
inline string getDetails(u_int32_t idCode, coded_message_t* table, u_int32_t size) { for (int i=0; i<size/sizeof(table[0]); i++) { if (table[i].idCode == idCode) { return table[i].strDetails; } } return ""; }
// For a given set of codes, XOR out the known codes and return what's left. Assuming we know about all valid codes, the return value should be zero.
inline u_int32_t findUnknownCodes(u_int32_t codes, coded_message_t* table, u_int32_t size) { for (int i=0; i<size/sizeof(table[0]); i++) { codes ^= table[i].idCode; } return codes; }
#endif /*CODED_MESSAGE_H_*/