[SETUPLIB][USETUP] Reduce duplicated code

- ScrollDownPartitionList() and ScrollUpPartitionList()
  --> ScrollUpDownPartitionList()

- GetPrimaryPartitionCount() and GetLogicalPartitionCount()
  --> generic GetPartitionCount() and macros.

- GetPrevUnpartitionedEntry() and GetNextUnpartitionedEntry()
  --> GetAdjUnpartitionedEntry() ("Adj" == Adjacent)
This commit is contained in:
Hermès Bélusca-Maïto 2024-07-04 15:28:53 +02:00
parent 97821f00d5
commit 7639cb750a
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
5 changed files with 73 additions and 122 deletions

View file

@ -702,9 +702,6 @@ ResolveArcNameManually(
BOOLEAN UseSignature; BOOLEAN UseSignature;
SIZE_T NameLength; SIZE_T NameLength;
PDISKENTRY DiskEntry;
PPARTENTRY PartEntry = NULL;
if (NtName->MaximumLength < sizeof(UNICODE_NULL)) if (NtName->MaximumLength < sizeof(UNICODE_NULL))
return STATUS_BUFFER_TOO_SMALL; return STATUS_BUFFER_TOO_SMALL;
@ -757,6 +754,9 @@ ResolveArcNameManually(
else else
if (PeripheralType == RDiskPeripheral) if (PeripheralType == RDiskPeripheral)
{ {
PDISKENTRY DiskEntry;
PPARTENTRY PartEntry = NULL;
if (UseSignature) if (UseSignature)
{ {
/* The disk signature is stored in AdapterKey */ /* The disk signature is stored in AdapterKey */

View file

@ -2432,59 +2432,39 @@ IsSamePrimaryLayoutEntry(
// PartitionInfo->PartitionType == PartEntry->PartitionType // PartitionInfo->PartitionType == PartEntry->PartitionType
} }
/**
* @brief
* Counts the number of partitioned disk regions in a given partition list.
**/
static static
ULONG ULONG
GetPrimaryPartitionCount( GetPartitionCount(
IN PDISKENTRY DiskEntry) _In_ PLIST_ENTRY PartListHead)
{ {
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
PPARTENTRY PartEntry; PPARTENTRY PartEntry;
ULONG Count = 0; ULONG Count = 0;
if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT) for (Entry = PartListHead->Flink;
{ Entry != PartListHead;
DPRINT("GPT-partitioned disk detected, not currently supported by SETUP!\n");
return 0;
}
for (Entry = DiskEntry->PrimaryPartListHead.Flink;
Entry != &DiskEntry->PrimaryPartListHead;
Entry = Entry->Flink) Entry = Entry->Flink)
{ {
PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry); PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
if (PartEntry->IsPartitioned) if (PartEntry->IsPartitioned)
Count++; ++Count;
} }
return Count; return Count;
} }
static #define GetPrimaryPartitionCount(DiskEntry) \
ULONG GetPartitionCount(&(DiskEntry)->PrimaryPartListHead)
GetLogicalPartitionCount(
IN PDISKENTRY DiskEntry)
{
PLIST_ENTRY ListEntry;
PPARTENTRY PartEntry;
ULONG Count = 0;
if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT) #define GetLogicalPartitionCount(DiskEntry) \
{ (((DiskEntry)->DiskStyle == PARTITION_STYLE_MBR) \
DPRINT("GPT-partitioned disk detected, not currently supported by SETUP!\n"); ? GetPartitionCount(&(DiskEntry)->LogicalPartListHead) : 0)
return 0;
}
for (ListEntry = DiskEntry->LogicalPartListHead.Flink;
ListEntry != &DiskEntry->LogicalPartListHead;
ListEntry = ListEntry->Flink)
{
PartEntry = CONTAINING_RECORD(ListEntry, PARTENTRY, ListEntry);
if (PartEntry->IsPartitioned)
Count++;
}
return Count;
}
static static
BOOLEAN BOOLEAN
@ -2727,73 +2707,53 @@ UpdateDiskLayout(
#endif #endif
} }
/**
* @brief
* Retrieves, if any, the unpartitioned disk region that is adjacent
* (next or previous) to the specified partition.
*
* @param[in] PartEntry
* Partition from where to find the adjacent unpartitioned region.
*
* @param[in] Direction
* TRUE or FALSE to search the next or previous region, respectively.
*
* @return The adjacent unpartitioned region, if it exists, or NULL.
**/
static static
PPARTENTRY PPARTENTRY
GetPrevUnpartitionedEntry( GetAdjUnpartitionedEntry(
IN PPARTENTRY PartEntry) _In_ PPARTENTRY PartEntry,
_In_ BOOLEAN Direction)
{ {
PDISKENTRY DiskEntry = PartEntry->DiskEntry; PDISKENTRY DiskEntry = PartEntry->DiskEntry;
PPARTENTRY PrevPartEntry; PLIST_ENTRY ListHead, AdjEntry;
PLIST_ENTRY ListHead;
if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT) /* In case of MBR disks only, check the logical partitions if necessary */
if ((DiskEntry->DiskStyle == PARTITION_STYLE_MBR) &&
PartEntry->LogicalPartition)
{ {
DPRINT("GPT-partitioned disk detected, not currently supported by SETUP!\n");
return NULL;
}
if (PartEntry->LogicalPartition)
ListHead = &DiskEntry->LogicalPartListHead; ListHead = &DiskEntry->LogicalPartListHead;
}
else else
{
ListHead = &DiskEntry->PrimaryPartListHead; ListHead = &DiskEntry->PrimaryPartListHead;
if (PartEntry->ListEntry.Blink != ListHead)
{
PrevPartEntry = CONTAINING_RECORD(PartEntry->ListEntry.Blink,
PARTENTRY,
ListEntry);
if (!PrevPartEntry->IsPartitioned)
{
ASSERT(PrevPartEntry->PartitionType == PARTITION_ENTRY_UNUSED);
return PrevPartEntry;
}
} }
return NULL; if (Direction)
} AdjEntry = PartEntry->ListEntry.Flink; // Next region.
static
PPARTENTRY
GetNextUnpartitionedEntry(
IN PPARTENTRY PartEntry)
{
PDISKENTRY DiskEntry = PartEntry->DiskEntry;
PPARTENTRY NextPartEntry;
PLIST_ENTRY ListHead;
if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT)
{
DPRINT("GPT-partitioned disk detected, not currently supported by SETUP!\n");
return NULL;
}
if (PartEntry->LogicalPartition)
ListHead = &DiskEntry->LogicalPartListHead;
else else
ListHead = &DiskEntry->PrimaryPartListHead; AdjEntry = PartEntry->ListEntry.Blink; // Previous region.
if (PartEntry->ListEntry.Flink != ListHead) if (AdjEntry != ListHead)
{ {
NextPartEntry = CONTAINING_RECORD(PartEntry->ListEntry.Flink, PartEntry = CONTAINING_RECORD(AdjEntry, PARTENTRY, ListEntry);
PARTENTRY, if (!PartEntry->IsPartitioned)
ListEntry);
if (!NextPartEntry->IsPartitioned)
{ {
ASSERT(NextPartEntry->PartitionType == PARTITION_ENTRY_UNUSED); ASSERT(PartEntry->PartitionType == PARTITION_ENTRY_UNUSED);
return NextPartEntry; return PartEntry;
} }
} }
return NULL; return NULL;
} }
@ -3094,8 +3054,8 @@ DeletePartition(
/* Adjust the unpartitioned disk space entries */ /* Adjust the unpartitioned disk space entries */
/* Get pointer to previous and next unpartitioned entries */ /* Get pointer to previous and next unpartitioned entries */
PrevPartEntry = GetPrevUnpartitionedEntry(PartEntry); PrevPartEntry = GetAdjUnpartitionedEntry(PartEntry, FALSE);
NextPartEntry = GetNextUnpartitionedEntry(PartEntry); NextPartEntry = GetAdjUnpartitionedEntry(PartEntry, TRUE);
if (PrevPartEntry != NULL && NextPartEntry != NULL) if (PrevPartEntry != NULL && NextPartEntry != NULL)
{ {

View file

@ -833,28 +833,22 @@ DrawPartitionList(
} }
} }
/**
* @param[in] Direction
* TRUE or FALSE to scroll to the next (down) or previous (up) entry, respectively.
**/
VOID VOID
ScrollDownPartitionList( ScrollUpDownPartitionList(
IN PPARTLIST_UI ListUi) _In_ PPARTLIST_UI ListUi,
_In_ BOOLEAN Direction)
{ {
PPARTENTRY NextPart = GetNextPartition(ListUi->List, ListUi->CurrentPartition); PPARTENTRY PartEntry =
if (NextPart) (Direction ? GetNextPartition
: GetPrevPartition)(ListUi->List, ListUi->CurrentPartition);
if (PartEntry)
{ {
ListUi->CurrentPartition = NextPart; ListUi->CurrentPartition = PartEntry;
ListUi->CurrentDisk = NextPart->DiskEntry; ListUi->CurrentDisk = PartEntry->DiskEntry;
DrawPartitionList(ListUi);
}
}
VOID
ScrollUpPartitionList(
IN PPARTLIST_UI ListUi)
{
PPARTENTRY PrevPart = GetPrevPartition(ListUi->List, ListUi->CurrentPartition);
if (PrevPart)
{
ListUi->CurrentPartition = PrevPart;
ListUi->CurrentDisk = PrevPart->DiskEntry;
DrawPartitionList(ListUi); DrawPartitionList(ListUi);
} }
} }

View file

@ -85,16 +85,13 @@ InitPartitionListUi(
IN SHORT Right, IN SHORT Right,
IN SHORT Bottom); IN SHORT Bottom);
VOID
ScrollDownPartitionList(
IN PPARTLIST_UI ListUi);
VOID
ScrollUpPartitionList(
IN PPARTLIST_UI ListUi);
VOID VOID
DrawPartitionList( DrawPartitionList(
IN PPARTLIST_UI ListUi); IN PPARTLIST_UI ListUi);
VOID
ScrollUpDownPartitionList(
_In_ PPARTLIST_UI ListUi,
_In_ BOOLEAN Direction);
/* EOF */ /* EOF */

View file

@ -1689,12 +1689,12 @@ SelectPartitionPage(PINPUT_RECORD Ir)
else if ((Ir->Event.KeyEvent.uChar.AsciiChar == 0x00) && else if ((Ir->Event.KeyEvent.uChar.AsciiChar == 0x00) &&
(Ir->Event.KeyEvent.wVirtualKeyCode == VK_DOWN)) /* DOWN */ (Ir->Event.KeyEvent.wVirtualKeyCode == VK_DOWN)) /* DOWN */
{ {
ScrollDownPartitionList(&ListUi); ScrollUpDownPartitionList(&ListUi, TRUE);
} }
else if ((Ir->Event.KeyEvent.uChar.AsciiChar == 0x00) && else if ((Ir->Event.KeyEvent.uChar.AsciiChar == 0x00) &&
(Ir->Event.KeyEvent.wVirtualKeyCode == VK_UP)) /* UP */ (Ir->Event.KeyEvent.wVirtualKeyCode == VK_UP)) /* UP */
{ {
ScrollUpPartitionList(&ListUi); ScrollUpDownPartitionList(&ListUi, FALSE);
} }
else if (Ir->Event.KeyEvent.wVirtualKeyCode == VK_RETURN) /* ENTER */ else if (Ir->Event.KeyEvent.wVirtualKeyCode == VK_RETURN) /* ENTER */
{ {