#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
#ifdef MEDIA671_F31E_F31J_P31J
extern unsigned int16_t G7152_;
#endif
extern FILE_INFO G2297_DataFiles[10];

static int16_t F7200_(FILE_ID_INFO* P3931_ps_, FILE_ID_INFO** P3932_pps_);
static int16_t F7201_GetDataFileIndexByFileID(unsigned int16_t P3933_ui_);

#ifdef MEDIA670_F31E_F31J
FILE_LIST G7151_;
char G2322_Unreferenced[4];
unsigned int16_t G7150_;
#endif
#ifdef MEDIA699_X31J_AU2E_AU3E
FILE_LIST G7151_;
char G2322_Unreferenced[4];
unsigned int16_t G7150_;
#endif
UDSTRUCT8 G2296_[1] = { { 0, 0, 0, 0, 0, 0 } };
#ifdef MEDIA699_X31J_AU2E_AU3E
unsigned int16_t G7152_;
#endif
#ifdef MEDIA670_F31E_F31J
unsigned int16_t G7152_;
#endif


void F7186_(int16_t* P3906_pi_, UDSTRUCT8** P3907_pps_)
{
        G7152_ = 0;
        for (;;) { /*_Infinite loop_*/
                if (G2296_[G7152_].UDS8_MemReqFlags == 0) {
                        break;
                }
                G7152_++;
        };
        *P3906_pi_ = G7152_;
        *P3907_pps_ = G2296_;
}

int16_t F7187_(
)
{
        int16_t L4362_i_;
        unsigned int16_t L4361_ui_;
        UDSTRUCT1* L4360_ps_;


        for (L4361_ui_ = 0; L4361_ui_ < G7152_; L4361_ui_++) {
                L4360_ps_ = (G2296_[L4361_ui_].UDS8_pc_6)(&G2296_[L4361_ui_]);
                if (L4360_ps_ == NULL) {
                        L4362_i_ = 3;
                        goto loc_D3B6;
                }
                (G2296_[L4361_ui_].UDS8_pc_A)(&G2296_[L4361_ui_]);
        }
        G7150_ = 0;
        L4362_i_ = 0;
        loc_D3B6:
        return L4362_i_;
}

void F7188_(int16_t* P3908_pi_, UDSTRUCT8** P3909_pps_)
{
        *P3908_pi_ = G7152_;
        *P3909_pps_ = G2296_;
}

unsigned long F7189_ReturnUdStruct77ByteCount(
)
{
        return (long)sizeof(FILE_ID_INFO);
}

int16_t F7190_InitUdStruct77(FILE_ID_INFO* P3910_ps_, int16_t P3911_i_FileID)
{
        int16_t L4364_i_;
        FILE_ID_INFO* L4363_ps_;
        int16_t L6066_i_Unreferenced;


        L4363_ps_ = P3910_ps_;
        L4363_ps_->ud77_i_Signature = 0x2352;
        L4363_ps_->ud77_i_Unused = 0;
        L4363_ps_->ud77_i_FileID = P3911_i_FileID;
        L4364_i_ = 0;
        if (L4364_i_);
        return L4364_i_;
}

int16_t F7191_(FILE_ID_INFO* P3912_ps_)
{
        int16_t L4366_i_;
        FILE_ID_INFO* L4365_ps_;


        if (F7200_(P3912_ps_, &L4365_ps_) < 0) {
                L4366_i_ = 3;
        } else {
                L4365_ps_->ud77_i_Signature = 0x2353;
                L4366_i_ = 0;
        }
        if (L4366_i_);
        return L4366_i_;
}

int16_t F7192_GetDataFileInfo(FILE_ID_INFO* P3913_ps_, unsigned int16_t P3914_ui_, long* P3915_pl_)
{
        int16_t L4369_i_;
        FILE_ID_INFO* L4367_ps_;
        unsigned int16_t L4368_i_;


        if (F7200_(P3913_ps_, &L4367_ps_) < 0) {
                L4369_i_ = 3;
        } else {
                L4368_i_ = F7201_GetDataFileIndexByFileID(L4367_ps_->ud77_i_FileID);
                switch (P3914_ui_) {
                        case C01_FILE_INFO_:
                                *P3915_pl_ = (long)G2297_DataFiles[L4368_i_].fi_ui_0A;
                                break;
                        case C02_FILE_INFO_:
                                *P3915_pl_ = (long)G2297_DataFiles[L4368_i_].fi_ui_0C;
                                break;
                        case C03_FILE_INFO_:
                                *P3915_pl_ = (long)G2297_DataFiles[L4368_i_].fi_ui_0E;
                                break;
                        case C04_FILE_INFO_:
                                *P3915_pl_ = (long)G2297_DataFiles[L4368_i_].fi_ui_04;
                                break;
                        case C05_FILE_INFO_:
                                *P3915_pl_ = (long)G2297_DataFiles[L4368_i_].fi_ui_10;
                                break;
                        case C06_FILE_INFO_:
                                *P3915_pl_ = (long)((G2297_DataFiles[L4368_i_].Flags & 0x01) ? 1 : 0);
                                break;
                        case C07_FILE_INFO_:
                                *P3915_pl_ = (long)((G2297_DataFiles[L4368_i_].Flags & 0x02) ? 1 : 0);
                                break;
                        case C08_FILE_INFO_:
                                *P3915_pl_ = (long)((G2297_DataFiles[L4368_i_].Flags & 0x04) ? 1 : 0);
                                break;
                        case C09_FILE_INFO_:
                                *P3915_pl_ = (long)((G2297_DataFiles[L4368_i_].Flags & 0x08) ? 1 : 0);
                                break;
                        case C10_FILE_INFO_:
                                *P3915_pl_ = (long)((G2297_DataFiles[L4368_i_].Flags & 0x10) ? 1 : 0);
                                break;
                        case C11_FILE_INFO_SOURCE_USAGE:
                                *P3915_pl_ = (long)(long)G2297_DataFiles[L4368_i_].SourceUsage;
                                break;
                        case C12_FILE_INFO_DESTINATION_USAGE:
                                *P3915_pl_ = (long)(long)G2297_DataFiles[L4368_i_].DestinationUsage;
                                break;
                        case C13_FILE_INFO_FILE_NAME:
                                *P3915_pl_ = (long)(long)G2297_DataFiles[L4368_i_].FileName;
                                break;
                        case C15_FILE_INFO_:
                                *P3915_pl_ = (long)((G2297_DataFiles[L4368_i_].Flags & 0x20) ? 1 : 0);
                                break;
                        default:
                                L4369_i_ = 3;
                                goto loc_D67E;
                }
                L4369_i_ = 0;
        }
        loc_D67E:
        return L4369_i_;
}

void F7193_RemoveExtensionsFromFileNames(char* P3916_pc_FileNamesList, unsigned int16_t P3917_ui_FileNameMaximumLength, unsigned int16_t P3918_ui_FileCount)
{ /* Replace non trailing '.' by '\0' */
        unsigned int16_t L4370_ui_;
        unsigned int16_t L4372_ui_;
        unsigned char* L4371_pc_;
        unsigned int16_t L4373_ui_;


        L4373_ui_ = P3917_ui_FileNameMaximumLength;
        for (L4370_ui_ = 0; L4370_ui_ < P3918_ui_FileCount; L4370_ui_++) {
                L4371_pc_ = M774_CAST_PUC(P3916_pc_FileNamesList) + L4370_ui_ * P3917_ui_FileNameMaximumLength;
                for (L4372_ui_ = 0; L4372_ui_ < L4373_ui_; L4372_ui_++) {
                        if (L4372_ui_ == L4373_ui_ - 1) {
                                L4371_pc_[L4372_ui_ + 1] = '\0';
                                break;
                        }
                        if ((L4371_pc_[L4372_ui_] == '.') && (L4371_pc_[L4372_ui_ + 1] != '\0')) {
                                L4371_pc_[L4372_ui_] = '\0';
                                break;
                        }
                        if (L4371_pc_[L4372_ui_] == '\0') {
                                L4371_pc_[L4372_ui_ + 1] = '\0';
                                break;
                        }
                }
        }
}

void F7194_RestoreExtensionsInFileNames(char* P3919_pc_FileNamesList, unsigned int16_t P3920_ui_FileNameMaximumLength, unsigned int16_t P3921_ui_FileCount)
{
        unsigned int16_t L4374_ui_;
        unsigned int16_t L4376_ui_;
        unsigned char* L4375_pc_;
        unsigned int16_t L4377_ui_;


        L4377_ui_ = P3920_ui_FileNameMaximumLength;
        for (L4374_ui_ = 0; L4374_ui_ < P3921_ui_FileCount; L4374_ui_++) {
                L4375_pc_ = M774_CAST_PUC(P3919_pc_FileNamesList) + L4374_ui_ * P3920_ui_FileNameMaximumLength;
                for (L4376_ui_ = 0; L4376_ui_ < L4377_ui_; L4376_ui_++) {
                        if (L4376_ui_ == L4377_ui_ - 1) {
                                break;
                        }
                        if (L4375_pc_[L4376_ui_] == '\0') {
                                if (L4375_pc_[L4376_ui_ + 1] == '\0')
                                        break;
                                L4375_pc_[L4376_ui_] = '.';
                                break;
                        }
                }
        }
}

char F7195_AreFileNamesIdentical(FILE_ID_INFO* P3922_ps_, FILE_ID_INFO* P3923_ps_)
{
        int16_t L4384_i_;
        FILE_ID_INFO* L4378_ps_;
        FILE_ID_INFO* L4379_ps_;
        char* L4380_pc_;
        char* L4381_pc_;
        char L4382_c_;
        unsigned int16_t L4383_i_;


        L4382_c_ = C0_FALSE;
        if (F7200_(P3922_ps_, &L4378_ps_) < 0) {
                L4384_i_ = 3;
        } else {
                if (F7200_(P3923_ps_, &L4379_ps_) < 0) {
                        L4384_i_ = 3;
                } else {
                        if (L4378_ps_->ud77_i_FileID == L4379_ps_->ud77_i_FileID) {
                                L4382_c_ = C1_TRUE;
                        } else {
                                L4383_i_ = F7201_GetDataFileIndexByFileID(L4378_ps_->ud77_i_FileID);
                                L4380_pc_ = G2297_DataFiles[L4383_i_].FileName;
                                L4383_i_ = F7201_GetDataFileIndexByFileID(L4379_ps_->ud77_i_FileID);
                                L4381_pc_ = G2297_DataFiles[L4383_i_].FileName;
                                if (F0087_strcmp(L4380_pc_, L4381_pc_) == 0) {
                                        L4382_c_ = C1_TRUE;
                                }
                        }
                }
        }
        return L4382_c_;
}

int16_t F7196_PrepareAndDrawFilePicker(FILE_ID_INFO* P3924_ps_, char* P3925_pc_FileNamesList, int16_t P3926_i_FileNameMaximumLength, int16_t P3927_i_FileCount)
{
        int16_t L4385_i_ = 0;


        F7193_RemoveExtensionsFromFileNames(P3925_pc_FileNamesList, P3926_i_FileNameMaximumLength, P3927_i_FileCount);
        G7150_ |= 2;
        G7151_.Title = G7070_PLEASE_SELECT_A_PORTRAIT;
        G7151_.pfv_04 = 0;
        G7151_.pfv_08_z = 0;
        G7151_.StringCompare = F0087_strcmp;
        G7151_.FileNameMaximumLength = P3926_i_FileNameMaximumLength;
        G7151_.FileNamesList = P3925_pc_FileNamesList;
        G7151_.FileCount = P3927_i_FileCount;
        if (F7082_DrawFilePickerDialog(&G7151_) < 0) {
                L4385_i_ = 99;
        } else {
                G7150_ |= 1;
        }
        return L4385_i_;
}

int16_t F7197_(char* P3928_pc_FileNamesList, int16_t P3929_i_FileNameMaximumLength, int16_t P3930_i_FileCount)
{
        int16_t L4386_i_ = 0;


        F7193_RemoveExtensionsFromFileNames(P3928_pc_FileNamesList, P3929_i_FileNameMaximumLength, P3930_i_FileCount);
        G7150_ |= 2;
        G7151_.FileNameMaximumLength = P3929_i_FileNameMaximumLength;
        G7151_.FileNamesList = P3928_pc_FileNamesList;
        G7151_.FileCount = P3930_i_FileCount;
        if (F7083_DrawInitialFilePickerFileList(&G7151_) < 0) {
                L4386_i_ = 99;
        }
        return L4386_i_;
}

int16_t F7198_(
)
{
        int16_t L4387_i_ = 0;


        if (G7150_ & 1) {
                if (F7085_(1) < 0) {
                        G7150_ &= ~0x01;
                } else {
                        L4387_i_ = 99;
                }
        }
        if (G7150_ & 2) {
                G7150_ &= ~0x02;
                F7194_RestoreExtensionsInFileNames(G7151_.FileNamesList, G7151_.FileNameMaximumLength, G7151_.FileCount);
        }
        return L4387_i_;
}

int16_t F7199_(
)
{
        int16_t L4388_i_;


        L4388_i_ = F7084_ProcessFilePickerInput();
        if (L4388_i_ < 0) {
                if (L4388_i_ == -2) { /* Cancel */
                        L4388_i_ = -22;
                } else {
                        if (L4388_i_ == -1) { /* New disk */
                                L4388_i_ = -23;
                        }
                }
        } else {
                if (G7150_ & 2) {
                        G7150_ &= ~0x02;
                        F7194_RestoreExtensionsInFileNames(G7151_.FileNamesList, G7151_.FileNameMaximumLength, G7151_.FileCount);
                }
        }
        return L4388_i_;
}

int16_t F7200_(FILE_ID_INFO* P3931_ps_, FILE_ID_INFO** P3932_pps_)
{
        int16_t L4390_i_;
        FILE_ID_INFO* L4389_ps_;


        L4389_ps_ = P3931_ps_;
        *P3932_pps_ = L4389_ps_;
        if (L4389_ps_->ud77_i_Signature != 0x2352) {
                L4390_i_ = -1;
        } else {
                L4390_i_ = 0;
        }
        if (L4390_i_);
        return L4390_i_;
}

int16_t F7201_GetDataFileIndexByFileID(unsigned int16_t P3933_ui_)
{
        unsigned int16_t L4391_i_ = 0;


        for (;;) { /*_Infinite loop_*/
                if (G2297_DataFiles[L4391_i_].FileID == P3933_ui_)
                        break;
                if (G2297_DataFiles[L4391_i_].FileID == (long)0x0000FFFE) {
                        L4391_i_ = 0;
                        break;
                }
                L4391_i_++;
        };
        return L4391_i_;
}
#endif
