Exemplo n.º 1
0
bool cConfigLoader::ParseFile (std::string filename)
{
	std::string line, name, value;

	// Open the file in the filemanager
	cFileManager *fmanager = cFileManager::Singleton();
	cIFile *file = fmanager->OpenFile(filename);

	// If the file doesn't exist, error out
	if (!file)
		return false;

	do
	{
		line = StripComments (file->GetLine());

		//  Add config name and value to map
		if (line.size() != 0)
		{
			name = line.substr(0,line.find_first_of(" "));
			value = line.substr(line.find_first_of(" ")+1, line.length());
			m_values.insert ( std::make_pair ( std::string(name), std::string(value)) );
		}
	}
	while (!file->Eof ());
	
	// Close the file in the filemanager
 	fmanager->CloseFile(file);

	return true;
}
Exemplo n.º 2
0
void MicroDVDSubtitleFormat::WriteFile(const AssFile *src, agi::fs::path const& filename, std::string const& encoding) const {
	agi::vfr::Framerate fps = AskForFPS(true, false);
	if (!fps.IsLoaded()) return;

	AssFile copy(*src);
	copy.Sort();
	StripComments(copy);
	RecombineOverlaps(copy);
	MergeIdentical(copy);
	StripTags(copy);
	ConvertNewlines(copy, "|");

	TextFileWriter file(filename, encoding);

	// Write FPS line
	if (!fps.IsVFR())
		file.WriteLineToFile(str(boost::format("{1}{1}%.6f") % fps.FPS()));

	// Write lines
	for (auto current : copy.Line | agi::of_type<AssDialogue>()) {
		int start = fps.FrameAtTime(current->Start, agi::vfr::START);
		int end = fps.FrameAtTime(current->End, agi::vfr::END);

		file.WriteLineToFile(str(boost::format("{%i}{%i}%s") % start % end % boost::replace_all_copy(current->Text.get(), "\\N", "|")));
	}
}
void MicroDVDSubtitleFormat::WriteFile(const AssFile *src, wxString const& filename, wxString const& encoding) const {
	agi::vfr::Framerate fps = AskForFPS(true, false);
	if (!fps.IsLoaded()) return;

	AssFile copy(*src);
	copy.Sort();
	StripComments(copy.Line);
	RecombineOverlaps(copy.Line);
	MergeIdentical(copy.Line);
	StripTags(copy.Line);
	ConvertNewlines(copy.Line, "|");

	TextFileWriter file(filename, encoding);

	// Write FPS line
	if (!fps.IsVFR()) {
		file.WriteLineToFile(wxString::Format("{1}{1}%.6f", fps.FPS()));
	}

	// Write lines
	for (LineList::const_iterator cur = copy.Line.begin(); cur != copy.Line.end(); ++cur) {
		if (AssDialogue *current = dynamic_cast<AssDialogue*>(*cur)) {
			int start = fps.FrameAtTime(current->Start, agi::vfr::START);
			int end = fps.FrameAtTime(current->End, agi::vfr::END);

			file.WriteLineToFile(wxString::Format("{%i}{%i}%s", start, end, current->Text));
		}
	}
}
void EncoreSubtitleFormat::WriteFile(const AssFile *src, wxString const& filename, wxString const&) const {
	agi::vfr::Framerate fps = AskForFPS(false, true);
	if (!fps.IsLoaded()) return;

	// Convert to encore
	AssFile copy(*src);
	copy.Sort();
	StripComments(copy.Line);
	RecombineOverlaps(copy.Line);
	MergeIdentical(copy.Line);
	StripTags(copy.Line);
	ConvertNewlines(copy.Line, "\r\n");


	// Encode wants ; for NTSC and : for PAL
	// The manual suggests no other frame rates are supported
	char sep = fps.NeedsDropFrames() ? ';' : ':';
	SmpteFormatter ft(fps, sep);

	// Write lines
	int i = 0;
	TextFileWriter file(filename, "UTF-8");
	for (LineList::const_iterator cur = copy.Line.begin(); cur != copy.Line.end(); ++cur) {
		if (AssDialogue *current = dynamic_cast<AssDialogue*>(*cur)) {
			++i;
			file.WriteLineToFile(wxString::Format("%i %s %s %s", i, ft.ToSMPTE(current->Start), ft.ToSMPTE(current->End), current->Text));
		}
	}
}
Exemplo n.º 5
0
//---------------------------------------------------------------------------------
// Purpose: Take a string, remove carriage return, remove comments, strips leading
//          and ending spaces
//---------------------------------------------------------------------------------
bool ParseLine(char *in, bool strip_comments, bool strip_start_comments)
{
	if (!in) return false;

	StripBOM(in);

	if ( strip_comments )
	{
		if (!StripComments(in)) return false;
	}
	else if (strip_start_comments)
	{
		if (!StripComments(in, true)) return false;
	}
	
	if (!StripEOL(in)) return false;

	if (!Trim(in)) return false;

	return true;
}
Exemplo n.º 6
0
//---------------------------------------------------------------------------------
// Purpose: Take a string, remove carriage return, remove comments, strips leading
//          and ending spaces and make a ban entry.
//
// Format of string is 
// ID int time_t "initiator" "reason" //comments here
//---------------------------------------------------------------------------------
bool ParseBanLine( char *in, ban_settings_t *banned_user, bool strip_comments, bool strip_start_comments ) {
	int length;

	if (!in) return false;

	StripBOM(in);
	
	if ( strip_comments )
	{
		if (!StripComments(in)) return false;
	}
	else if (strip_start_comments) 
	{
		if (!StripComments(in, true)) return false;
	}
	
	if (!StripEOL(in)) return false;

	if (!Trim(in)) return false;

	length = Q_strlen(in);
	if (length == 0) return false;
	
	args_list_size = GetArgs(in);

	if ( args_list_size < 4 )
		return false; // need at least 4 arguments ID, TIME, and PLAYER_NAME, INITIATOR ... Reason isn't manditory.

	Q_strcpy (banned_user->key_id, args_list[0].arg);
	banned_user->byID = ( (args_list[0].arg[0] == 'S') || (args_list[0].arg[0] == 's') );
	banned_user->expire_time = atoi(args_list[1].arg);
	Q_strcpy ( banned_user->player_name, args_list[2].arg );
	Q_strcpy ( banned_user->ban_initiator, args_list[3].arg );
	if ( args_list_size > 4 )
		Q_strcpy ( banned_user->reason, args_list[4].arg );
	
	return true;
}
Exemplo n.º 7
0
    void LoadSourceFile()
    {
        if (! (sasm.sourceFile = fopen(sasm.sourceFileName, "rb")))
            ExitOnError("Could not open source file");
    
        while (!feof(sasm.sourceFile))
        {
            if (fgetc(sasm.sourceFile) == '\n')
                sasm.sourceCodeLines++;
        }
        sasm.sourceCodeLines++;
    
        rewind(sasm.sourceFile);
    
        if (! (sasm.sourceCode = (char**)malloc(sasm.sourceCodeLines * sizeof(char*))))
            ExitOnError("Could not allocate space for source code");
    
        for (int i = 0; i < sasm.sourceCodeLines; i++)
        {
            if (! (sasm.sourceCode[i] = (char*)malloc(MAX_SOURCE_LINE_SIZE + 1)))
                ExitOnError("Could not allocate space for source code");
    
            fgets(sasm.sourceCode[i], MAX_SOURCE_LINE_SIZE, sasm.sourceFile);
    
            StripComments(sasm.sourceCode[i]);
            TrimWhiteSpace(sasm.sourceCode[i]);
    
            // Make sure to add a new newline if it was removed by the stripping of the
            // comments and whitespace. We do this by checking the character right before
            // the null terminator to see if it's \n. If not, we move the terminator over
            // by one and add it. We use strlen () to find the position of the newline
            // easily.
    
            int newLineIdex = strlen(sasm.sourceCode[i]) - 1;
            if (sasm.sourceCode[i][newLineIdex] != '\n' )
            {
                sasm.sourceCode[i][newLineIdex + 1] = '\n';
                sasm.sourceCode[i][newLineIdex + 2] = '\0';
            }
        }

		fclose(sasm.sourceFile);
    }
Exemplo n.º 8
0
void TTXTSubtitleFormat::ConvertToTTXT(AssFile &file) const {
	file.Sort();
	StripComments(file);
	RecombineOverlaps(file);
	MergeIdentical(file);
	StripTags(file);
	ConvertNewlines(file, "\r\n");

	// Find last line
	agi::Time lastTime;
	if (!file.Events.empty())
		lastTime = file.Events.back().End;

	// Insert blank line at the end
	auto diag = new AssDialogue;
	diag->Start = lastTime;
	diag->End = lastTime+OPT_GET("Timing/Default Duration")->GetInt();
	file.Events.push_back(*diag);
}
Exemplo n.º 9
0
void TTXTSubtitleFormat::ConvertToTTXT(AssFile &file) const {
	file.Sort();
	StripComments(file);
	RecombineOverlaps(file);
	MergeIdentical(file);
	StripTags(file);
	ConvertNewlines(file, "\r\n");

	// Find last line
	AssTime lastTime;
	for (auto line : file.Line | boost::adaptors::reversed | agi::of_type<AssDialogue>()) {
		lastTime = line->End;
		break;
	}

	// Insert blank line at the end
	auto diag = new AssDialogue;
	diag->Start = lastTime;
	diag->End = lastTime+OPT_GET("Timing/Default Duration")->GetInt();
	file.Line.push_back(*diag);
}
Exemplo n.º 10
0
void EncoreSubtitleFormat::WriteFile(const AssFile *src, agi::fs::path const& filename, agi::vfr::Framerate const& video_fps, std::string const&) const {
	agi::vfr::Framerate fps = AskForFPS(false, true, video_fps);
	if (!fps.IsLoaded()) return;

	// Convert to encore
	AssFile copy(*src);
	copy.Sort();
	StripComments(copy);
	RecombineOverlaps(copy);
	MergeIdentical(copy);
	StripTags(copy);
	ConvertNewlines(copy, "\r\n");

	// Encore wants ; for NTSC and : for PAL
	// The manual suggests no other frame rates are supported
	SmpteFormatter ft(fps, fps.NeedsDropFrames() ? ";" : ":");

	// Write lines
	int i = 0;
	TextFileWriter file(filename, "UTF-8");
	for (auto const& current : copy.Events)
		file.WriteLineToFile(str(boost::format("%i %s %s %s") % ++i % ft.ToSMPTE(current.Start) % ft.ToSMPTE(current.End) % current.Text));
}
Exemplo n.º 11
0
void TTXTSubtitleFormat::ConvertToTTXT(AssFile &file) const {
	file.Sort();
	StripComments(file.Line);
	RecombineOverlaps(file.Line);
	MergeIdentical(file.Line);
	StripTags(file.Line);
	ConvertNewlines(file.Line, "\r\n");

	// Find last line
	AssTime lastTime;
	for (LineList::reverse_iterator cur = file.Line.rbegin(); cur != file.Line.rend(); ++cur) {
		if (AssDialogue *prev = dynamic_cast<AssDialogue*>(*cur)) {
			lastTime = prev->End;
			break;
		}
	}

	// Insert blank line at the end
	AssDialogue *diag = new AssDialogue;
	diag->Start = lastTime;
	diag->End = lastTime+OPT_GET("Timing/Default Duration")->GetInt();
	file.Line.push_back(diag);
}
Exemplo n.º 12
0
int LoadBase(const BYTE *pAveBuffer, DWORD dwAveBufferSize)
{
  int rCount=0;
  AVP_BaseHeader    BaseHeader;
  SPtrArray   BlockHeaderArray;
  AVP_BlockHeader * bhp;

  PtrArrayReset(&BlockHeaderArray);

//BaseHeader read

  memcpy(&BaseHeader, pAveBuffer, sizeof(BaseHeader));

  if(*(DWORD*)BaseHeader.Authenticity==AVP_MAGIC)
    memcpy(BaseHeader.Authenticity, pAveBuffer, sizeof(BaseHeader)-0x40);
  
  if(BaseHeader.Magic==AVP_MAGIC)
  {
    BYTE* nbuf=NULL;
    UINT i;
    UINT j;
    BOOL names_done;
      
//BlockHeaderTable read

    bhp = (AVP_BlockHeader*)&pAveBuffer[BaseHeader.BlockHeaderTableFO];
    for(i=0; i<BaseHeader.BlockHeaderTableSize; i++)
    {
      PtrArrayAdd(&BlockHeaderArray,bhp);
      bhp++;
    }

    names_done=0;

lab_names_done:

    for(i=0; i<BaseHeader.BlockHeaderTableSize; i++)
    {
      BYTE* buf = 0;
      DWORD npos;
      DWORD k;

      bhp=(AVP_BlockHeader*)BlockHeaderArray.pp[i];
      if(bhp->Compression) goto abort_;

      // Fix LINK16 bug
      if (bhp->CompressedSize == 0 || bhp->UncompressedSize == 0)
        continue;
      
      switch(bhp->BlockType)
      {
      case BT_NAME:
        break;
      case BT_LINK16:
        //if(!add_16bit_links)
          //continue;
      case BT_RECORD:
      case BT_LINK32:
        if(names_done)
          break;
      case BT_EDIT:
      case BT_COMMENT:
      default: continue;
      }
      
      /*BYTE* */ buf=malloc(bhp->CompressedSize);

      if(buf==NULL) goto abort_;

      memcpy(buf,&pAveBuffer[bhp->BlockFO], bhp->CompressedSize);

      if( bhp->CRC!=CalcSum(buf,bhp->CompressedSize) )
      {
        free(buf);
        goto abort_;
      }

      switch(bhp->BlockType)
      {
      case BT_NAME:
        if(names_done)
        {
          MemFileWrite( &mfNames, buf, bhp->CompressedSize );
        }
        else
        {
          if(nbuf) free(nbuf);
          nbuf=malloc(bhp->CompressedSize+1);
          memcpy(nbuf+1,buf,bhp->CompressedSize);
          nbuf[0]='\n';
          for(k=1; k<=bhp->CompressedSize; k++)
            if(nbuf[k]=='\n') nbuf[k]=0;
          free(buf);
          buf=0;
          names_done=1;
          goto lab_names_done;
        }
        break;

      case BT_RECORD:
        rCount+=bhp->NumRecords;
        npos=MemFileGetPosition(&mfNames);
        
        switch(bhp->RecordType)
        {
        case RT_MEMORY:
          {
            R_Memory* p=(R_Memory*)buf;
            for( j=0; j<bhp->NumRecords; j++,p++ )
            {
              p->NameIdx +=npos;
              if(p->LinkIdx!=(WORD)-1)  p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType];
            }
            break;
          }
        case RT_SECTOR:
          {
            R_Sector* p=(R_Sector*)buf;
            for(j=0;j<bhp->NumRecords;j++,p++)
            {
              p->NameIdx +=npos;
              if(p->LinkIdx!=(WORD)-1)  p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType];
            }
            break;
          }
        case RT_FILE:
          {
            R_File* p=(R_File*)buf;
            for(j=0;j<bhp->NumRecords;j++,p++)
            {
              p->NameIdx +=npos;
              if(p->LinkIdx!=(WORD)-1)  p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType];
            }
            break;
          }
        case RT_CA:
          {
            R_CA* p=(R_CA*)buf;
            for(j=0;j<bhp->NumRecords;j++,p++)
            {
              p->NameIdx +=npos;
              if(p->LinkIdx!=(WORD)-1)  p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType];
            }
            break;
          }
        case RT_UNPACK:
          {
            R_Unpack* p=(R_Unpack*)buf;
            for(j=0;j<bhp->NumRecords;j++,p++)
            {
              p->NameIdx +=npos;
              if(p->LinkIdx!=(WORD)-1)  p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType];
            }
            break;
          }
        case RT_EXTRACT:
          {
            R_Extract* p=(R_Extract*)buf;
            for(j=0;j<bhp->NumRecords;j++,p++)
            {
              p->NameIdx +=npos;
              if(p->LinkIdx!=(WORD)-1)  p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType];
            }
            break;
          }
        case RT_KERNEL:
          {
            R_Kernel* p=(R_Kernel*)buf;
            for(j=0;j<bhp->NumRecords;j++,p++)
            {
              if(p->LinkIdx!=(WORD)-1)  p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType];
            }
            break;
          }
        case RT_JUMP:
          {
            R_Jump* p=(R_Jump*)buf;
            for(j=0;j<bhp->NumRecords;j++,p++)
            {
              if(p->LinkIdx!=(WORD)-1)  p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType];
            }
            break;
          }
        default: break;
        }
        MemFileWrite(&mfRecordsArray[bhp->BlockType][bhp->RecordType],buf,bhp->CompressedSize);
        break;
      case BT_LINK32:
      case BT_LINK16:
        {
          BYTE* b=buf;
          UINT k;
          for(k=0; k<bhp->NumRecords; k++)
          {
            DWORD* dptr=(DWORD*)b;
            DWORD q=0;
            DWORD l=dptr[0];
            b+=l;
            l-=dptr[1];

            if(BT_LINK32==bhp->BlockType)
              StripComments(((BYTE*)dptr)+dptr[1]+8);
            MemFileWrite(&mfRecordsArray[bhp->BlockType][bhp->RecordType],&l,4);
            MemFileWrite(&mfRecordsArray[bhp->BlockType][bhp->RecordType],&q,4);
            MemFileWrite(&mfRecordsArray[bhp->BlockType][bhp->RecordType],((BYTE*)dptr)+dptr[1]+8,l-8);
          }
        }
        break;
      default:
        break;
      }
      if(buf) free(buf);
    }
    if(nbuf) free(nbuf);

    for(i=0; i<BaseHeader.BlockHeaderTableSize; i++)
    {
      bhp=(AVP_BlockHeader*)BlockHeaderArray.pp[i];
      switch(bhp->BlockType)
      {
      case BT_NAME: 
        NameIdx+=bhp->NumRecords;
        break;
      case BT_RECORD:
      case BT_LINK32:
      case BT_LINK16:
        RNumTable[bhp->BlockType][bhp->RecordType]+=bhp->NumRecords;
      default:
        break;
      }
    }
  }
abort_:;
  free(BlockHeaderArray.pp);
  return rCount;
}
Exemplo n.º 13
0
//---------------------------------------------------------------------------------
// Purpose: Take a string, remove carriage return, remove comments, strips leading
//          and ending spaces
//
// Format of string is 
// "alias name" "question" //comments here
//---------------------------------------------------------------------------------
bool ParseCommandReplace(char *in, char *alias, char *command_type, char *replacement)
{
	int	i;
	int length;
	int	end_alias_index = 0;
	bool found_alias = false;

	// Cut carriage return out
	Q_strcpy(alias,"");
	Q_strcpy(replacement,"");
	Q_strcpy(command_type,"");

	if (!in) return false;

	length = Q_strlen(in);
	if (length <= 2) return false;

	// Strip leading comments
	if ( !StripComments (in, true) ) return false;
	if (!StripEOL(in)) return false;
	if (!Trim(in)) return false;

	length = Q_strlen(in);
	if (length == 0) return false;

	// String is now devoid of end and start spaces/tabs
	// Is this an alias string ?
	if (in[0] == '\"')
	{
		// Check for end marker of alias
		for (i = 1; i < (int) Q_strlen(in); i++)
		{
			if (in[i] == '\"')
			{
				found_alias = true;
				end_alias_index = i;
				break;
			}
		}
	}
	else
	{
//		MMsg("Alias string missing !!\n");
		return false;
	}

	if (!found_alias)
	{
		// Didn't find end marker
//		MMsg("Alias string not formed correctly !!\n");
		return false;
	}


	// Copy into alias string
	for (i = 1; i < end_alias_index; i++)
	{
		alias[i-1] = in[i];
	}

	// Terminate alias string
	alias[end_alias_index - 1] = '\0';


	if (in[end_alias_index + 1] == '\0')
	{
		// Only alias string found
//		MMsg("Must have command type in string !!\n");
		return false;
	}

	// Find start of command type string
	for (;;)
	{
		end_alias_index ++;
		if (in[end_alias_index] == '\0')
		{
			// Only alias string found
//			MMsg("Must have command type in string !!\n");
			return false;
		}

		if (in[end_alias_index] != ' ' && in[end_alias_index] != '\t') break;
	}			
		
	// This should be the type string 
	command_type[0] = in[end_alias_index];
	command_type[1] = '\0';

	if (!FStrEq(command_type, "R") && // RCon
		!FStrEq(command_type, "C") && // Client
		!FStrEq(command_type, "S")) // Say
	{
//		MMsg("Invalid command\n");
		return false;
	}

	// Find start of command string
	for (;;)
	{
		end_alias_index ++;
		if (in[end_alias_index] == '\0')
		{
			// Only alias string found
//			MMsg("Must have command in string !!\n");
			return false;
		}

		if (in[end_alias_index] != ' ' && in[end_alias_index] != '\t') break;
	}	

	// Copy command into replacement string
	i = 0;
	for (;;)
	{
		replacement[i] = in[end_alias_index];
		i++;
		end_alias_index ++;
		if (in[end_alias_index] == '\0')
		{
			// End marker for question
			replacement[i] = '\0';
			break;
		}
	}

	return true;
}
Exemplo n.º 14
0
//---------------------------------------------------------------------------------
// Purpose: Take a string, remove carriage return, remove comments, strips leading
//          and ending spaces
//
// Format of string is 
// "alias name" "question" //comments here
//---------------------------------------------------------------------------------
bool ParseAliasLine3(char *in, char *alias, char *question, bool strip_comments, bool strip_start_comments)
{
	int	i;
	int length;
	int	end_alias_index = 0;
	bool found_alias = false;

	// Cut carriage return out
	Q_strcpy(alias,"");
	Q_strcpy(question,"");

	if (!in) return false;

	StripBOM(in);

	if ( strip_comments )
	{
		if (!StripComments(in)) return false;
	}
	else if (strip_start_comments) 
	{
		if (!StripComments(in, true)) return false;
	}
	
	if (!StripEOL(in)) return false;

	if (!Trim(in)) return false;

	length = Q_strlen(in);
	if (length == 0) return false;

	// String is now devoid of end and start spaces/tabs

	// Is this an alias string ?
	if (in[0] == '\"')
	{
		// Check for end marker of alias
		for (i = 1; i < (int) Q_strlen(in); i++)
		{
			if (in[i] == '\"')
			{
				found_alias = true;
				end_alias_index = i;
				break;
			}
		}
	}
	else
	{
//		MMsg("Alias string missing !!\n");
		return false;
	}

	if (!found_alias)
	{
		// Didn't find end marker
//		MMsg("Alias string not formed correctly !!\n");
		return false;
	}


	// Copy into alias string
	for (i = 1; i < end_alias_index; i++)
	{
		alias[i-1] = in[i];
	}

	// Terminate alias string
	alias[end_alias_index - 1] = '\0';


	if (in[end_alias_index + 1] == '\0')
	{
		// Only alias string found
//		MMsg("Must have question in string !!\n");
		return false;
	}

	// Find start of question string
	for (;;)
	{
		end_alias_index ++;
		if (in[end_alias_index] == '\"') break;
		if (in[end_alias_index] == '\0')
		{
			// Only alias string found
//			MMsg("Must have question in string !!\n");
			return false;
		}
	}			
		
	end_alias_index ++;
	if (in[end_alias_index] == '\0')
	{
		// Only alias string found
//		MMsg("Must have question in string !!\n");
		return false;
	}

	// Copy question into question
	i = 0;
	for (;;)
	{
		question[i] = in[end_alias_index];
		i++;
		end_alias_index ++;
		if (in[end_alias_index] == '\"')
		{
			// End marker for question
			question[i] = '\0';
			break;
		}

		if (in[end_alias_index] == '\0')
		{
			// Only alias string found
//			MMsg("Must have question in string !!\n");
			return false;
		}
	}

	return true;
}
Exemplo n.º 15
0
//---------------------------------------------------------------------------------
// Purpose: Take a string, remove carriage return, remove comments, strips leading
//          and ending spaces
//
// Format of string is 
// "alias name" command here //comments here
//---------------------------------------------------------------------------------
bool ParseAliasLine(char *in, char *alias, bool strip_comments, bool strip_start_comments)
{
	int	i;
	int length;
	int	copy_start;
	int	end_alias_index = 0;
	bool found_alias = false;

	Q_strcpy(alias,"");

	if (!in) return false;

	StripBOM(in);

	if ( strip_comments )
	{
		if (!StripComments(in)) return false;
	}
	else if (strip_start_comments) 
	{
		if (!StripComments(in, true)) return false;
	}

	if (!StripEOL(in)) return false;

	if (!Trim(in)) return false;

	length = Q_strlen(in);
	if (length == 0) return false;

	// String is now devoid of end and start spaces/tabs
	// Is this an alias string ?
	if (in[0] == '\"')
	{
		// Check for end marker of alias
		for (i = 1; i < (int) Q_strlen(in); i++)
		{
			if (in[i] == '\"')
			{
				found_alias = true;
				end_alias_index = i;
				break;
			}
		}

		if (!found_alias)
		{
			// Didn't find end marker
			Q_strcpy(alias,in);
			// Chop max length
			alias[ALIAS_LENGTH] = '\0';
			return true;
		}

		// Copy into alias string
		for (i = 1; i < end_alias_index; i++)
		{
			alias[i-1] = in[i];
		}

		// Terminate alias string
		alias[end_alias_index - 1] = '\0';

		if (in[end_alias_index + 1] == '\0')
		{
			// Only alias string found
			Q_strcpy(in, alias);
			alias[ALIAS_LENGTH] = '\0';
			return true;
		}

		length = Q_strlen(in);
		copy_start = end_alias_index + 1;

		// find start of command
		for (;;)
		{
			if (copy_start == length) return false;
			if (in[copy_start] == '\0') return false;
			if (in[copy_start] != ' ' && in[copy_start] != '\t') break;
			copy_start++;
		}

		i = 0;
		for (;;)
		{
			if (in[copy_start] == '\0')
			{
				in[i] = '\0';
				break;
			}

			in[i] = in[copy_start];
			copy_start ++;
			i ++;
		}
	}
	else
	{
		Q_strcpy(alias, in);
		alias[ALIAS_LENGTH] = '\0';
	}

	return true;
}
Exemplo n.º 16
0
void ReadData ( const char *inFilename,
                FILE *infile,
                LList<BranchPtr> &library )
{
   printf ( "Reading from file '%s'\n", inFilename );

   char line [128];
   LList<BranchPtr> *current = 0;

   while ( fgets ( line, sizeof(line), infile ) )
   {
      if ( StripComments ( line ) )
      {
         if ( line[0] == '[' )
         {
            // We have found the start of a new opening continuation

            current = &library;
            printf ( "processing %s\n", line );
         }
         else
         {
            // We have found the next move in the current continuation

            if ( !current )
            {
               fprintf ( stderr,
                         "Error:  move '%s' is not in a continuation!\n",
                         line );

               exit(1);
            }

            int len = (int) strlen(line);
            int isValid = 0;

            if ( len == 4 || (len == 5 && line[4] == '?') )
            {
               int x1 = line[0] - 'a';
               int y1 = line[1] - '1';
               int x2 = line[2] - 'a';
               int y2 = line[3] - '1';

               if ( x1 >= 0 && x1 <= 7 &&
                    y1 >= 0 && y1 <= 7 &&
                    x2 >= 0 && x2 <= 7 &&
                    y2 >= 0 && y2 <= 7 )
               {
                  isValid = 1;
                  unsigned short source = (x1+2) + (y1+2)*12;
                  unsigned short dest   = (x2+2) + (y2+2)*12;
                  unsigned short move   = (source << 8) | dest;

                  // First, see if this move is already there...
                  Branch *b = 0;

                  for ( unsigned j=0; j < current->numItems(); j++ )
                  {
                     Branch *x = (*current)[j];
                     if ( x->move == move )
                     {
                        b = x;
                        break;
                     }
                  }

                  if ( !b )
                  {
                     // This branch wasn't found, so create it!

                     b = new Branch;
                     if ( !b )
                     {
                        fprintf ( stderr, "Error: out of memory!\n" );
                        exit(1);
                     }

                     b->isBad = (line[4] == '?');
                     b->move  = move;

                     if ( current->addToBack ( b ) != DDC_SUCCESS )
                     {
                        fprintf ( stderr, "Error adding branch to node!\n" );
                        exit(1);
                     }
                  }

                  current = &(b->replies);
               }
            }

            if ( !isValid )
            {
               fprintf ( stderr,
                         "Error:  invalid move syntax: '%s'\n",
                         line );
               exit(1);
            }
         }
      }
   }
}