How to map the country names in one enum to the time zone in another seperate enum - c

I am having an enum named Country like this,
Enum_1:
typedef enum = {Pacific/Midway, America/Adak, Etc/GMT+10, Pacific/Marquesas, Pacific/Gambier}Country;
and an enum for storing their corresponding time zones
Enum_2:
typedef enum = {GMT-11:00, GMT-10:00, GMT-10:00, GMT-09:30, GMT-09:00}zone;
I want to give the country as input from the user. When the input is given the code has to check the Country in Enum_1 and if the country is present, then it maps to the corresponding time zone in Enum_2
I know that enum returns only integer. I don't know whether we can map one enum to other
Example input:
Pacific/Midway
Output should be:
GMT-11:00
I am really confused about this. I want to know even if it is possible to implement this method in C?

Related

One variable name with multiple attribution

I want to write an function which reads specific sensor values from a modbus device.
To read the sensor value I need the device address and the register number.
I need an solution to store this two attribution under one name.
Example:
TEMPSENSOR_1 = deviceAddress = 0x09, registerNum = 0
sensorRead(TEMPSENSOR_1){
get the two attribution by name
modbusRequest(deviceAddress, registerNum ....);
}
When I am make an modbusRequest need the deviceAddress and registerNum from as name TEMPSENSOR_1.
How can I do this?
If your code is written in C++ (not in C) :
I think you need to use a std::pair from the header <utility> to do that.
https://en.cppreference.com/w/cpp/utility/pair
This allow you to store two values in the same object. But please make sure that these two values are strongly related to each other. If it's not, do not use that, and refactor your code.
If your code is written in C :
I think you want to use a struct who store two values.
https://www.tutorialspoint.com/cprogramming/c_structures.htm
This allow you to store two values in the same object. But please make sure that these two values are strongly related to each other. If it's not, do not use that, and refactor your code.
Do you need more informations ?

Write enum to DB as lowercase value

I am getting a #RequestBody where one of the fields is an enum in my model.
ie
public enum X{
VALUE("Value")
}
I am able to pass in "Value" with the request which gets mapped to the correct enum value just fine. But when I save the body to the database it saves it as "VALUE". Is there some annotation or relatively easy way to save it as "Value" in the DB?
my fallback option is to just rename the enum to Value.

What is the purpose of LVCOLUMN.cchTextMax and LVITEM.cchTextMax?

I have tried to set the values of LVCOLUMN.cchTextMax and LVITEM.cchTextMax to a random value (less than the corresponding string length) and it worked, did it work by mistake or these fields are not necessary to set?
I have also seen examples that do not use these fields, for example: http://www.codeproject.com/Articles/9148/C-functions-to-insert-columns-items-and-subitems-i
The documentation for the LVITEM structure has the following to say on the cchTextMax member:
This member is only used when the structure receives item attributes. It is ignored when the structure specifies item attributes. For example, cchTextMax is ignored during LVM_SETITEM and LVM_INSERTITEM. It is read-only during LVN_GETDISPINFO and other LVN_ notifications.
Unless you are using this structure to retrieve item information, this member is ignored. When receiving data you have to pass a pointer to a buffer to pszText, and communicate its size through the cchTextMax member.
This is a common pattern throughout the Windows API, where the same structure is used to set and query values. The semantics of the individual members depend on the direction. Other common structures, that are used in a similar fashion include TVITEM or MENUITEMINFO, for example.

Different typedef for same information

I was working on a piece of code and I encountered this :
typedef jobject jthread;
typedef jobject jthreadGroup;
What is the significance of different typedef for similar entry ?
It allows you to have two different names for the same type. There are (at least) two different reasons you'd want this:
In the code, you want the reader to see what type of data we're dealing with. After all, a "thread" is diffferent from a "group of threads", even if the underlying type representing them are different.
Ability to change type for one of the types without affecting the other. If at a later stage, we decide that "jthreadGroup" is better to be a new type, we can change that without affecting "jthread".
Imagine that we have a small game, where we count the score in an integer type. We also have a count of players:
typedef int ScoreCount;
typedef int PlayerCount;
Later on, we decide that we only need a small number, short to count players:
typedef short PlayerCount;
Same idea as using "jobject", just a different base-type, that possibly make it easy to understand.
May be for acheiving code clarity they typedefed same type in different name
Here you can use both jthread and jthreadGroup instead of jobject type

Objective-C / C giving enums default values

I read somewhere about giving enums default values like so:
typedef enum {
MarketNavigationTypeNone = 0,
MarketNavigationTypeHeirachy = 1,
MarketNavigationTypeMarket = 2
} MarketNavigationLevelType;
.. but i can't remember the value of doing this. If i don't give them default values - and then someone later on reorders the enum - what are the risks?
If i always use the enum name and don't even refer to them by their integer value, is there any risks?
The only possible problem i can think of is if i'm initialising an enum from an int value from a DB - and the enum is reordered - then the app would break.
That are not default values, you are giving them the values they will always have.
If you wouldn't initialize them explicitly, the first enumerators value is zero. For all others, if there is no initializer, their value is the value of the previous enumerator increased by one.
There are two reasons for giving them explicit values:
you don't want them to have the values they'd have otherwise
you want to make it clear what value they have (for you or other developers)
If you always refer to them by their name and never explicitly use an integral value for comparison or assignment, explicitly giving them a value is not needed.
In general this only matters if the enum is exposed to some kind of external API or it is going to be used to exchange data via data files or other means. If the enum is only every used within your app and nowhere else then the actual values don't matter.

Resources