Ejemplo n.º 1
0
// Try each of the four constructors
void main(void)
 {
   LISTSTR::iterator i;
   LISTSTR test; 					// default constructor

   test.insert(test.end(), "one");
   test.insert(test.end(), "two");

   LISTSTR test2(test);            			// construct from another list
   LISTSTR test3(3, "three");      			// construct with three elements
    							// containing the value "three" 
   LISTSTR test4(++test3.begin(),test3.end());	// create from part of test3

   // Print them all out
   for (i =  test.begin(); i != test.end(); ++i)
     cout << *i << " ";
   cout << endl;

   for (i =  test2.begin(); i != test2.end(); ++i)
     cout << *i << " ";
   cout << endl;

   for (i =  test3.begin(); i != test3.end(); ++i)
     cout << *i << " ";
   cout << endl;

   for (i =  test4.begin(); i != test4.end(); ++i)
     cout << *i << " ";
   cout << endl;
 }
Ejemplo n.º 2
0
static int ReadWriteASection(bool bToRead, short SectionID,
                             FILE* pFile, void* pData = NULL)
{
    int nResult = 0;

    switch (SectionID)
    {
        case SECTION_SIGNATURE:
        {
            if (bToRead)
            {
                char acBuffer[LEN_SIG_FILE_START] = {'\0'};
                nResult = ReadAndValidateString(pFile, acBuffer, DATABASE_SIGNATURE);
                if (nResult != 0)
                {
                    ReadWriteASection(bToRead, SECTION_FILE_INVALID, NULL);
                }
            }
            else
            {
                nResult = WriteAString(pFile, DATABASE_SIGNATURE);
            }
            if (nResult == 0)
            {
                nResult = ReadWriteASection(bToRead, SECTION_VERSION, pFile);
            }
        }
        break;
        case SECTION_VERSION:
        {
            if (bToRead)
            {
                char acBuffer[LEN_VERSION] = {'\0'};
                nResult = ReadAndValidateString(pFile, acBuffer, VERSIONLINE);
                if(nResult != 0)
                {
                    CString strVer = (const char*)acBuffer;
                    strVer.Remove('.');
                    strVer.Remove('*');
                    strVer.Remove('/');
                    strVer.Replace("Version", "");
                    strVer.Remove(' ');

                    // Get the cfx version info
                    INT nVer = 0;

                    nVer = atoi(strVer);

                    switch (nVer)
                    {
                        case 10:
                            // Successfull
                            nResult = 0;
                            break;
                        default:
                            // Invalid
                            nResult = INVALID_FORMAT_FILE;
                            break;
                    }
                }

                // If the newer version[1.1] file is opened
                else
                {
                    char acBuffer[LEN_APPLICATION_VERSION] = {'\0'};

                    // Reading Application version
                    ReadAndValidateString(pFile, acBuffer, APPLICATION_VERSION);
                }

                if (nResult != 0)
                {
                    ReadWriteASection(bToRead, SECTION_FILE_INVALID, NULL);
                }
            }
            else
            {
                nResult = WriteAString(pFile, VERSIONLINE);
                // Writing Application version is added in the Version 1.1
                nResult = WriteAString(pFile, APPLICATION_VERSION);
            }
            if (nResult == 0)
            {
                nResult = ReadWriteASection(bToRead, SECTION_PROJECT_TABLE_SIGNATURE, pFile);
            }
        }
        break;
        case SECTION_PROJECT_TABLE_SIGNATURE:
        {
            if (bToRead)
            {
                char acBuffer[LEN_SIG_PROJ_TABLE] = {'\0'};
                nResult = ReadAndValidateString(pFile, acBuffer, PROJECT_TABLE_SIG);
                if (nResult != 0)
                {
                    ReadWriteASection(bToRead, SECTION_FILE_INVALID, NULL);
                }
            }
            else
            {
                nResult = WriteAString(pFile, PROJECT_TABLE_SIG);
            }
            if (nResult == 0)
            {
                nResult = ReadWriteASection(bToRead, SECTION_PROJECT_TABLE_COUNT, pFile);
            }
        }
        break;
        case SECTION_PROJECT_TABLE_COUNT:
        {
            UCHAR Entries = 0;
            if (bToRead)
            {
                //fseek(pFile, sizeof("\n")-1, SEEK_CUR);
                if (fread(&Entries, sizeof(Entries), 1, pFile) == 1)
                {
                    nResult = ReadWriteASection(bToRead, SECTION_PROJECT_TABLE_ENTRY,
                                                pFile, &Entries);
                }
                else
                {
                    nResult = GetTheErrorType(pFile);
                }
            }
            else
            {
                Entries = (UCHAR) g_ProjCfgManager.GetProjectCount();
                //fputs("\n", pFile);
                if (fwrite(&Entries, sizeof(UCHAR), 1, pFile) == 1)
                {
                    nResult = ReadWriteASection(bToRead, SECTION_PROJECT_TABLE_ENTRY,
                                                pFile, &Entries);
                }
                else
                {
                    nResult = PROBLEM_IN_WRITING_READING;
                }
            }
        }
        break;
        case SECTION_PROJECT_TABLE_ENTRY:
        {
            UCHAR Entries = *((UCHAR*) pData);
            if (bToRead)
            {
                for (UCHAR i = 0; (i < Entries) && (nResult == 0); i++)
                {
                    PROJECTDATA sProjectDataTmp;
                    if (sProjectDataTmp.Read(pFile))
                    {
                        g_ProjCfgManager.AddModifyProjDetail(sProjectDataTmp);
                    }
                    else
                    {
                        nResult = GetTheErrorType(pFile);
                    }
                }
            }
            else
            {
                bool bAllWell = true;
                LISTSTR ProjectList;
                g_ProjCfgManager.GetProjectList(ProjectList);

                for (LISTSTR::iterator i = ProjectList.begin(); (i != ProjectList.end()) && bAllWell; ++i)
                {
                    PROJECTDATA ProjData;
                    if (bAllWell = g_ProjCfgManager.GetProjectData(*i, ProjData))
                    {
                        bAllWell = ProjData.Write(pFile);
                    }
                }
                if (!bAllWell)
                {
                    nResult = PROBLEM_IN_WRITING_READING;
                }
            }
            if (nResult == 0)
            {
                nResult = ReadWriteASection(bToRead, SECTION_SECTION_TABLE_ENTRY,
                                            pFile, &Entries);
            }
        }
        break;
        case SECTION_SECTION_TABLE_ENTRY:
        {
            bool bAllWell = true;
            if (bToRead)
            {
                UCHAR ProjectEntries = *((UCHAR*) pData);
                for (UCHAR i = 0; (i < ProjectEntries) && bAllWell; i++)
                {
                    string ProjectName;
                    bAllWell = ReadAString(pFile, ProjectName);
                    //ProjectName[ProjectName.length()-1] = '\0';
                    UCHAR SectionEntries = 0;
                    if (bAllWell)
                    {
                        //fseek(pFile, sizeof("\n")-1, SEEK_CUR);
                        bAllWell = (fread(&SectionEntries, sizeof(SectionEntries),
                                          1, pFile) == 1);
                    }
                    if (bAllWell)
                    {
                        for (UCHAR j = 0; (j < SectionEntries) && bAllWell; j++)
                        {
                            SECTIONDATA CurrSection;
                            if (bAllWell = CurrSection.Read(pFile))
                            {
                                g_ProjCfgManager.AddModifySection(
                                    ProjectName, CurrSection);
                            }
                            else
                            {
                                nResult = GetTheErrorType(pFile);
                            }
                        }
                    }
                }
                if (!bAllWell)
                {
                    nResult = PROBLEM_IN_WRITING_READING;
                }
            }
            else
            {
                LISTSTR ProjectList;
                g_ProjCfgManager.GetProjectList(ProjectList);

                for (LISTSTR::iterator i = ProjectList.begin(); (i != ProjectList.end()) && bAllWell; ++i)
                {
                    const char* str = i->c_str();
                    if ((nResult = WriteAString(pFile, (char*) str)) == 0)
                    {
                        LISTSTR SectionList;
                        UCHAR Sections = (UCHAR) g_ProjCfgManager.GetSectionList(*i, SectionList);
                        //fputs("\n", pFile);
                        if (fwrite(&Sections, sizeof(UCHAR), 1, pFile) != 1)
                        {
                            bAllWell = false;
                        }

                        for (LISTSTR::iterator j = SectionList.begin(); (j != SectionList.end()) && bAllWell; ++j)
                        {
                            SECTIONDATA CurrSecData;
                            bAllWell = g_ProjCfgManager.GetSectionData(*i, *j, CurrSecData);
                            if (bAllWell)
                            {
                                bAllWell = CurrSecData.Write(pFile);
                            }
                        }
                    }
                }
                if (!bAllWell)
                {
                    nResult = PROBLEM_IN_WRITING_READING;
                }
            }
            if (bAllWell)
            {
                nResult = ReadWriteASection(bToRead, SECTION_SIGNATURE_FILE_END, pFile);
            }
        }
        break;
        case SECTION_SIGNATURE_FILE_END:
        {
            if (bToRead)
            {
                char acBuffer[LEN_SIG_FILE_END] = {'\0'};
                nResult = ReadAndValidateString(pFile, acBuffer, DB_END_SIGNATURE);
                if (nResult != 0)
                {
                    ReadWriteASection(bToRead, SECTION_FILE_INVALID, NULL);
                }
            }
            else
            {
                nResult = WriteAString(pFile, DB_END_SIGNATURE);
            }
        }
        break;


        default:
        {
            nResult = FILE_INVALID_SECTIONID;
        }
        break;
    }

    return nResult;
}