示例#1
0
int			network_listen(struct timeval *timeout)
{
  fd_set		set;
  int			ret;

  flush_list();
  setfd(&set);
  if (timeout)
    logger_debug("[NETWORK] waiting for next action in %lu:%lu",
	   timeout->tv_sec,
	   timeout->tv_usec);
  else
    logger_debug("[NETWORK] waiting for extern action. nothing planified");
  if ((ret = (select(g_network->nfds, &set, NULL, NULL, timeout))) > 0)
    {
      find_speaker(&set,
		   g_network->read,
		   &extract_from_socket,
		   &execute_from_socket);
      find_speaker(&set,
		   g_network->listened,
		   &extract_from_listener,
		   &execute_from_listener);
    }
  g_network->nfds = 0;
  return (ret);
}
示例#2
0
文件: solve.c 项目: awhillas/comp9021
int main(int argc, char * argv[]) {

    if (argc == 1)
        return EXIT_SUCCESS;

    printf("Number of words: %d\n", argc - 1);

    // Clean the input a bit. and count sentences.
    
    int nb_of_sentences = 0;
    int nb_of_sir_occurances = 0;
    for (int i = 1; i < argc; i++) {
        int length = strlen(argv[i]);
        if(argv[i][length - 1] == '"' && ispunct(argv[i][length - 2])) {
            argv[i][length - 1] = argv[i][length - 2];
            argv[i][length - 2] = '"';
        }
        // Count sentences
        char last = argv[i][length - 1];
        if (last == '.' || last == '!' || last == '?')
            nb_of_sentences++; 
        // Count Sir occurances (double counts).
        if(strcmp(argv[i], "Sir") == 0 || strcmp(argv[i], "\"Sir") == 0)
            nb_of_sir_occurances++;
        if(strcmp(argv[i], "Sirs") == 0 || strcmp(argv[i], "\"Sirs") == 0)
            nb_of_sir_occurances += count_sirs(i + 1, argv);
    }
    printf("Number of Sentences: %d\n", nb_of_sentences);
    printf("Number of occurences of Sirs: %d\n", nb_of_sir_occurances);
    
    // Sentences
    
    int end_of_sentences[nb_of_sentences];
    int current_sentence = 0;
    for (int i = 1; i < argc; i++) {
        int length = strlen(argv[i]);
        char last = argv[i][length - 1];
        if (last == '.' || last == '!' || last == '?')
            end_of_sentences[current_sentence++] = i; 
    }
    
    // Sirs
    
    int sir_occurances[nb_of_sir_occurances];
    int current_sir = 0;
    for (int i = 1; i < argc; i++) {
        if(strcmp(argv[i], "Sir") == 0 || strcmp(argv[i], "\"Sir") == 0)
            sir_occurances[current_sir++] = i + 1;
        if(strcmp(argv[i], "Sirs") == 0 || strcmp(argv[i], "\"Sirs") == 0)
            while(current_sir <= nb_of_sir_occurances) {
                i++;
                if(strcmp(argv[i], "and") != 0 && strcmp(argv[i], "or") != 0)
                    sir_occurances[current_sir++] = i;
                else {
                    if (strcmp(argv[i + 1], "I") != 0) 
                        sir_occurances[current_sir++] = i + 1;
                    break;
                }
            }
    }
    // Trim off punctuation from end of Sir names:
    for (int i = 0; i < nb_of_sir_occurances; i++) {
        int word = sir_occurances[i];
        int length = strlen(argv[word]);
        if(ispunct(argv[word][length - 1]))
            argv[word][length - 1] = '\0';
    }
    // Count distinct Sirs:
    char * sirs[nb_of_sir_occurances]; 
    // ...and if we know this we can ignore the excess spaces.
    int nb_of_sirs = 0; 
    for(int i = 0; i < nb_of_sir_occurances; i++) {
        int current_sir = sir_occurances[i];
        bool duplicate = false;
        // Check if the current Sir has occured previously...
        for(int j = 0; j < i; j++) {
            int previous_sir = sir_occurances[j];
            if(strcmp(argv[current_sir], argv[previous_sir]) == 0) {
                duplicate = true;
                break;
            }
        }
        if(!duplicate)
            sirs[nb_of_sirs++] = argv[current_sir];
    }
    // Sort the Sirs
    qsort(sirs, nb_of_sirs, sizeof(char *), cmpstr);

    // Find Sir Statments
    
    Sentence sentences[nb_of_sentences];   // Sentences data.
    int current_begin = 1;  // coz argv inclues the program name at 0
    for (int s = 0; s < nb_of_sentences; s++) {
        Sentence current_s = {current_begin, end_of_sentences[s], -1, -1, -1, -1, -1, false, 0};
        for (int w = current_begin; w <= end_of_sentences[s]; w++) {
            // Look for quotes...
            if(argv[w][0] == '"') {
                current_s.open = w++;
                do {
                    int length = strlen(argv[w]);
                    for (int c = 0; c < length; c++)
                        if(argv[w][c] == '"') {
                            current_s.close = w;
                            break;
                        }
                    if(current_s.close != -1) {
                        // Remove punctuation and move on...
                        if(ispunct(argv[w][length - 1]))
                            argv[w][length - 1] = '\0';
                        break;
                    }
                } while (++w <= end_of_sentences[s]); 
            }
        }
        current_begin = end_of_sentences[s] + 1;
        sentences[s] = current_s;
    }
    // Look for Sir who is speaking.
    for (int i = 0; i < nb_of_sentences; i++) {
        Sentence * s = &sentences[i];
        if (s->open != -1) {
            int speaker_i = find_speaker(argv, *s);
            if(speaker_i != -1)
                s->speaker = find_index(argv[speaker_i], sirs, nb_of_sirs);
            // match phrase to one of the 8 templates.
            match_phrase(s, argv);
            find_sirs(argv, s, sirs, nb_of_sirs);
        }
    }
    
    
    
    
    // Output testing.

    for (int i = 1; i < argc; i++){
        printf("%s ", argv[i]);
        if(in_array(i, end_of_sentences, nb_of_sentences))
            printf("\n");
    }
    printf("The Sirs are:");
    print_array(sirs, nb_of_sirs);
    printf("\n");
}