BOOL CBranch_patcherDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// Extra initialization here

	RECT cltRect;
	GetClientRect( &cltRect ),
	m_Display = new CRichEditCtrl();
	m_Display->Create( WS_CHILD|WS_VISIBLE|WS_BORDER|WS_HSCROLL|WS_VSCROLL|ES_AUTOHSCROLL|ES_AUTOVSCROLL|ES_MULTILINE,
					   CRect( 20, 180, cltRect.right-20, cltRect.bottom-20 ), this, 1 );

	// Initialize directories
	loadConfiguration();
	processCommandLine();
	displayTokens();

	EnteringTokens = false;
	m_SrcDirLabel = "Source Dir";
	m_TargetDirLabel = "Target Dir";
	UpdateData( false );
	((CButton*)GetDlgItem( IDC_DoPatch ))->EnableWindow( FALSE );

	return TRUE;  // return TRUE  unless you set the focus to a control
}
void CBranch_patcherDlg::OnButtonExtractTokens() 
{
	if ( ! EnteringTokens )
	{
		EnteringTokens = true;
		extractDirTokens();	
		SrcDirBackup = m_SrcDir;
		TargetDirBackup = m_DestDir;
		m_SrcDir = Token1;
		m_DestDir = Token2;
		m_SrcDirLabel = "Enter Token 1";
		m_TargetDirLabel = "Enter Token 2";
		m_Filename = "The tokens above were extracted from the directories.";
		((CButton*)GetDlgItem( IDC_ButtonExtractTokens ))->SetWindowText( "Store Tokens" );
		GetDlgItem( IDC_TopText )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_ButtonClearTokens )->EnableWindow( FALSE );
		GetDlgItem( IDC_ButtonPatch )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_ButtonPatch )->EnableWindow( FALSE );
		GetDlgItem( IDC_DoPatch )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_Group )->ShowWindow( SW_HIDE );
		UpdateData( false );
	}
	else
	{
		UpdateData( true );
		EnteringTokens = false;
		Token1 = m_SrcDir;
		Token2 = m_DestDir;
		m_SrcDirLabel = "Source Dir";
		m_TargetDirLabel = "Target Dir";
		m_SrcDir = SrcDirBackup;
		m_DestDir = TargetDirBackup;
		m_Filename = "";
		((CButton*)GetDlgItem( IDC_ButtonExtractTokens ))->SetWindowText( "Enter Tokens" );
		GetDlgItem( IDC_TopText )->ShowWindow( SW_SHOW );
		GetDlgItem( IDC_ButtonClearTokens )->EnableWindow( TRUE );
		GetDlgItem( IDC_ButtonPatch )->ShowWindow( SW_SHOW );
		GetDlgItem( IDC_ButtonPatch )->EnableWindow( TRUE );
		GetDlgItem( IDC_DoPatch )->ShowWindow( SW_SHOW );
		GetDlgItem( IDC_Group )->ShowWindow( SW_SHOW );
		displayTokens();
	}
}
void constructorTokenizer(char *filename)
{
	//TODO: As check for filename extenstion .asm
	char *strToken, *dupLine, tempStr[100], *strEndInverted;
	int multiLineComment = 0, counter, tempStrCounter;//, strFound=0, distance=0;
	int innerCounter, strConstStart, strConstEnd, strConstLen, strProcessed;
	
	file = fopen ( filename, "r" );
	filelineno = 0;
	head = current = 0;
	if ( file == NULL )
	{
      perror(filename);
    }
    /*Basic logic for tokenizing a file
     * 1. Read a line with fgets
     * 2. increment file line no
     * 3. Use strtok function to loop through all the 
     * 	  string tokens file current file read
     * 4. Look for multiline comments
     * 5. If found loop thorough string tokens and even
     * 	  fgets function till end of multiline character
     * 	  found. Imp keep updading filelineno on ever
     *    successfull read of fgets
     * 6. Then look for Single line comments if found skip the
     *    current line
     * 7. If none of the above is found, then it may be a valid
     *    jack token or combination of two or three tokens or a 
     *    string token which would need special attentiong.
     * 8. Check string lenght of strToken. If its 1 then it might
     *    be possible symbol of jack language. Add it to linked list
     *    and proceed further.
     * 9. If not it may be a combination of symbols and other jack language 
     *    constructs. Now loop through strToken string and look for occurance of 
     *    symbols, string constants quotes
     * 10.If symbol is found add it to linked list. And loop through rest of
     *    string. 
     *    
     * Add it to a linked list of tokens which 
     *    records token string, fileline no where string was found
	 * 8. 
     * 8. End
     */
    while ( fgets ( line, sizeof(line), file ) != NULL ) // read a line 
    {
		filelineno++;
		//printf("Tokens at line %d: ", filelineno);
		dupLine = strdup(line);
		for(strToken = strtok(dupLine," \t\n"); strToken != NULL; strToken = strtok(NULL, " \t\n"))
		{
			if(multiLineComment)
			{
				if(strstr(strToken,"*/")) //look for end of mulitline comment
				{
					multiLineComment = 0; //if found continue to next token in line
				}
			}
			else if(strstr(strToken,"/*")) //its a start of multiline comment
			{
				multiLineComment = 1;
				if(strstr(strToken,"*/")) //look for end of mulitline comment
				{
					multiLineComment = 0; //if found continue to next token in line
				}
			}
			else if(strstr(strToken,"//")) //single line comment found break out of for loop
			{
				break;
			}
			else // it may be a valid token or combination of symbols and tokens
			{
				if(strlen(strToken) == 1) //then it may a valid symbol add it
				{
					
					addTokenToList(strToken); //may be a need of static pointer here
				}
				else //it may contain symbols and string constant quotes "
				{
					memset(tempStr,0,100); //first clear tempString buffer
					tempStrCounter = 0;
					strProcessed = 0;
					for(counter=0;counter<strlen(strToken);counter++)
					{
						if(strToken[counter] == '{' || strToken[counter] == '}' || \
						   strToken[counter] == '(' || strToken[counter] == ')' || \
						   strToken[counter] == '[' || strToken[counter] == ']' || \
						   strToken[counter] == '.' || strToken[counter] == ',' || \
						   strToken[counter] == ';' || strToken[counter] == '+' || \
						   strToken[counter] == '-' || strToken[counter] == '*' || \
						   strToken[counter] == '/' || strToken[counter] == '&' || \
						   strToken[counter] == '|' || strToken[counter] == '<' || \
						   strToken[counter] == '>' || strToken[counter] == '=' || \
						   strToken[counter] == '~')
						   { //we just found a symbol
							   //first check if it was first character
								if(tempStrCounter > 0) //check if there was a previous
								{					   //token being extracted
									tempStr[tempStrCounter] = '\0'; //set the null indicator
									addTokenToList(tempStr);
									memset(tempStr, 0, 100); //clear tempStr buffer
									tempStrCounter = 0; //reset tempStrCounter 
								}
								tempStr[0] = strToken[counter];
								tempStr[1] = '\0';
								addTokenToList(tempStr);
								memset(tempStr, 0, 100);
							}
							else if(strToken[counter] == '\"') //logic for string constant
							{
								if(tempStrCounter > 0) //check if there was a previous
								{					   //token being extracted
									tempStr[tempStrCounter] = '\0'; //set the null indicator
									addTokenToList(tempStr);
									memset(tempStr, 0, 100); //clear tempStr buffer
									tempStrCounter = 0; //reset tempStrCounter 
								}
								strProcessed = 1;
								strConstStart = (strToken+counter) - dupLine;
								strEndInverted = strchr(line+strConstStart+1, '\"');
								if(!strEndInverted) //not found end inverted commas copy till end of line
								{					//may be a bug in this block of if code
									strcpy(tempStr, line+strConstStart);
									addTokenToList(tempStr);
									break; 
								}
								else //we found end inverted commas
								{
									strConstEnd = strEndInverted - line;
									strConstLen = strConstEnd - strConstStart - 1;
									strToken = dupLine + strConstEnd + 1;
									strncpy(tempStr, line+strConstStart, strConstLen+2); //accomadate for two inverted commas
									addTokenToList(tempStr);
									memset(tempStr, 0, 100);
									for(innerCounter=0;innerCounter<strlen(strToken);innerCounter++)
									{
										if(strToken[innerCounter] == '{' || strToken[innerCounter] == '}' || \
										strToken[innerCounter] == '(' || strToken[innerCounter] == ')' || \
										strToken[innerCounter] == '[' || strToken[innerCounter] == ']' || \
										strToken[innerCounter] == '.' || strToken[innerCounter] == ',' || \
										strToken[innerCounter] == ';' || strToken[innerCounter] == '+' || \
										strToken[innerCounter] == '-' || strToken[innerCounter] == '*' || \
										strToken[innerCounter] == '/' || strToken[innerCounter] == '&' || \
										strToken[innerCounter] == '|' || strToken[innerCounter] == '<' || \
										strToken[innerCounter] == '>' || strToken[innerCounter] == '=' || \
										strToken[innerCounter] == '~')
										{ //we found a symbol
											if(tempStrCounter > 0) //check if there was a previous
											{					   //token being extracted
												tempStr[tempStrCounter] = '\0'; //set the null indicator
												addTokenToList(tempStr);
												memset(tempStr, 0, 100); //clear tempStr buffer
												tempStrCounter = 0; //reset tempStrCounter 
											}
											tempStr[0] = strToken[innerCounter];
											tempStr[1] = '\0';
											addTokenToList(tempStr);
											memset(tempStr, 0, 100);
										}
										else
										{
											tempStr[tempStrCounter] = strToken[innerCounter];
											tempStrCounter++;
										}
									}
									if(tempStrCounter > 0) //string had no symbol or string ended with no symbols
									{					   //this block of if may be a redudant code
										tempStr[tempStrCounter] = '\0';
										printf("token: %s\n", tempStr);
										tempStrCounter = 0;
										memset(tempStr, 0, 100);
									}
									break;
								}
							} //sting constant logic ends here
							else
							{
								tempStr[tempStrCounter] = strToken[counter];
								tempStrCounter++;
							}
						}
						if(tempStrCounter > 0) //string had no symbol or string ended with no symbols
						{
							tempStr[tempStrCounter] = '\0';
							addTokenToList(tempStr);
							tempStrCounter = 0;
							memset(tempStr, 0, 100);
						}
						if(strProcessed)
						{
							strProcessed = 0;
							break;
						}
					}
				
			}
		}
	}
	displayTokens();
	current = head;
}
Exemple #4
0
/* processLine(char* line)
 * processes a line from the script file.
 * This is the main routine.
 */
static void
processLine(char* line)
{
  char**		command1;
  char**		command2;
  char			separator;
  char*			filename;
  char*			tokens[MAXTOKENS + 1];
  char**		token;
  int			tokenCount;
  int			i;
  int			ok;

  /* Break the line into tokens.
   * If the is a problem parsing, we do not continue with this line.
   */
  ok = tokenize(line, tokens, MAXTOKENS, &tokenCount);
  if(!ok)
    return;

  /* Make sure that the last token of the line is the semicolon.
   * This makes life easier later on.
   * I suspect that this means that one cannot have comments after a command.
   */
  if((tokenCount < 1) || (*tokens[tokenCount - 1] != ';'))
    {
      tokens[tokenCount] = ";";
      tokenCount++;
      tokens[tokenCount] = NULL;
    }

  /* If there is only one token (i.e., the semicolon) then we have nothing
   * to do.
   */
  if(tokenCount < 2)
    return;

  if(verbose)
    displayTokens("tokens:", tokens);

  /* Now we want to break the line into the separate command(s) and find the
   * separator between the command(s).
   */
  separator = ';';
  token = tokens;
  command1 = &token[0];
  i = 0;
  while((i < tokenCount) && !isSeparator(*token[i]))
    i++;

  if(i >= tokenCount)
    {
      error("cissh: semicolon is missing, this should not happen");
      return;
    }

  /* We've found the separator, so save it.
   * We now know where the second command begins, if there is one.
   */
  separator = *token[i];
  token[i] = NULL;
  i++;
  command2 = &token[i];

  if(verbose)
    displayTokens("command1:", command1);

  if(verbose)
    printf("separator = '%c'\n", separator);

  /* If separator is not ';', then there is a second command so we process
   * it a little.
   */
  if(separator != ';')
    {
      while((i < tokenCount) && !isSeparator(*token[i]))
	i++;
      token[i] = NULL;
      i++;

      if(verbose)  
	displayTokens("command2:", command2);
    }

  /* A sanity check.
   */
  if(i != tokenCount)
    {
      error("cissh: parsing error, probably too many commands");
      return;
    }

  /* Now, depending on the separator execute the line.
   */
  switch(separator)
    {

      /* There is a single command on this line, execute it.
       */
    case ';':
      cisshSingleCommand(command1);
      break;

      /* We have redirected input.
       * Get the input filename.
       * Make sure there is nothing following the filename.
       * Execute it.
       */
    case '<':
      filename = command2[0];

      if(command2[1] != NULL)
	error("cissh: too many tokens after a redirect");

      cisshRedirectedInput(command1, filename);
      break;

      /* We have redirected output.
       * Get the output filename.
       * Make sure there is nothing following the filename.
       * Execute it.
       */
    case '>':
      filename = command2[0];

      if(command2[1] != NULL)
	error("cissh: too many tokens after a redirect");

      cisshRedirectedOutput(command1, filename);
      break;

      /* We have a pipe.
       * Do it.
       */
    case '|':
      cisshPipe(command1, command2);
      break;

      /* For comments, we do nothing.
       * Comments should be taken care of in the parsing routine.
       */
    case '#':
	break;

	/* Hmmm... don't know what this is.
	 */
    default:
      error("cissh: unknown separator");
      break;
    }
}
void CBranch_patcherDlg::OnButtonClearTokens() 
{
	Token1 = "";
	Token2 = "";
	displayTokens();
}