예제 #1
0
/*
   Given a flight system with airports, and a file of schedules for those airports,
   add flight times to the system.
 */
void parseSchedule(flightSys_t* s, FILE* schedule) {
    char* newAirportPrefix = "AIRPORT: ";
    size_t prefixLen = strlen(newAirportPrefix);
    airport_t* curAirport = NULL;

    /*
       curAirport is the airport we are currently adding a schedule for.
       In other words, it is the source airport.

       The while loop parses the file line by line and sets a new curAirport when a line that starts with "STATION: " is found.
       It'll also echo the schedule of the previous airport.
       Otherwise, it treats the current line as a new entry in the schedule of the curAirport and calls addFlight.
     */
    char line[MAX_LINE_LEN];
    while(fgets(line, MAX_LINE_LEN, schedule)) {
	if(!strcmp(line,"\n")) continue;
	else if(strncmp(line,newAirportPrefix,prefixLen) == 0){
    if(curAirport) printSchedule(curAirport); //done with previous schedule so print it

	    stripNewLine(line);
	    char* srcName = line+prefixLen;
	   
      curAirport = getAirport(s,srcName);
	   
      if(curAirport)
		printf("Adding schedule for airport %s\n",srcName);
	    else
		printf("Cannot find airport %s\n",srcName);
	} else if (curAirport) {
	    char dstName[MAX_LINE_LEN];
	    char departureStr[MAX_LINE_LEN];
	    char arrivalStr[MAX_LINE_LEN];
	    char priceStr[MAX_LINE_LEN];
	    if(4!=sscanf(line,"%s %s %s $%s",dstName,departureStr,arrivalStr, priceStr)) { //parses the line as destination airport name, departure time, arrival time, and price
		printf("Skipping line: %s\n",line);
		continue;
	    }
	    airport_t* dst = getAirport(s,dstName);
	    if(dst==NULL) {
		printf("Cannot find airport %s\n",dstName);
		continue;
	    }

	    timeHM_t arrival;
	    timeHM_t departure;
	    int cost;
	    char* endptr;
	    cost = (int) strtol(priceStr,&endptr,10);
	    if(!stringToTime(arrivalStr, &arrival) || !stringToTime(departureStr, &departure) || *endptr) {
		printf("Skipping line: %s\n",line);
		continue;
	    }
	    addFlight(curAirport,dst,&departure,&arrival, cost);
	}
    }
    if(curAirport) printSchedule(curAirport);
}
예제 #2
0
/*
 *  COMMAND CENTRE
 */
String Controller::doCommand(String string)
{
  //Log.Debug("Command: %s", string);
  
  String s = NULL;
  int i = (int) stringToTime(string);
  char command;
  
  if (string.length() > 0)
  {
    command = string.charAt(0);
  }
  else
  {
    return s;
  }
  
  switch (command)
  {
    case 'A': // status request only
      break;
    case 'L': // move left
      moveLeft(i);
      break;
    case 'R': // move right
      moveRight(i);
      break;
    case 'I': // update interval
      setInterval(i);
      break;
    case 'J': // update step size
      setStepSize(i);
      break;
    case 'S': // update start/end/rewind times
      setTimes(string);
      break;
    case 'T': // update date and time
      setTimestamp(stringToTime(string));
      break;
    default: // returns NULL
      return s;
  }
  s = getStatus();
  
  return s;
}
bool CEnumInfo<T>::compareTime(char *pTime1, char *pTime2)
{
	const double time1 = stringToTime(pTime1);
	const double time2 = stringToTime(pTime2);
	return time1 > time2;
}
예제 #4
0
int CMoleSendCard::load_cache_xml(xmlNodePtr cur)
{
    cur = cur->xmlChildrenNode;
    while (cur)
    {
        if (!xmlStrcmp(cur->name, reinterpret_cast<const xmlChar*>("card")))
        {
            char begin[64];
            char end[64];
            uint32_t province;
            uint32_t city;
            uint32_t type;
            uint32_t senderid;
            char sendernick[16];
            uint32_t mapid;
            char msg[4096];

            DECODE_XML_PROP_STR(begin,      cur, "begin");
            DECODE_XML_PROP_STR(end,        cur, "end");
            DECODE_XML_PROP_UINT32(province,cur, "province");
            DECODE_XML_PROP_UINT32(city,    cur, "city");
            DECODE_XML_PROP_UINT32(type,    cur, "type");
            DECODE_XML_PROP_UINT32(senderid,cur, "senderid");
            DECODE_XML_PROP_STR(sendernick, cur, "sendernick");
            DECODE_XML_PROP_UINT32(mapid,   cur, "mapid");
            DECODE_XML_PROP_STR(msg,        cur, "msg");

            conf_t conf;
            memset(&conf, 0, sizeof(conf));

            if (!stringToTime(begin, conf.begin))
            {
                ERROR_LOG("read begin time failed");
                return -1;
            }
            if (!stringToTime(end, conf.end))
            {
                ERROR_LOG("read end time failed");
                return -1;
            }

            conf.province_code = province;
            conf.city_code = city;

            proto_head_t *ph = (proto_head_t *)(conf.buf);
            ph->seq = 0;
            ph->cmd = 0xE101;
            ph->ret = 0;
            
            DEBUG_LOG("==========load conf==========");           

            int p = sizeof(proto_head_t);
           
            *(uint32_t *)(&conf.buf[p]) = type;
            p += sizeof(uint32_t);
            DEBUG_LOG("load conf type = %u", type);           

            time_t t = time(0);
            *(uint32_t *)(&conf.buf[p]) = t;
            p += sizeof(uint32_t);

            *(uint32_t *)(&conf.buf[p]) = senderid;
            p += sizeof(uint32_t);
            DEBUG_LOG("load conf senderid = %u", senderid);

            strncpy(&conf.buf[p], sendernick, sizeof(sendernick)); 
            p += sizeof(sendernick);
            DEBUG_LOG("load conf sendernick = %s", sendernick);           
 
            *(uint32_t *)(&conf.buf[p]) = mapid;
            p += sizeof(uint32_t);
            DEBUG_LOG("load conf mapid = %u", mapid);

            int l = strlen(msg);
            *(uint32_t *)(&conf.buf[p]) = l;
            p += sizeof(uint32_t);
            DEBUG_LOG("load conf msg_len = %u", l);

            memcpy(&conf.buf[p], msg, l);
            p += l;
            DEBUG_LOG("load conf msg = %s", msg);

            conf.proto_len = p;

            std::map<uint32_t, std::vector<conf_t> >::iterator it;
            it = m_confs.find(conf.province_code);
            if (it == m_confs.end())
            {
                m_confs[conf.province_code] = std::vector<conf_t>();
                m_flags[conf.province_code] = std::vector<std::set<uint32_t> >();
            }
            m_confs[conf.province_code].push_back(conf);
            m_flags[conf.province_code].push_back(std::set<uint32_t>());
        }
        cur = cur->next;
    }

    return 0; 
}
예제 #5
0
/*
   Given a file with routes and a flight system, parses the file and determine (and print) the earliest time each route can be completed.
   Lines of bad form are skipped while new lines are ignored.
 */
void calcRouteTimes(flightSys_t* s, FILE* routes) {
    char* newRoutePrefix = "ROUTE: ";
    size_t prefixLen = strlen(newRoutePrefix);
    char line[MAX_LINE_LEN];
    timeHM_t now;
    char route[MAX_LINE_LEN];
    char timeBuf[MAX_LINE_LEN];
    char airportBuf[MAX_LINE_LEN];
    airport_t* curAirport = NULL;
    int curCost = 0;
    int totalCost = 0;

    /*
       This while loop parses the file line by line and calculate the route times as it goes.
       If a new route line is found, the current route (if it exists) would be done and the result is printed.
       A current route exists if curAirport is not null. 

       curAirport tells us where we are on the current route.

       now tells us the current time (the earliest time we can reach the curAirport on the current route).
     */
    while(fgets(line, MAX_LINE_LEN,routes)) {
	stripNewLine(line);

	if(!strlen(line)) continue; //ignore line if it's empty
	else if(strncmp(line,newRoutePrefix,prefixLen) == 0) { //if beginning of line starts with "ROUTE: ", we are calculating for a new route
	    if(curAirport) printCompleteRoute(route,&now, totalCost); //if curAirport is not NULL, we are done with the previous route so print the result

	    curAirport = NULL;
	    totalCost = 0;
	    if(3!=sscanf(line+prefixLen,"%s %s %s",route,airportBuf,timeBuf) //parse rest of new route line into route name, airport name, and start time 
		    || !stringToTime(timeBuf,&now) //ensure time is valid
		    || !(curAirport = getAirport(s,airportBuf))) { //ensure airport exists and sets curAirport to the starting airport
		printf("Skipping line: %s\n",line);
		continue;
	    }
	} else if (curAirport) {
	    /*
	       This is the case when we are in the middle of calculating for a route,
	       and the line should just be next airport we have to get to.
	       Here, we use getNextFlight to determine the flight to take to the next airport.
	       If there are no possible flights, the route cannot be completed.
	     */
	    airport_t* next = getAirport(s,line);
	    if(!next) {
		printf("Skipping line: %s\n",line);
		continue;
	    }
	    timeHM_t depart;
	    timeHM_t arrival;
	    if(!getNextFlight(curAirport,next,&now,&depart,&arrival,&curCost)) {
		curAirport = NULL;
		totalCost = 0;
		printf("Route %s cannot be completed\n",route);
		continue;
	    }
	    now = arrival;
	    curAirport = next;
	    totalCost += curCost;
	}
    }
    if(curAirport) printCompleteRoute(route,&now,totalCost);
}
예제 #6
0
int addTransactionWizard(char* wallet) {
  Transaction tx;
  tx.date.year = getCurrentYear();
  tx.date.month = getCurrentMonth();
  tx.date.day = getCurrentDay();
  tx.time.hour = getCurrentHour();
  tx.time.minute = getCurrentMinute();
  tx.time.second = getCurrentSecond();
  strcpy(tx.description, (char*)"");
  int curstep = 0;
  while(1) {
    SetBackGround(0x0A);
    drawScreenTitle("Add transaction");
    // < (first label), SELECT of on date step, and Next or Finish (last label)
    drawFkeyLabels(curstep>0 ? 0x036F : -1, curstep == 2 ? 0x000F : 0, 0, 0, 0,
                   curstep==4 ? 0x04A4 : 0x04A3);
    if(curstep == 0) {
      MenuItem menuitems[5];
      menuitems[0].text = (char*)"Debit";
      menuitems[1].text = (char*)"Credit";

      Menu menu;
      menu.items=menuitems;
      menu.type=MENUTYPE_FKEYS;
      menu.numitems=2;
      menu.height=2;
      menu.startY=3;
      menu.pBaRtR=1;
      int inloop=1;
      while(inloop) {
        // this must be here, inside this loop:
        SetBackGround(0x0A);
        drawScreenTitle("Add transaction", "Select type:");
        drawFkeyLabels(-1, -1, -1, -1, -1, 0x04A3);
        int res = doMenu(&menu);
        if(res == MENU_RETURN_EXIT) return 0;
        else if(res == KEY_CTRL_F6 || res == MENU_RETURN_SELECTION) {
          tx.credit = menu.selection == 2;
          curstep++;
          break;
        }
      }
    } else if(curstep == 1) {
      drawScreenTitle(NULL, "Amount:");
      char samount[20] = "";
      if(tx.amount.val) {
        currencyToString(samount, &tx.amount);
      }
      textInput input;
      input.charlimit=12;
      input.acceptF6=1;
      input.symbols = 0; // allow the decimal separator
      input.forcetext = 1;
      input.buffer = (char*)samount;
      input.type = INPUTTYPE_NUMERIC;
      while(1) {
        input.key=0;
        int res = doTextInput(&input);
        if (res==INPUT_RETURN_EXIT) return 0; // user aborted
        else if (res==INPUT_RETURN_CONFIRM) {
          if(!stringToCurrency(&tx.amount, samount)) {
            if(!tx.amount.val) {
              AUX_DisplayErrorMessage(0x4B);
            } else {
              curstep++;
            }
            break;
          } else AUX_DisplayErrorMessage(0x43);
        } else if (res==INPUT_RETURN_KEYCODE && input.key == KEY_CTRL_F1) {
          curstep--;
          break;
        }
      }
    } else if(curstep == 2) {
      drawScreenTitle(NULL, "Date:");
      mPrintXY(7, 4, getInputDateFormatHint(), TEXT_MODE_TRANSPARENT_BACKGROUND, TEXT_COLOR_BLACK);
      textInput input;
      input.x=7;
      input.width=8;
      input.charlimit=8;
      input.acceptF6=1;
      input.type=INPUTTYPE_DATE;
      char datebuffer[15];
      fillInputDate(&tx.date, datebuffer);
      input.buffer = (char*)datebuffer;
      while(1) {
        input.key=0;
        int res = doTextInput(&input);
        if (res==INPUT_RETURN_EXIT) return 0; // user aborted
        else if (res==INPUT_RETURN_CONFIRM) {
          int len = strlen(datebuffer);
          if(len == input.charlimit) {
            int yr,m,d;
            stringToDate(datebuffer, &yr, &m, &d);
            if(isDateValid(yr, m, d)) {
                tx.date.year = yr;
                tx.date.month = m;
                tx.date.day = d;
                curstep++;
                break; // continue to next step
            } else invalidFieldMsg(0);
          } else invalidFieldMsg(0);
        } else if (res==INPUT_RETURN_KEYCODE) {
          if(input.key==KEY_CTRL_F1) {
            curstep=curstep-1; break;
          } else if(input.key==KEY_CTRL_F2) {
            int ey=0, em=0, ed=0;
            if(!selectDateScreen(&ey, &em, &ed,
                                  (char*)"Select transaction date:", NULL, 1)) {
              tx.date.year = ey;
              tx.date.month = em;
              tx.date.day = ed;
              curstep++; break; // continue to next step
            }
            break; //redraw
          }
        }
      }
    } else if(curstep == 3) {
      drawScreenTitle(NULL, "Time:");
      mPrintXY(8, 4, "HHMMSS", TEXT_MODE_TRANSPARENT_BACKGROUND, TEXT_COLOR_BLACK);
      
      textInput input;
      input.x=8;
      input.width=6;
      input.charlimit=6;
      input.acceptF6=1;
      input.type=INPUTTYPE_TIME;
      char tbuffer[15];
      fillInputTime(&tx.time, tbuffer);
      input.buffer = (char*)tbuffer;
      while(1) {
        input.key=0;
        int res = doTextInput(&input);
        if (res==INPUT_RETURN_EXIT) return 0; // user aborted
        else if (res==INPUT_RETURN_CONFIRM) {
          if((int)strlen(tbuffer) == input.charlimit) {
            int h, m, s;
            stringToTime(tbuffer, &h, &m, &s);
            if(isTimeValid(h, m, s)) {
              tx.time.hour = h;
              tx.time.minute = m;
              tx.time.second = s;
              curstep++;
              break; // continue to next step
            } else invalidFieldMsg(1);
          } else invalidFieldMsg(1);
        } 
        else if (res==INPUT_RETURN_KEYCODE && input.key==KEY_CTRL_F1) { curstep--; break; }
      }
    } else if(curstep == 4) {
      drawScreenTitle(NULL, "Description:");
      textInput input;
      input.charlimit=128;
      input.acceptF6=1;
      input.buffer = (char*)tx.description;
      int inloop = 1;
      while(inloop) {
        input.key=0;
        int res = doTextInput(&input);
        if (res==INPUT_RETURN_EXIT)
          return 0; // user aborted
        else if (res==INPUT_RETURN_CONFIRM)
          inloop = 0; // all fields complete, continue with transaction adding
        else if (res==INPUT_RETURN_KEYCODE && input.key == KEY_CTRL_F1) {
          curstep--;
          break;
        }
      }
      if(!inloop) break;
    }
  }
  addTransaction(&tx, wallet);
  return 1;
}