enumeration (English)
/ɪˌnjuːməˈɹeɪʃən/
1. The act of mentioning a number of things one by one.
2. The act of determining the amount of something; counting.
3. A detailed account, in which each thing is specially noticed.
So I found out that 2 years ago, my OSCP automation repo was actually posted by a company named National Cyber Security Services. :D https://m.facebook.com/ncybersec/photos/oscp-automation-a-collection-of-personal-scripts-used-in-hacking-exercises-1-enu/1502887116548674/
I actually found this because I'm about to do some HTB and I was going to use that as reference material.
It may seem like such a minor thing, but it's actually amazing to see that a company saw value in it enough to post about it (even with the fact that it was extremely basic and the enum script is now an older version before I debugged due to being a complete noob with GitHub at the time.) Fangirl moment. XD
Repos below for the curious, though that GitHub account isn't active anymore (I stupidly registered it with a university email and never bothered to look into getting it back!)
I am actually thinking of re-creating something like this as I start up with HTB again (kinda got the hacking bug...)
A collection of personal scripts used in hacking excercises. - GitHub - C-Cracks/OSCP-Automation: A collection of personal scripts used in h
Lazarus – ways to extract values from an enumeration
Hello, guys! We're continuing to master Lazarus. It's a wonderful programming language similar to Delphi. There's a data type called an enumeration. And sometimes we want to extract values from it. Here's a funny question: How do we do this? We'll look at that today.
Let's create a standard project in Lazarus. With a Windows window. In the TYPE section, we'll add our enumeration. Let's say it's the seasons of the year: Autumn, Winter, Spring, Summer.
ESeason = (Autumn, Winter, Spring, Summer);
Method 1. WriteStr. The simplest.
And here's the simplest method. It's using WriteStr. We write the desired enumeration type element into a string.
Var
MySeason: ESeason;
StrVal:String;
begin
WriteStr(StrVal,MySeason);
Memo1.Lines.Add('MySeason=' + StrVal);
WriteStr(StrVal,Winter);
Memo1.Lines.Add(Winter=' + StrVal);
WriteStr(StrVal,ESeason.Spring);
Memo1.Lines.Add('ESeason.Spring=' + StrVal);
End;
Our StrVal text variable can hold a wide variety of enumeration values. This is all possible! Quite flexible and variable! Notice how many possibilities there are. You can pass a variable of an enumeration type, like MySeason. You can also pass one of the enumeration elements directly by name, like Winter.
And you can pass one of the enumeration values with the full name, as expected. The enumeration type and its specific value separated by a dot. For example, ESeason.Spring.
Method 2. GetEnumName (), Ord(). More complicated.
First, we include TypInfo in Uses.
And here's an important detail. TypeInfo doesn't work if enumerations have assigned numeric values.
For example, if an enumeration is defined like this:
ESeason2 = (Autumn=1, Winter, Spring, Summer); //typeinfo doesn't work when there are numeric values!
So, we're continuing to work with our enumeration from the beginning. This will work:
ESeason = (Autumn, Winter, Spring, Summer);
Now let's move on to the code. Extracting a value from the enumeration is done in several steps.
Preliminary detail. We obtain the numeric equivalent of the value via Ord().
Ord(MySeason)
Preliminary detail. We obtain information about our enumeration. Via TypeInfo.
TypeInfo(ESeason)
And we do everything through the conversion operator. Convert to a string using GetEnumType
Pass the type of our enumeration to GetEnumType. This is ESeason. And the numeric value we're interested in is MySeason.
IntVal:=ord(MySeason);
Example Program
var
MySeason:ESeason;
IntVal: integer;
StrVal:String;
begin
StrVal:=GetEnumName(TypeInfo(ESeason),ord(MySeason));
Memo1.Lines.Add('MySeason Integer value = ' + IntToStr(IntVal));
Memo1.Lines.Add('MySeason String value = ' + StrVal);
IntVal:=Ord(Winter);
StrVal:=GetEnumName(TypeInfo(ESeason),ord(Winter));
Memo1.Lines.Add('Winter Integer - ' + IntToStr(IntVal));
Memo1.Lines.Add('Winter String - ' + StrVal);
IntVal:=Ord(ESeason.Spring);
StrVal:=GetEnumName(TypeInfo(ESeason),ord(ESeason.Spring));
Memo1.Lines.Add('ESeason.Spring Integer - ' + IntToStr(IntVal));
Memo1.Lines.Add('ESeason.Spring String - ' + StrVal);
End;
Method 3. Manually assigning values. The most sophisticated method.
To do this, we'll create a chain. We'll also write the identifiers into a string array.
This still works as a constant. And with ESeason.
const MySeasonNames: Array[ESeason] of string =('Autumn','Winter','Spring','Summer');
Let me remind you that the enumeration itself looks like this:
ESeason = (Autumn, Winter, Spring, Summer);
We're essentially associating the enumeration with a special array. In the array, all values are written as strings.
Now we can access this array by passing the enumeration value as an array index.
For example, like this
StrVal:=MySeasonNames[MySeason];
Or like this
StrVal:=MySeasonNames[Winter];
Or like this
StrVal:=MySeasonNames[ESeason.Spring];
We've sort of mapped the array, associating it with the enumeration elements. A clever move. And now we access our map. By throwing enumeration values into an array. The array index is the enumeration value.
Those are three such methods! Delphi is interesting! Let's learn, guys! I'm learning too! And I'm learning everything gradually. Sometimes I spend a lot of time even on a couple of lines of code.
Dima Link is making retro videogames, apps, a little of music, write stories, and some retro more.