コード例 #1
0
ファイル: Helper-Arrays.c プロジェクト: Fulgen301/SGGP
global func FindInArray4K(array a, v)			{return FindInArray(a, v);}
コード例 #2
0
int SrvrSaveWorldToFile( FILE *pFile, INDEX iWorld )
{
	PWORLD pWorld = GetSetMember( WORLD, &g.worlds, iWorld );
	int sz = 0, tmp, cnt;
	int worldpos;
	int linesize, linepos;
	int nlines;
	PFLATLAND_MYLINESEG *linearray;
	int wallsize, wallpos;
	int nwalls;
	PWALL *wallarray;
	int sectorsize, sectorpos;
	int nsectors;
	PSECTOR *sectorarray;
	int namesize, namepos;
	int nnames;
	PNAME *namearray;
	int texturesize, texturepos;
	int ntextures;
	PFLATLAND_TEXTURE *texturearray;

#ifdef LOG_SAVETIMING
	uint32_t begin, start = GetTickCount();
	begin = start;
#endif
	linearray   = GetLinearLineArray( pWorld->lines, &nlines );
	wallarray   = GetLinearWallArray( pWorld->walls, &nwalls );
	sectorarray = GetLinearSectorArray( pWorld->sectors, &nsectors );
	namearray   = GetLinearNameArray( pWorld->names, &nnames );
	texturearray = GetLinearTextureArray( pWorld->textures, &ntextures );

#ifdef LOG_SAVETIMING
	Log1( "Built arrays: %d", GetTickCount() - start );
	start = GetTickCount();
#endif
	fwrite( "FLAT", 1, 4, pFile );
	//-------------------------------------------------
	// Version 0: did not save "FLAT" tag - assumes version 1 info
	// Version 1: had extra nInto - sector reference for wall links...
	// Version 2: didn't compute sector origins, need to fix 
	// Version 3: lacked having sector ID saved
	// Version 4: lacked a text name per sector
	//   new section introduced - NAME ... referenced name sets
	//   for (walls?) and sectors.
	// version 5: lacked a count of lines in names - assume one line. 
	// Version 6: added flags to name type
	// version 7: (see version 8 notes for changes from 7)
	// version 8: Added Texture information (default texture for prior versions)
	//            internal added sector point sets (computed on load)
	//            internal added line reference count (computed on load)
	//            added current grid sizes/ grid color (saved in flatland)
	// version 9: flipped image upside down so +Y is 'top' and -Y is 'low'
	//            any version less than this must mult Y by -1 on load.
	// version 10: going to add sizes per section(added), and consider
	//            splitting off 'section' handlers keyed on 
	//            section names - will need coordination though :/ .. not added
	// (current - no changes to save or load)
	// version 11: 
	//-------------------------------------------------
	tmp = CURRENTSAVEVERSION;  

	// writesize( pWorld )
	worldpos = ftell( pFile );
	fwrite( &sz, 1, sizeof( sz ), pFile ); 
	sz = 0;

	Log1( "Saving version %d", tmp );
	sz += fwrite( &tmp, 1, sizeof( tmp ), pFile );

	//----- write lines -------
	sz += fwrite( "LINE", 1, 4, pFile );
	WriteSize( line );
	linesize += fwrite( &nlines, 1, sizeof(nlines), pFile );
	for( cnt = 0; cnt < nlines; cnt++ )
	{
		LINESEGFILE lsf;
      SetRay( &lsf.r, &linearray[cnt]->r );
      lsf.start = linearray[cnt]->dFrom;
      lsf.end   = linearray[cnt]->dTo;
		linesize += fwrite( &lsf, 1, sizeof( LINESEGFILE ), pFile );
	}
	UpdateSize( line );
#ifdef LOG_SAVETIMING
	Log1( "Wrote Lines: %d", GetTickCount() - start );
	start = GetTickCount();
#endif
	sz += fwrite( "WALL", 1, 4, pFile );
	WriteSize( wall );
	wallsize += fwrite( &nwalls, 1, sizeof(nwalls), pFile );
	for( cnt = 0; cnt < nwalls; cnt++ )
	{
		FILEWALLV2 WriteWall;
		PWALL pwall = wallarray[cnt];
		WriteWall.flags = pwall->flags;

		tmp = WriteWall.nSector = FindInArray( (POINTER*)sectorarray, nsectors, GetUsedSetMember( SECTOR, &pWorld->sectors, pwall->iSector ) );
		if( tmp < 0 )
			Log1( "Failed to find referenced sector... save will fail %d", cnt );

		tmp = WriteWall.nLine = FindInArray( (POINTER*)linearray, nlines, GetUsedSetMember( FLATLAND_MYLINESEG, &pWorld->lines, pwall->iLine ) );
		if( tmp < 0 )
			Log1( "Failed to find referenced line... save will fail %d", cnt );

		if( pwall->iWallInto != INVALID_INDEX )
		{
			tmp = WriteWall.nWallInto = FindInArray( (POINTER*)wallarray, nwalls, GetUsedSetMember( WALL, &pWorld->walls, pwall->iWallInto ) );
			if( tmp < 0 )
				Log1( "Failed to find referenced wall into... save will fail %d", cnt );
		}
		else
			WriteWall.nWallInto = -1;

		tmp = WriteWall.nWallStart = FindInArray( (POINTER*)wallarray, nwalls, GetUsedSetMember( WALL, &pWorld->walls, pwall->iWallStart ) );
		if( tmp < 0 )
			Log1( "Failed to find referenced starting wall... save will fail %d", cnt );

		tmp = WriteWall.nWallEnd = FindInArray( (POINTER*)wallarray, nwalls, GetUsedSetMember( WALL, &pWorld->walls, pwall->iWallEnd ) );
		if( tmp < 0 )
			Log1( "Failed to find referenced ending wall... save will fail %d", cnt );

		wallsize += fwrite( &WriteWall, 1, sizeof( WriteWall ), pFile );	
	}
	UpdateSize( wall );
#ifdef LOG_SAVETIMING
	Log1( "Wrote Walls: %d", GetTickCount() - start );
	start = GetTickCount();
#endif
	sz += fwrite( "SECT", 1, 4, pFile );
	WriteSize( sector );
	sectorsize += fwrite( &nsectors, 1, sizeof( nsectors ), pFile );
	for( cnt = 0; cnt < nsectors; cnt++ )
	{
		FILESECTORV8 WriteSector;
		WriteSector.nName = FindInArray( (POINTER*)namearray, nnames, GetUsedSetMember( NAME, &pWorld->names, sectorarray[cnt]->iName ) );
		//WriteSector.nID = sectorarray[cnt]->nID;
		WriteSector.flags = sectorarray[cnt]->flags;
		SetRay( &WriteSector.r, &sectorarray[cnt]->r );
		tmp = WriteSector.nwall = FindInArray( (POINTER*)wallarray, nwalls, GetUsedSetMember( WALL, &pWorld->walls, sectorarray[cnt]->iWall ) );
		if( tmp < 0 )
			Log1( "Failed to find wall referenced by sector.. save failing %d", cnt );
		WriteSector.nTexture = FindInArray( (POINTER*)texturearray, ntextures, GetUsedSetMember( FLATLAND_TEXTURE, &pWorld->textures, sectorarray[cnt]->iTexture ) );
		if( tmp < 0 )
			Log1( "Failed to find texture referenced by sector.. save failing %d", cnt );
		sectorsize += fwrite( &WriteSector, 1, sizeof( WriteSector ), pFile );
	}
	UpdateSize( sector );
#ifdef LOG_SAVETIMING
	Log1( "Wrote Sectors: %d", GetTickCount() - start );
	start = GetTickCount();
#endif
	sz += fwrite( "NAME", 1, 4, pFile );
	WriteSize( name );
	namesize += fwrite( &nnames, 1, sizeof( nnames ), pFile );
	for( cnt = 0; cnt < nnames; cnt++ )
	{
		int l;
		uint16_t lines = namearray[cnt]->lines;
		namesize += fwrite( &namearray[cnt]->flags, 1, sizeof( namearray[cnt]->flags ), pFile );
		namesize += fwrite( &lines, 1, sizeof( lines ), pFile );
		for( l = 0; l < lines; l++ )
		{
			namesize += fwrite( &namearray[cnt]->name[l].length, 1, sizeof( namearray[cnt]->name[l].length ), pFile );
			namesize += fwrite( namearray[cnt]->name[l].name, 1, namearray[cnt]->name[l].length, pFile );
		}
	}
	UpdateSize( name );
#ifdef LOG_SAVETIMING
	Log1( "Wrote Names: %d", GetTickCount() - start );
	start = GetTickCount();
#endif
	sz += fwrite( "TEXT", 1, 4, pFile );

	WriteSize( texture );

	texturesize += fwrite( &ntextures, 1, sizeof( ntextures ), pFile );
	for( cnt = 0; cnt < ntextures; cnt++ )
	{
		char flag;
		int nName = FindInArray( (POINTER*)namearray, nnames, GetUsedSetMember( NAME, &pWorld->names, texturearray[cnt]->iName ) );
		texturesize += fwrite( &nName, 1, sizeof( nName ), pFile );
		flag = texturearray[cnt]->flags.bColor;
		texturesize += fwrite( &flag, 1, sizeof( flag ), pFile );
		// if flag is set - write cdata 
		if( flag )
		{
			texturesize += fwrite( &texturearray[cnt]->data.color, 1, sizeof( CDATA ), pFile );
		}				
	}

	UpdateSize( texture );

#ifdef LOG_SAVETIMING
	Log1( "Wrote Textures: %d", GetTickCount() - start );
	start = GetTickCount();
#endif
	{
		int endpos;
		endpos = ftell( pFile );
		fseek( pFile, worldpos, SEEK_SET );
		fwrite( &sz, 1, sizeof( sz ), pFile );
		fseek( pFile, endpos, SEEK_SET );
	}

#ifdef LOG_SAVETIMING
	Log1( "Wrote File: %d", GetTickCount() - begin );
#endif

	Release( texturearray );
	Release( namearray );
	Release( sectorarray );
	Release( wallarray );
	Release( linearray );
#ifdef LOG_SAVETIMING
	Log1( "Released arrays: %d", GetTickCount() - start );
#endif

	return sz;
}
コード例 #3
0
/*
   GoodAddresses method for the DNS Provider Test Client

   Checks the specified values against the expected array values
   and returns TRUE if the same, else FALSE

   Returns "nameserver" field from /etc/resolv.conf file
*/
Boolean 
DNSTestClient::goodAddresses(const Array<String> &addr, 
                             Boolean verbose)
{
	FILE *arq;
	int i, j, ind = 0, count = 0;
    char *ptr;
    char buffer[150];
    Boolean ok = false;
    Array<String> addrlist;
    
    // open resolv.conf file to retrieve informations
    if((arq = fopen(RESOLV_CONF.getCString(), "r")) == NULL)
    	return ok;
    
	// retrieve addresses list
    while(!feof(arq)) {
    	memset(buffer, 0, sizeof(buffer));
    	fscanf(arq, "%s", buffer);

    	if(!strlen(buffer))
    		continue;

		// Verify if key is search
		if(IsEqual(buffer, "domain")) {
            ind = DOMAINNAME;
			continue;
        }
        else if(IsEqual(buffer, "search")) {
            ind = SEARCHLIST;
			continue;
        }
    	else if(IsEqual(buffer, "nameserver")) {
    		ind = ADDRESSES;
    		continue;
    	}
    	else if(ind == ADDRESSES) {
    		if(verbose)
    			cout << "Reading " << buffer << endl;
    		
			if(!FindInArray(addrlist, String(buffer)))
				addrlist.append(String(buffer));
		}
		else
			continue;
    }
    fclose(arq);

    if (verbose) {
    	for(i=0; i<addrlist.size(); i++)
      		cout<<"Checking " << addrlist[i] << " against Addresses "<<endl;
    }
	
	// List all search list
	if (verbose) {
		cout<<"Checking Addresses " << endl;
		for(i = 0; i < addr.size(); i++)
      		cout << addr[i] << endl;
    }

	if(addrlist.size() == 0)
        // Addresses list array not exists
		ok = true;
	else if(addr.size() == addrlist.size()) {
		if(verbose)
			cout <<"Searching address list " << endl;
		for(i=0; i<addr.size(); i++) {
			for(j=0; j<addrlist.size(); j++) {
				if(addrlist[j] == addr[i]) 
					count++;
			}
		}
		if(count == addr.size())
            // Addresses list array is ok
			ok = true;
	}
   	return ok;
}
コード例 #4
0
// Read domain name, addresses e search list from /etc/resolv.conf
Boolean
DNSService::getDNSInfo()
{
#ifdef DNSPROVIDER_DEBUG
    cout << "DNSService::getDNSInfo()" << endl;
#endif

    FILE *fp;
    int count = 0, ind = 0;
    char buffer[512];
    Boolean ok = false;
    String strBuffer;
    // Open file DNS Configuration File
    if ((fp = fopen(DNS_FILE_CONFIG.getCString(), "r")) == NULL)
    {
        throw CIMOperationFailedException(
            "DNSService: can't open configuration file.");
    }

    // Check configuration file contents.
    while (!feof(fp))
    {
        memset(buffer, 0, sizeof(buffer));
        fscanf(fp, "%511s", buffer);
        strBuffer.assign(buffer);

        // Verify if keys exist
        if(String::equalNoCase(strBuffer, DNS_ROLE_DOMAIN) ||
           String::equalNoCase(strBuffer, DNS_ROLE_SEARCH) ||
           String::equalNoCase(strBuffer, DNS_ROLE_NAMESERVER) )
             count++;
        //A minimum of one key should exist
        if (count >= 1)
        {
            ok = true;
            break;
        }
    }
    if(!ok)
    {
        fclose(fp);
        throw CIMOperationFailedException
                  ("DNSService: configuration file is corrupt.");
    }

    // Clear all attributes
    rewind(fp);
    dnsName.clear();
    dnsSearchList.clear();
    dnsAddresses.clear();

    // Retrieve DNS informations from file
    while(!feof(fp)) {
        memset(buffer, 0, sizeof(buffer));
        fscanf(fp, "%511s", buffer);
        if(!strlen(buffer))
            continue; 
        if( buffer[0] == '#' )
        {
            // If it is a comment line then read the whole line and continue
            fgets(buffer,512,fp);
            continue;
        }
        strBuffer.assign(buffer);

        // Verify if key is domain name
        if(String::equalNoCase(strBuffer, DNS_ROLE_DOMAIN)) {
            fscanf(fp, "%511s", buffer);
            dnsName.assign(buffer);
#ifdef DNSPROVIDER_DEBUG
    cout << "DNSService::getDNSInfo() - buffer = `" << buffer << "'" << endl;
    cout << "DNSService::getDNSInfo() - dnsName = `" <<
         dnsName.getCString() << endl;
#endif
        }
        else
        {
            // Verify if key is search list
            if(String::equalNoCase(strBuffer, DNS_ROLE_SEARCH)) {
                ind = SEARCHLIST;
                continue;
            }
            // Verify if key is address (DNS server)
            else if(String::equalNoCase(strBuffer, DNS_ROLE_NAMESERVER)) {
                ind = ADDRESSES;
                continue;
            }
            else
            {
                switch (ind) 
                {
                    case SEARCHLIST:
                        // Make sure not to add multiple identical entries
                        if (!FindInArray(dnsSearchList, strBuffer))
                        {
                            dnsSearchList.append(strBuffer);
                            // if there's not already a Domain Name, use
                            // the first Search entry.
                            if (dnsName.size() == 0)
                                dnsName.assign(strBuffer);
                        }
                        break;

                    case ADDRESSES:
                        if(!FindInArray(dnsAddresses, String(buffer)))
                            dnsAddresses.append(strBuffer);
                        break;

                    default:
                        ok = false;
                        break;
                }
            }
        }
        if(!ok)
            break;
    }
    fclose(fp);
    return ok;
}
コード例 #5
0
ファイル: templateparser.cpp プロジェクト: joyfish/os45
bool CTemplateParserApp::GetFileNames(SgmlElement *pFile, CString &csSourceFile, 
                                      CString &csTargetFile, bool bTemplate)
{
   bool success = true;

   const char *szFile = pFile->GetParameter();
   const char *szPrefix = pFile->GetValue("prefix");
   const char *szWriteToRoot = pFile->GetValue("writeToRoot");
   /*
   if (szPrefix && szWriteToRoot)
      printf("<file>: %s, prefix: '%s', writeToRoot: '%s'\n", szFile, szPrefix, szWriteToRoot);
   else if (szPrefix)
      printf("<file>: %s, prefix: '%s'\n", szFile, szPrefix);
   else if (szWriteToRoot)
      printf("<file>: %s, writeToRoot: '%s'\n", szFile, szWriteToRoot);
   */

   bool bWriteToRoot = false;
   if (szWriteToRoot)
      bWriteToRoot = (stricmp("true", szWriteToRoot) == 0);

   bool bHasPrefix = false;
   CString csPrefix;
   if (szPrefix)
   {
      // The prefix may contain keywords
      csPrefix = szPrefix;
      LTextBuffer ltbPrefix(csPrefix);
      ReplaceAllKeywords(ltbPrefix);
      csPrefix = ltbPrefix.GetString();
      bHasPrefix = true;
   }
   
   if (szFile)
   {
      CString csSrcFile;
      csSrcFile = szFile;
      // The source file may contain keywords
      LTextBuffer ltbSrcFile(csSrcFile);
      ReplaceAllKeywords(ltbSrcFile);
      csSrcFile = ltbSrcFile.GetString();

      csSrcFile.Replace('/', '\\');
      int nLastSlash = csSrcFile.ReverseFind('\\');
      CString csFileName = csSrcFile;
      if (nLastSlash > 0)
      {
         // Has directory/ies
         CString csDirs = csSrcFile.Mid(0, nLastSlash);
         csFileName = csSrcFile.Mid(nLastSlash + 1);
         if (!bWriteToRoot)
         {
            // TODO: this "look if the path is already in the array" algorithm
            // is O(n^2)!

            CString csMarker("\\");

            // have a look if the path is more than one (new) diretory deep
            int nFirstSlash = csDirs.Find('\\');
            while (nFirstSlash > 0)
            {
               CString csDirSub = csDirs.Mid(0, nFirstSlash);
               CString csFullPath = m_csTargetPath + csDirSub + csMarker;
               if (FindInArray(m_writtenFiles, csFullPath) < 0)
               {
                  m_writtenFiles.Add(csFullPath);
               }
               nFirstSlash = csDirs.Find('\\', nFirstSlash+1);
            }

            // the directory itself
            CString csFullPath = m_csTargetPath + csDirs + csMarker;
            if (FindInArray(m_writtenFiles, csFullPath) < 0)
            {
               m_writtenFiles.Add(csFullPath);
            }


            // this is the "meat line":
            success = LIo::CreateDirs(m_csTargetPath, csDirs);
            if (!success)
            {
               // TPERR_CREATE_DIR
               // config.xml: Beim Erstellen eines Verzeichnisses in '%s' ist ein Fehler aufgetreten.\nIst das Verzeichnis schreibgeschützt?
               // config.xml: Creating a directory in '%s' failed.\nIs the directory write protected?
               MakeErrorMessage(TPERR_CREATE_DIR, m_csTargetPath);
            }
         }

         if (success)
         {
            if (bHasPrefix)
            {
               CString csTmp = csPrefix;

               if (bTemplate)
               {
                  // In case of "index" as a file name, remove "index"
                  if (csFileName.Mid(0, 5) == _T("index"))
                     csFileName = csFileName.Mid(5);
               }

               csTmp += csFileName;
               if (bWriteToRoot)
                  csFileName = csTmp;
               else
                  csFileName = csDirs + CString(_T("\\")) + csTmp;
            }
            else
            {
               CString csTmp = csFileName;
               if (bWriteToRoot)
                  csFileName = csTmp;
               else
                  csFileName = csDirs + CString(_T("\\")) + csTmp;
            }
         }
      }
      
      if (success)
      {
         if (bTemplate)
         {
            // If the file name ends with ".tmpl", remove that
            if (csFileName.Mid(csFileName.GetLength() - 5) == _T(".tmpl"))
               csFileName = csFileName.Mid(0, csFileName.GetLength() - 5);
         }

         csSourceFile = m_csSourcePath + csSrcFile;
         csTargetFile = m_csTargetPath + csFileName;

         // printf("%s --> %s\n", csSourceFile, csTargetFile);
      }
   }
   else
   {
      success = false;
      // TPERR_READ_FILES
      // config.xml: Beim Auslesen der zu verarbeitenden Dateien ist ein Fehler aufgetreten.
      // config.xml: An error occurred while reading which files to handle.
      MakeErrorMessage(TPERR_READ_FILES);
   }

   return success;
}