/* direction: -1 for top, 1 for bottom
 * edge: 0 for top, max for bottom
 */
int findLocationEdge(int min, int max, char *matchString, int direction, int edge)
{
  if (max < min)
  {
    return -1;
  }

  int middle;
  char *stateMid;
  char *stateMidBelow;
  middle = (min + max)/2;

  location_t *location = malloc(sizeof(location_t));

  /* read in current middle to compare to matchstring */
  FILE *ifp = NULL;
  char filename [1024];
  sprintf(filename, "../../Data/Locations/location_%06d.dat", middle);
  ifp = fopen(filename, "rb");
  location = read_location(ifp);
  fclose(ifp);
  stateMid = location->state;
  free(location);

   /* catch end-case of mid being first element */
  if (middle == edge)
  {
    if (strcmp(matchString, stateMid) == 0)
    {
      return middle;
    }
    else
    {
      return -1;
    }
  }

  /* Reallocate memory for temporary location*/
  location = malloc(sizeof(location_t));
  sprintf(filename, "../../Data/Locations/location_%06d.dat", middle + direction);
  ifp = fopen(filename, "rb");
  location = read_location(ifp);
  fclose(ifp);
  stateMidBelow = location->state;
  free(location);


  if(strcmp(matchString, stateMid) < 0 || (direction == -1 && strcmp(matchString, stateMidBelow) == 0))
  {
      return findLocationEdge(min, middle - 1, matchString, direction, edge);
  }
  else if (strcmp(matchString, stateMid) > 0 || (direction == 1 && strcmp(matchString, stateMidBelow) == 0))
  {
    return findLocationEdge(middle + 1, max, matchString, direction, edge);
  }
  else
  {
    return middle;
  }
}
Example #2
0
timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
{
	const unsigned char *tzf;
	timelib_tzinfo *tmp;
	int version;

	if (seek_to_tz_position(&tzf, timezone, tzdb)) {
		tmp = timelib_tzinfo_ctor(timezone);

		version = read_preamble(&tzf, tmp);
		read_header(&tzf, tmp);
		read_transistions(&tzf, tmp);
		read_types(&tzf, tmp);
		if (version == 2) {
			skip_64bit_preamble(&tzf, tmp);
			read_64bit_header(&tzf, tmp);
			skip_64bit_transistions(&tzf, tmp);
			skip_64bit_types(&tzf, tmp);
			skip_posix_string(&tzf, tmp);
		}
		read_location(&tzf, tmp);
	} else {
		tmp = NULL;
	}

	return tmp;
}
timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
{
	const unsigned char *tzf;
	char *memmap = NULL;
	size_t maplen;
	timelib_tzinfo *tmp;

	if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
		tmp = timelib_tzinfo_ctor(timezone);

		read_preamble(&tzf, tmp);
		read_header(&tzf, tmp);
		read_transistions(&tzf, tmp);
		read_types(&tzf, tmp);

#ifdef HAVE_SYSTEM_TZDATA
		if (memmap) {
			const struct location_info *li;

			/* TZif-style - grok the location info from the system database,
			 * if possible. */

			if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
				tmp->location.comments = strdup(li->comment);
                                strncpy(tmp->location.country_code, li->code, 2);
				tmp->location.longitude = li->longitude;
				tmp->location.latitude = li->latitude;
				tmp->bc = 1;
			}
			else {
				strcpy(tmp->location.country_code, "??");
				tmp->bc = 0;
				tmp->location.comments = strdup("");
			}

			/* Now done with the mmap segment - discard it. */
			munmap(memmap, maplen);
		} else
#endif
		{
			/* PHP-style - use the embedded info. */
			read_location(&tzf, tmp);
		}
	} else {
		tmp = NULL;
	}

	return tmp;
}
Example #4
0
timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
{
	const unsigned char *tzf;
	timelib_tzinfo *tmp;

	if (seek_to_tz_position(&tzf, timezone, tzdb)) {
		tmp = timelib_tzinfo_ctor(timezone);

		read_preamble(&tzf, tmp);
		read_header(&tzf, tmp);
		read_transistions(&tzf, tmp);
		read_types(&tzf, tmp);
		read_location(&tzf, tmp);
	} else {
		tmp = NULL;
	}

	return tmp;
}
Example #5
0
/* ip inquiries.
 * ip is little endian 4-byte int.
 * note that location encoding is not converted, which will most likely be gb2312.
 */
int seeker_lookup(QQWrySeeker *seeker, unsigned int ip, char location[], int loc_size)
{
	if (loc_size <= 0)
		return -1;

	int low = 0, up = (seeker->idx_end - seeker->idx_beg) / 7;
	while (low <= up)
	{
		int mid = (low + up) / 2;
		int compare = ip_match(seeker->fp, ip, seeker->idx_beg + mid * 7);
		if (compare == 0) /* we found the record */
			return read_location(seeker->fp, location, loc_size);
		if (compare < 0)
			up = mid - 1;
		else
			low = mid + 1;
	}

	location[0] = 0;
	return 0;
}
Example #6
0
/* read a full location including country and area.
 * the return value and policy of buf[] is the same as read_str().
 */
static int read_location(FILE *fp, char buf[], int buf_size)
{
	if (buf_size <= 0)
		return -1;

	buf[0] = 0;
	if (!fread(buf, 1, 1, fp))
		return 0;

	if (buf[0] == 0x01)
	{
		fseek(fp, read_int3(fp), SEEK_SET);
		return read_location(fp, buf, buf_size);
	}

	fseek(fp, -1, SEEK_CUR);
	int i = read_location_piece(fp, buf, buf_size);  /* read country */
	i += read_location_piece(fp, buf + i, buf_size - i); /* read area */

	return i;
}
int main(int argc, char **argv)
{
  int i, currentNebUser, currentUserMessage, matches = 0;
  /* print usage if needed */
  if (argc != 1)
  {
    fprintf(stderr, "No args needed");
    exit(0);
  }

  char filename[1024];
  FILE *file = NULL, *fp = NULL, *userFile = NULL, *messageFile = NULL;

  sprintf(filename, "../../Data/tableinfo.dat");
  file = fopen(filename, "rb");

  int locationNum, userNum, messageNum;
  fread(&locationNum, sizeof(int), 1, file);
  fread(&userNum, sizeof(int), 1, file);
  fread(&messageNum, sizeof(int), 1, file);
  fclose(file);

  struct timeval time_start, time_end;

  int previousBest =0;
  int bestUserID =0;
  char *matchString = "Nebraska";

  /* start time */
  gettimeofday(&time_start, NULL);

  // Get list of NE cities
  int nebraskaStart, nebraskaEnd;
  nebraskaStart = findLocationEdge(0, locationNum, matchString, -1, 0);
  nebraskaEnd = findLocationEdge(0, locationNum, matchString, 1, locationNum);
  //printf("NE Start: %d, NE End: %d", nebraskaStart, nebraskaEnd);

  for (i = nebraskaStart; i <= nebraskaEnd; i++)
  {
    /* open the corresponding file */
    sprintf(filename, "../../Data/Locations/location_%06d.dat", i);

    fp = fopen(filename,"rb");

    if (!fp) {
      fprintf(stderr, "Cannot open %s\n", filename);
      exit(0);
    }

    // Get location id from NE cities
    location_t *loc = read_location(fp);
    //printf("%s, %s\n", loc->city, loc->state);
    fclose(fp);

    //printf("%s, %s\n", loc->city, loc->state);

    // Get list of users from NE cities
    int firstNebUserAtLocation, lastNebUserAtLocation;
    firstNebUserAtLocation = findUserEdge(0, userNum, loc->locationID, -1, 0);
    lastNebUserAtLocation = findUserEdge(0, userNum, loc->locationID, 1, userNum);
    //printf("  Start: %i, End: %i\n", firstNebUserAtLocation, lastNebUserAtLocation);

    // foreach user locationStart to locationEnd
    for (currentNebUser = firstNebUserAtLocation; currentNebUser <= lastNebUserAtLocation; currentNebUser++)
    {
      // open user file at current ID in loop
      sprintf(filename, "../../Data/Users/user_%06d.dat", currentNebUser);
      userFile = fopen(filename, "rb");

      if (!userFile) {
        fprintf(stderr, "Cannot open %s\n", filename);
        exit(0);
      }

      // Get userid from NE users
      user_t *usr = read_user(userFile);
      fclose(userFile);

      // get list of messages from NE users
      int firstUserMessage, lastUserMessage;
      firstUserMessage = findMessageEdge(0, messageNum, usr->id, -1, 0);
      lastUserMessage = findMessageEdge(0, messageNum, usr->id, 1, messageNum);
      //printf("    UserMessages Start: %i, End: %i\n", firstUserMessage, lastUserMessage);
      if(firstUserMessage == -1 && lastUserMessage == -1)
      {
	printf("Warning, no messages found for user %i\n", usr->id);
      }
      else
      {
        int criteriaCount = 0;
        // foreach message associated with current user id
        for (currentUserMessage = firstUserMessage; currentUserMessage <= lastUserMessage; currentUserMessage++)
        {
          // open message file
          sprintf(filename, "../../Data/Messages/message_%07d.dat", currentUserMessage);
          messageFile = fopen(filename, "rb");

          if (!messageFile) {
            fprintf(stderr, "Cannot open %s\n", filename);
            exit(0);
          }

          message_t *msg = read_message(messageFile);
          fclose(messageFile);

          // check time; increment
          if (msg->hour == 8 || ((msg->hour == 9) && (msg->minute == 0)))
          {
            criteriaCount++;
          }
        }
	if (criteriaCount > previousBest)
	{
	  previousBest = criteriaCount;
	  bestUserID = usr->id;
	}
      }
    }

    free_location(loc);
  }

  /* end time */
  gettimeofday(&time_end, NULL);

  float totaltime = (time_end.tv_sec - time_start.tv_sec)
  + (time_end.tv_usec - time_start.tv_usec) / 1000000.0f;

  printf("\n\nUserID  %i and Number of Messages %i \n", bestUserID, previousBest);
  printf("\n\nProcess time %f seconds\n", totaltime);

  return 0;
}