Exemplo n.º 1
0
//----------------------------------------
//	main
//----------------------------------------
int main(int /*argc*/, char** /*argv*/)
{
	PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION);

	ScopedLogMessage msg("RegularExpressionTest ", "start", "end");

	{
		Poco::FileOutputStream ostr(kOutputFileName);

		ostr << LeadingHTML;

		Poco::Net::HTTPStreamFactory::registerFactory();
		Poco::URI uri(kSourceFileName);
		try
		{
			std::auto_ptr<std::istream> pStr(Poco::URIStreamOpener::defaultOpener().open(uri));
			std::stringstream ss;
			Poco::StreamCopier::copyStream(*pStr.get(), ss);

			msg.Message(Poco::format(" Finding out methods/enumerations from \"%s\".", std::string(kSourceFileName)));
			Poco::RegularExpression re("<a name=\"[0-9]+\">[A-Za-z]+</a>");
			Poco::RegularExpression::Match match;
			match.offset = 0;
			while(0 != re.match(ss.str(), match.offset, match))
			{
				std::string foundStr(ss.str().substr(match.offset, match.length));

				std::string numStr;
				Poco::RegularExpression re_num("[0-9]+");
				re_num.extract(foundStr, 0, numStr);
				
				std::string nameStr;
				Poco::RegularExpression re_name("[A-Za-z]+");
				re_name.extract(foundStr, std::strlen("<a name=\"\">")+numStr.length(), nameStr);

				msg.Message(Poco::format("  found \"%s\"", nameStr));

				ostr << "<tr><td><a href='" << uri.toString() << "#" << numStr << "' target='_blank'>"
					 << nameStr << "</a></td>";

				std::string descStr;
				Poco::RegularExpression re_desc("(description\">\n<p>)(.*)(</p>)");
				Poco::RegularExpression::Match match_desc;
				if(re_desc.match(ss.str(), match.offset, match_desc))
				{
					descStr = ss.str().substr(match_desc.offset, match_desc.length);
					re_desc.subst(descStr, "$2");
					Poco::replaceInPlace(descStr, "\"", "'");
					Poco::replaceInPlace(descStr, "href='Poco.", "href='http://pocoproject.org/docs/Poco.");
				}
				ostr << "<td>" << descStr << "</td></tr>" << std::endl;

				match.offset += match.length;
			}
		}
		catch(Poco::Exception& exc)
		{
			msg.Message(exc.displayText());
		}

		ostr << TrailingHTML;
	}
	msg.Message(Poco::format(" HTML file \"%s\" created.", std::string(kOutputFileName)));

	return 0;
}
Exemplo n.º 2
0
int main() {
  Filesystem filesystem;
  char line[LINE_MAX]= "", command[WORD_MAX]= "", temp[WORD_MAX],
       arg1[WORD_MAX]= "", arg2[WORD_MAX]= "", prompt[WORD_MAX]= "%";
  int verbose= 0, length= 0, num_matched= 0, argument_error, done= 0;

  setup_memory_checking();

  printf("%s ", prompt);
  /* continue reading lines until the end of the input */
  while (!done && fgets(line, sizeof(line) - sizeof(*line), stdin) != NULL) {

    temp[0]= '\0';
    sscanf(line, "%[ \t\n]%n", temp, &length);

    if (strcmp(temp, "") != 0 && length == strlen(temp))
      printf("%s ", prompt);  /* ignore empty input lines */

      else {

        /* num_matched is the number of format specifiers that were matched */
        num_matched= sscanf(line, "%s %s %s %s", command, arg1, arg2, temp);
        if (verbose == 1)
          printf("%s", line);

        /* argument_error will be 1 if the wrong number of arguments are
           given for any command */
        argument_error= 0;

        switch(command_idx(command)) {

          case LOGOUT:
          case EXIT:
            if (num_matched == 1)
              done= 1;  /* also quit if the command entered is logout or
                           exit */
            else argument_error= 1;
            break;

          /* call mkfs() if the line began with "mkfs" with no following
             arguments */
          case MKFS:
            if (num_matched == 1)
              mkfs(&filesystem);
            else argument_error= 1;
            break;

          /* call touch() if the line began with "touch" and had one
             following argument; if touch() returns -1 print an appropriate
             error message */
          case TOUCH:
            if (num_matched != 2)
              argument_error= 1;
            else
              if (touch(&filesystem, arg1) == -1)
                printf("Missing or invalid operand.\n");
            break;

          /* call mkdir() if the line began with "mkdir" and had one
             following argument; if mkdir() returns -1 or -2 print an
             appropriate error message */
          case MKDIR:
            if (num_matched != 2)
              argument_error= 1;
            else
              switch (mkdir(&filesystem, arg1)) {
                case -1: printf("Missing or invalid operand.\n");
                         break;
                case -2: printf("Cannot create directory %s: File exists.\n",
                                arg1);
                         break;
                default: break;  /* no-op; 0 return is expected */
              }
            break;

          /* call cd() if the line began with "cd" and had one following
             argument; if cd() returns -1 or -2 print an appropriate error
             message */
          case CD:
            if (num_matched != 1 && num_matched != 2)
              argument_error= 1;
            else
              switch (cd(&filesystem, arg1)) {
                case -1: printf("%s: No such file or directory.\n", arg1);
                         break;
                case -2: printf("%s: Not a directory.\n", arg1);
                         break;
                default: break;  /* no-op; 0 return is expected */
              }
            break;

          /* call ls() if the line began with "ls" and had one following
             argument; if ls() returns -1 print an appropriate error
             message */
          case LS:
            if (num_matched != 1 && num_matched != 2)
              argument_error= 1;
            else
              if (ls(filesystem, arg1) == -1)
                printf("%s: No such file or directory.\n", arg1);
            break;

          /* call pwd() if the line began with "pwd" with no following
             arguments */
          case PWD:
            if (num_matched == 1)
              pwd(filesystem);
            else argument_error= 1;
            break;

          /* call rm() if the line began with "rm" and had one following
             argument; if rm() returns -1, -2, or -3 print an appropriate
             error message */
          case RM:
            if (num_matched != 2)
              argument_error= 1;
            else
              switch (rm(&filesystem, arg1)) {
                case -1: printf("%s: No such file or directory.\n", arg1);
                         break;
                case -2: printf("Cannot remove directory '%s'.\n", arg1);
                         break;
                case -3: printf("Missing or invalid operand.\n");
                         break;
                default: break;  /* no-op; 0 return is expected */
              }
            break;

           /* call re_name() if the line began with "rename" with two
              following arguments; if it returns an error code (-1 through
              -4) print an appopriate error message */
          case RENAME:
            if (num_matched != 3)
              argument_error= 1;
            else
              switch (re_name(&filesystem, arg1, arg2)) {
                case -1: printf("%s: No such file or directory.\n", arg1);
                         break;
                case -2: printf("Missing or invalid operand.\n");
                         break;
                case -3: printf("File or directory %s already exists.\n",
                                arg1);
                         break;
                case -4: printf("%s and %s are the same file.\n", arg1, arg2);
                         break;
                default: break;  /* no-op; 0 return is expected */
              }
            break;

           /* call rmfs() if the line began with "rmfs" with no following
              arguments */
          case RMFS:
            if (num_matched == 1)
              rmfs(&filesystem);
            else argument_error= 1;
            break;

          /* the variable verbose is set to 1 if the "set verbose" command
             is entered. */
          case SET:
            if (num_matched == 2 && strcmp(arg1, "verbose") == 0)
              verbose= 1;
            else argument_error= 1;
            break;

            /* the variable verbose is set to 0 if "unset verbose" is
               entered. */
          case UNSET:
            if (num_matched == 2 && strcmp(arg1, "verbose") == 0)
              verbose= 0;
            else argument_error= 1;
            break;

          /* error message for a command not matching one of the function
             names */
          default: printf("%s: Command not found.\n", command);
            break;
        }

        /* error message for a command with the wrong number of arguments */
        if (argument_error)
          printf("Invalid arguments.\n");

        if (verbose == 1)
          printf("\n");
        printf("%s ", prompt);

        /* sscanf() will fail if the line read is empty, in which case the
           prior command will be executed again unless it's first cleared
           out */
        arg1[0]= arg2[0]= command[0]= '\0';
      }
  }

  check_memory_leak();

  return 0;
}