Beispiel #1
0
void readAssignmnet(assignment_node *assignmentList, teacher_node *teacherList, course_node *courseList, char *s)
{
	int teacher_number;
	int course_number;
	if (sscanf(s, "A %d %d", &teacher_number, &course_number) > 0){
		teacher *myTeacher = get_teacher(teacher_number, teacherList);
		course *myCourse = get_course(course_number, courseList);
		assignment *newAssignment = create_assignment(myTeacher, myCourse);
		add_assignment(assignmentList, newAssignment);
	}
}
/* opens filename for reading, reads in each line by calling the overloaded
<< operator for the assignment class. populates the assigned_list and
completed_list with assignments from filename. */
void Assignment_Book::load(string filename) {
    int assignment_count = 0;
    Assignment assignment;
    ifstream fin(filename);
    if (!fin)
        throw std::exception("Input file not opened!");
	while (fin >> assignment) {
		if (assignment.get_status() == 0) {
			add_assignment(assignment);
			assignment_count++;
		}
		else if (assignment.get_status() == 1 || assignment.get_status() == 2) {
			add_completed_assignment(assignment);
			assignment_count++;
		
	}
        else throw std::exception("Assignment has non-standard status code.");
    }
    if (assignment_count == 0)
        cout << "No assignments to load." << endl;
    return;
}
Beispiel #3
0
void AnalyzeUsage::visit_select(ast::Select& x) {
  std::vector<Ref<code::Block>> blocks;
  unsigned idx = 0;

  for (auto& br : x.branches) {
    if (idx > 0) {
      // Enter the <anonymous> scope block
      _enter_block(*(br->condition));

      // Accept the condition ..
      br->condition->accept(*this);

      // Exit the block and forget what happened
      _exit_block(false);
    } else {
      // Accept the condition (without forgetting about it)
      br->condition->accept(*this);
    }

    // Enter the <anonymous> scope block
    _enter_block(*br->block);

    // Run the base method (which iterates over the statements)
    Visitor::visit_block(*br->block);

    // Remember the block
    blocks.push_back(_scope->top());

    // Exit the <anonymous> scope block
    _scope->exit();

    idx += 1;
  }

  if (x.else_block) {
    // Enter the <anonymous> scope block
    _enter_block(*x.else_block);

    // Run the base method (which iterates over the statements)
    Visitor::visit_block(*x.else_block);

    // Remember the block
    blocks.push_back(_scope->top());

    // Exit the <anonymous> scope block
    _scope->exit();
  }

  // Compile a list of slots that have been (possibly) assigned
  std::unordered_map<ast::Node*, code::Slot*> slots;
  std::map<ast::Node*, int> block_assign;

  for (auto& block : blocks) {
    for (auto& item : _assign[block]) {
      if (slots.find(item->context) == slots.end()) {
        slots[item->context] = item;
        block_assign[item->context] = 0;
      }

      if (block_assign[item->context] == -1) {
        // We've been previously marked as possibly assigned ..
        continue;
      }

      auto is_assigned = item->is_assigned(block);
      if (is_assigned && *is_assigned) {
        block_assign[item->context] += 1;
      } else if (!*is_assigned) {
        block_assign[item->context] = -1;
      }
    }
  }

  // Iterate through the non-local assignments (in total)
  int required = (x.branches.size() + 1);
  for (auto& item : block_assign) {
    auto slot = slots[item.first];
    slot->add_assignment(_scope->top(), item.second == required);
    _assign[_scope->top()].push_back(slot);
  }
}
Beispiel #4
0
int main(int argc, char **argv)
{
	static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ];
	struct sockaddr_can addr;
	static struct canfd_frame frame;
	static struct timeval today_tv, log_tv, last_log_tv, diff_tv;
	struct timespec sleep_ts;
	int s; /* CAN_RAW socket */
	FILE *infile = stdin;
	unsigned long gap = DEFAULT_GAP; 
	int use_timestamps = 1;
	static int verbose, opt, delay_loops;
	static unsigned long skipgap;
	static int loopback_disable = 0;
	static int infinite_loops = 0;
	static int loops = DEFAULT_LOOPS;
	int assignments; /* assignments defined on the commandline */
	int txidx;       /* sendto() interface index */
	int eof, txmtu, i, j;
	char *fret;

	while ((opt = getopt(argc, argv, "I:l:tg:s:xv?")) != -1) {
		switch (opt) {
		case 'I':
			infile = fopen(optarg, "r");
			if (!infile) {
				perror("infile");
				return 1;
			}
			break;

		case 'l':
			if (optarg[0] == 'i')
				infinite_loops = 1;
			else
				if (!(loops = atoi(optarg))) {
					fprintf(stderr, "Invalid argument for option -l !\n");
					return 1;
				}
			break;

		case 't':
			use_timestamps = 0;
			break;

		case 'g':
			gap = strtoul(optarg, NULL, 10);
			break;

		case 's':
			skipgap = strtoul(optarg, NULL, 10);
			if (skipgap < 1) {
				fprintf(stderr, "Invalid argument for option -s !\n");
				return 1;
			}
			break;

		case 'x':
			loopback_disable = 1;
			break;

		case 'v':
			verbose++;
			break;

		case '?':
		default:
			print_usage(basename(argv[0]));
			return 1;
			break;
		}
	}

	assignments = argc - optind; /* find real number of user assignments */

	if (infile == stdin) { /* no jokes with stdin */
		infinite_loops = 0;
		loops = 1;
	}

	if (verbose > 1) { /* use -v -v to see this */
		if (infinite_loops)
			printf("infinite_loops\n");
		else
			printf("%d loops\n", loops);
	}

	sleep_ts.tv_sec  =  gap / 1000;
	sleep_ts.tv_nsec = (gap % 1000) * 1000000;

	/* open socket */
	if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
		perror("socket");
		return 1;
	}

	addr.can_family  = AF_CAN;
	addr.can_ifindex = 0;

	/* disable unneeded default receive filter on this RAW socket */
	setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);

	/* try to switch the socket into CAN FD mode */
	setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));

	if (loopback_disable) {
		int loopback = 0;

		setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
			   &loopback, sizeof(loopback));
	}

	if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		perror("bind");
		return 1;
	}

	if (assignments) {
		/* add & check user assginments from commandline */
		for (i=0; i<assignments; i++) {
			if (strlen(argv[optind+i]) >= BUFSZ) {
				fprintf(stderr, "Assignment too long!\n");
				print_usage(basename(argv[0]));
				return 1;
			}
			strcpy(buf, argv[optind+i]);
			for (j=0; j<BUFSZ; j++) { /* find '=' in assignment */
				if (buf[j] == '=')
					break;
			}
			if ((j == BUFSZ) || (buf[j] != '=')) {
				fprintf(stderr, "'=' missing in assignment!\n");
				print_usage(basename(argv[0]));
				return 1;
			}
			buf[j] = 0; /* cut string in two pieces */
			if (add_assignment("user", s, &buf[0], &buf[j+1], verbose))
				return 1;
		}
	}

	while (infinite_loops || loops--) {

		if (infile != stdin)
			rewind(infile); /* for each loop */

		if (verbose > 1) /* use -v -v to see this */
			printf (">>>>>>>>> start reading file. remaining loops = %d\n", loops);

		/* read first non-comment frame from logfile */
		while ((fret = fgets(buf, BUFSZ-1, infile)) != NULL && buf[0] != '(') {
			if (strlen(buf) >= BUFSZ-2) {
				fprintf(stderr, "comment line too long for input buffer\n");
				return 1;
			}
		}

		if (!fret)
			goto out; /* nothing to read */

		eof = 0;

		if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
			   device, ascframe) != 4) {
			fprintf(stderr, "incorrect line format in logfile\n");
			return 1;
		}

		if (use_timestamps) { /* throttle sending due to logfile timestamps */

			gettimeofday(&today_tv, NULL);
			create_diff_tv(&today_tv, &diff_tv, &log_tv);
			last_log_tv = log_tv;
		}

		while (!eof) {

			while ((!use_timestamps) ||
			       (frames_to_send(&today_tv, &diff_tv, &log_tv) < 0)) {

				/* log_tv/device/ascframe are valid here */

				if (strlen(device) >= IFNAMSIZ) {
					fprintf(stderr, "log interface name '%s' too long!", device);
					return 1;
				}

				txidx = get_txidx(device); /* get ifindex for sending the frame */
 
				if ((!txidx) && (!assignments)) {
					/* ifindex not found and no user assignments */
					/* => assign this device automatically       */
					if (add_assignment("auto", s, device, device, verbose))
						return 1;
					txidx = get_txidx(device);
				}

				if (txidx == STDOUTIDX) { /* hook to print logfile lines on stdout */

					printf("%s", buf); /* print the line AS-IS without extra \n */
					fflush(stdout);

				} else if (txidx > 0) { /* only send to valid CAN devices */

					txmtu = parse_canframe(ascframe, &frame);
					if (!txmtu) {
						fprintf(stderr, "wrong CAN frame format: '%s'!", ascframe);
						return 1;
					}

					addr.can_family  = AF_CAN;
					addr.can_ifindex = txidx; /* send via this interface */
 
					if (sendto(s, &frame, txmtu, 0,	(struct sockaddr*)&addr, sizeof(addr)) != txmtu) {
						perror("sendto");
						return 1;
					}

					if (verbose) {
						printf("%s (%s) ", get_txname(device), device);

						if (txmtu == CAN_MTU)
							fprint_long_canframe(stdout, &frame, "\n", CANLIB_VIEW_INDENT_SFF, CAN_MAX_DLEN);
						else
							fprint_long_canframe(stdout, &frame, "\n", CANLIB_VIEW_INDENT_SFF, CANFD_MAX_DLEN);
					}
				}

				/* read next non-comment frame from logfile */
				while ((fret = fgets(buf, BUFSZ-1, infile)) != NULL && buf[0] != '(') {
					if (strlen(buf) >= BUFSZ-2) {
						fprintf(stderr, "comment line too long for input buffer\n");
						return 1;
					}
				}

				if (!fret) {
					eof = 1; /* this file is completely processed */
					break;
				}

				if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
					   device, ascframe) != 4) {
					fprintf(stderr, "incorrect line format in logfile\n");
					return 1;
				}

				if (use_timestamps) {
					gettimeofday(&today_tv, NULL);

					/* test for logfile timestamps jumping backwards OR      */
					/* if the user likes to skip long gaps in the timestamps */
					if ((last_log_tv.tv_sec > log_tv.tv_sec) ||
					    (skipgap && labs(last_log_tv.tv_sec - log_tv.tv_sec) > skipgap))
						create_diff_tv(&today_tv, &diff_tv, &log_tv);

					last_log_tv = log_tv;
				}

			} /* while frames_to_send ... */

			if (nanosleep(&sleep_ts, NULL))
				return 1;

			delay_loops++; /* private statistics */
			gettimeofday(&today_tv, NULL);

		} /* while (!eof) */

	} /* while (infinite_loops || loops--) */

out:

	close(s);
	fclose(infile);

	if (verbose > 1) /* use -v -v to see this */
		printf("%d delay_loops\n", delay_loops);

	return 0;
}