Exemple #1
0
void* thr_func(void * arg)
{
    int count = (int)arg;
    int beg = Info[count].beg;
    int end = Info[count].end;
    connected[count] = 1;

    char* file_pointer = Info[count].file_pointer;
    sum_str += count_str(file_pointer, beg, end);
    sum_word += count_Word(file_pointer, beg, end);
}
Exemple #2
0
void		free_tab(char **tab)
{
	int	i;

	i = count_str(tab);
	while (tab[i])
	{
		free(tab[i]);
		i--;
	}
	free(tab);
}
Exemple #3
0
char		*printf_my_getstr(const char *s, int (*cmp)(char))
{
  char		*ret;
  int		i;

  if ((ret = malloc(sizeof(*s) * (count_str(s, cmp) + 1))) == NULL)
    return (NULL);
  i = 0;
  while (s && cmp(s[i]))
    {
      ret[i] = s[i];
      i++;
    }
  ret[i] = 0;
  return (ret);
}
Exemple #4
0
    void draw_inventory(sf::RenderWindow & win, level const & lvl)
    {
        auto & res = resource_manager::get();
        auto & inv = lvl.chip.get<inventory>();
        position p(inventory_xpos, inventory_ypos);
        
        /// Draw keys
        int key_pos = static_cast<int>(entity_id::blue_key);
        for (int i=0; i < 4; ++i)
        {
            draw_tile_at(win, tile_id::floor, p);
            
            entity_id key_id = static_cast<entity_id>(key_pos + i);
            unsigned count = inv.count(key_id);
            if (count)
            {
                draw_tile_at(win, key_id, p);
                sf::Text count_str(std::to_string(count), res[font_uid::arial], 15);
                count_str.setColor(sf::Color::Red);
                count_str.setPosition((float)p.x, (float)p.y);
                win.draw(count_str);
            }
                
            p.x += tile_width;
        }
        
        /// Draw Boots
        p = position(inventory_xpos, inventory_ypos + tile_height);
        int boot_pos = static_cast<int>(entity_id::flippers);
        for (int i=0; i < 4; ++i)
        {
            draw_tile_at(win, tile_id::floor, p);
            entity_id boot_id = static_cast<entity_id>(boot_pos + i);
            if (inv.contains(boot_id))
                draw_tile_at(win, boot_id, p);
            p.x += tile_width;
        }
        
          // TODO Draw weapons
//         p = position(inventory_xpos, inventory_ypos + (tile_height * 2));
//         for (int i=0; i < 4; ++i)
//         {
//             chips::draw(win, entity(entity_id::floor), p);
//             p.x += tile_width;
//         }
    }
Exemple #5
0
int main(void) {
  char strin[100];
scanf("%[^\n]s",strin);

    int i=count_str(strin);
    if(i!=0)
    {
    	printf("extension is:");
   for(i=i+1;strin[i]!='\0';i++)
   {
   	printf("%c",strin[i]);
   } 
    }
    else
     printf("there is no extension in the entered word");
    return 0;
}
/** send out response according to different http request. 
a typical workflow of auto case would be
/check_server_status						(by com-module)
/init_test									(by com-module)
/set_testcase								(by com-module)
/check_server 								(by widget)
/init_session_id?session_id=2033			(by widget)
/auto_test_task?session_id=2033				(by widget)
/check_execution_progress?session_id=2033	(by widget)
/ask_next_step?session_id=2033				(by widget)
/commit_result 								(by widget)
/auto_test_task?session_id=2033				(by widget)
/manual_cases 								(by widget)
/generate_xml 								(by widget)
/check_server_status 						(by com-module)
/get_test_result 							(by com-module)

a typical workflow of manual case would be
/check_server_status						(by com-module)
/init_test									(by com-module)
/set_testcase								(by com-module)
/check_server 								(by widget)
/init_session_id?session_id=2033			(by widget)
/auto_test_task?session_id=2033				(by widget)
/manual_cases 								(by widget)
/check_server_status 						(by com-module)
/commit_manual_result 						(by widget)
...
/check_server_status 						(by com-module)
/commit_manual_result 						(by widget)
/generate_xml 								(by widget)
/get_test_result 							(by com-module)

**/
void HttpServer::processpost(int s, struct HttpRequest *prequest)
{
	prequest->prefix = "application/json";
	string json_str = "{\"OK\":1}";

#if defined(__WIN32__) || defined(__WIN64__)
	cout << "prequest->path is:" << prequest->path << endl;
	cout << "prequest->content is:" << prequest->content << endl;
#else
	if (g_show_log) {
		DBG_ONLY("prequest->path is:" << prequest->path);
		DBG_ONLY("prequest->content is:" << prequest->content);
	}
#endif

	if (prequest->path.find("/init_test") != string::npos) {	// invoke by com-module to init some para for test
		set_cl_timeout(0);
		Json::Reader reader;
		Json::Value value;
		if (g_show_log)
			DBG_ONLY("[ init the test suite ]");

		bool parsed = reader.parse(prequest->content, value);
		if (parsed) {
			g_launcher = value["launcher"].asString();
            g_platform = value["platform"].asString();

			m_suite_name = value["suite_name"].asString();
			if (g_launcher == "wrt-launcher") {
				g_run_wiget = true;
				m_suite_id = value["suite_id"].asString();

				g_launch_cmd = g_launcher + " -s " + m_suite_id;
				g_kill_cmd = g_launcher + " -k " + m_suite_id;
				killAllWidget();
			} else if (g_platform == "androidmobile") {	// xwalk on android
				g_run_wiget = false;
				//am start -n org.xwalk.tct-2dtransforms-css3-tests/.tct-2dtransforms-css3-testsActivity
				//g_launch_cmd = "am start -n org.xwalk." + m_suite_name + "/." + m_suite_name + "Activity";
				//g_kill_cmd = "am force-stop org.xwalk." + m_suite_name;
				g_launch_cmd = "";
				g_kill_cmd = "";
			} else if (g_platform == "windowshttp"){ // xwalk on windows
				g_run_wiget = true;
				m_suite_id = value["suite_id"].asString();
				//g_launch_cmd = "C:\\\"Program Files\"\\"+m_suite_id + "\\" + g_launcher;
				g_launch_cmd = "notepad C:\\\"Program Files\"\\" + m_suite_id + "\\" + m_suite_id + "\\manifest.json";
				g_kill_cmd = "";
				sleep(1);
		    }else {
#if defined(__WIN32__) || defined(__WIN64__)
				g_launch_cmd = "start /b " + g_launcher;
				g_kill_cmd = "";
#else
				g_launch_cmd = g_launcher + " &";
				if (g_launcher.find("XWalkLauncher") != string::npos)	//kill xwalk
					g_kill_cmd = "pkill XWalkLauncher";
				//g_kill_cmd = "ps ax | grep xwalk | grep -v testkit-lite | grep -v grep | awk '{print $1F}' | xargs kill -9;";
				else
					g_kill_cmd = "";	// not kill browser
#endif
				m_suite_id = value["suite_id"].asString();
				g_run_wiget = true;
				//wait for the index window.close, otherwise will occur bind aleady error
				sleep(1);
			}
		} else {
			if (g_show_log) {
				DBG_ONLY
				    ("error while parse para from com-module, can't start test");
				DBG_ONLY(prequest->content);
			}
			json_str = "{\"Error\":\"parse error\"}";
		}
	} else if (prequest->path.find("/execute_cmd") != string::npos){  //Only work on Windows! invoke by com-module for executing command on windows platform
		if (g_show_log){
			DBG_ONLY("[ execute command: ]" << endl);
			DBG_ONLY(prequest->content);
		}
		Json::Reader reader;
		Json::Value value;

		bool parsed = reader.parse(prequest->content, value);
		string json_cmd = "";
		if (parsed){
			json_cmd = value["cmd"].asString();
		}

		std::vector < string > outputs;
		string cmd = json_cmd;
		cout << "[ command: ]" << cmd << endl;
		run_cmd(cmd, "", &outputs);
		for (unsigned int i = 0; i < outputs.size(); i++){
			cout << "[ output: ]" << outputs[i] << endl;
		}
#if defined(__WIN32__) || defined(__WIN64__)
	} else if (prequest->path.find("/powershell_install") != string::npos){
		//Only work with Windows PowerShell. JSON {suite: suitename, host: hostip, file:suitefile_file_path}
		Json::Reader reader;
		Json::Value value;

		bool parsed = reader.parse(prequest->content, value);
		string json_suite = "";
		string json_file = "";
		string json_host = "";
		if (parsed) {
			json_suite = value["suite"].asString();
			json_file = value["file"].asString();
			json_host = value["host"].asString();
		}

		string ps_cmd = "powershell -file c:\\stub\\powershell\\download.ps1 ";
		ps_cmd = ps_cmd + json_suite + " " + json_file + " " + json_host;
		cout << "[ command: ]" << ps_cmd << endl;
		int ret = run_cmd_return_code(ps_cmd);
		if(ret != 1){
			if (ret == -1)
				json_str = "{\"Error\":\"Fail to install "+ json_suite +"\"}";
			else if (ret == -2)
				json_str = "{\"Error\":\"Fail to download "+ json_file +"\"}";
			else if (ret == -5)
				json_str = "{\"Error\":\"Fail to execute "+ json_file +". Popen return null\"}";
			else
				json_str = "{\"Error\":\"Unknown script errors.\"}";
		}
	} else if (prequest->path.find("/powershell_uninstall") != string::npos){
		//Only work with Windows PowerShell. JSON {suite: suitename}
		Json::Reader reader;
		Json::Value value;

		bool parsed = reader.parse(prequest->content, value);
		string json_suite = "";
		if (parsed) {
			json_suite = value["suite"].asString();
		}

		string ps_cmd = "powershell -file c:\\stub\\powershell\\uninstall.ps1 ";
		ps_cmd = ps_cmd + json_suite;
		cout << "[ command: ]" << ps_cmd << endl;
		int ret = run_cmd_return_code(ps_cmd);
		if(ret != 1){
			json_str = "{\"Error\":\"Fail to uninstall.\"}";
		}
	} else if (prequest->path.find("/powershell_exitcode") != string::npos){
		string ps_cmd = "powershell -file c:\\stub\\powershell\\exitcode.ps1 0";
		cout << "[ command: ]" << ps_cmd << endl;
		int ret = run_cmd_return_code(ps_cmd);
		cout << "[ return: ]" << ret << endl;
#endif
	} else if (prequest->path.find("/execute_async_cmd") != string::npos) {
		Json::Reader reader;
		Json::Value value;

		bool parsed = reader.parse(prequest->content, value);
		string json_cmd = "";
		if (parsed) {
			json_cmd = value["cmd"].asString();
		}
		string cmd = json_cmd;
		run_cmd_async(cmd);
    }else if (prequest->path.find("/set_testcase") != string::npos) {	// invoke by com-module to send testcase data
		m_block_finished = false;
		m_set_finished = false;
		m_timeout_count = 0;
		m_server_checked = false;
		if (g_show_log)
			DBG_ONLY("[ set test cases ]");
		parse_json_str(prequest->content);

		set_timer(20);	// set timer here incase widget hang.
		m_check_times = 0;

		m_block_case_index = 0;
		if (m_current_block_index == 1)
			m_total_case_index = 0;
	} else if (prequest->path == "/check_server") {	// invoke by index.html to find server running or not
		set_cl_timeout(0);
		m_server_checked = true;
		m_check_times = 0;
		cancel_time_check();
		if (g_show_log)
			DBG_ONLY
			    ("[ checking server, and found the server is running ]");
	} else if (prequest->path == "/check_server_status") {	// invoke by com-module to get server status
		Json::Value status;
		status["block_finished"] = m_block_finished ? 1 : 0;
		status["finished"] = m_set_finished ? 1 : 0;
		if (m_exeType == "auto" && check_cl_timeout()) {
			status["error_code"] = 2;
			status["finished"] = 1;	//finish current set if widget is timeout
		} else if (m_failto_launch > m_max_fail_launch) {
			status["error_code"] = 2;
			status["finished"] = 1;	// finish current set if can't launch widget
		} else if (g_error_code > 0) {
			status["error_code"] = g_error_code;
			g_error_code = 0;
		}

		pthread_mutex_lock(&result_mutex);
		char count[8];
		memset(count, 0, 8);
		sprintf(count, "%d", m_result.size());
		status["count"] = count;
		status["cases"] = m_result;
		m_result.clear();
		pthread_mutex_unlock(&result_mutex);

		json_str = status.toStyledString();

		if (!m_server_checked) {
			if (g_show_log)
				DBG_ONLY
				    ("wait for widget check_server, please check on device."
				     << endl);
		}
		if (m_totalBlocks > 0) {
			if (g_show_log)
				DBG_ONLY("group: " << m_current_block_index <<
					 "/" << m_totalBlocks <<
					 ", total case: " << m_total_case_index
					 << "/" << m_totalcaseCount <<
					 ", group case: " << m_block_case_index
					 << "/" << m_block_case_count <<
					 ", m_timeout_count:" <<
					 m_timeout_count);
			if (m_exeType != "auto") {
				if (g_show_log)
					DBG_ONLY
					    ("manual cases. please check on device."
					     << endl);
			}
		}
	} else if (prequest->path == "/shut_down_server") {
		if (g_run_wiget == true)
			killAllWidget();	// kill all widget when shutdown server
		gIsRun = 0;
	} else if (prequest->path.find("/init_session_id") != string::npos) {	// invoke by index.html to record a session id
		set_cl_timeout(0);
		int index = prequest->path.find('=');
		if (index != -1) {
			m_running_session = prequest->path.substr(index + 1);
			if (g_show_log)
				DBG_ONLY("[ sessionID: " << m_running_session <<
					 " is gotten from the client ]");
		} else {
			if (g_show_log)
				DBG_ONLY("[ invalid session id ]");
		}
	} else if (prequest->path.find("/ask_next_step") != string::npos) {	// invoke by index.html to check whether there are more cases
		set_cl_timeout(0);
		if (m_block_finished || m_set_finished)
			json_str = "{\"step\":\"stop\"}";
		else
			json_str = "{\"step\":\"continue\"}";

		m_timeout_count = 0;	// reset the timeout count
	} else if (prequest->path.find("/auto_test_task") != string::npos) {	// invoke by index.html to get current auto case
		set_cl_timeout(0);
		if (m_test_cases == NULL) {
			json_str = "{\"Error\":\"no case\"}";
		} else if (m_exeType != "auto") {
			json_str = "{\"none\":0}";
		} else {
			string error_type = "";
			bool find_tc =
			    get_auto_case(prequest->content, &error_type);
			if (find_tc == false) {
				json_str = "{\"" + error_type + "\":0}";
			} else {
				json_str =
				    m_test_cases[m_block_case_index].to_json().
				    toStyledString();
			}
		}
	} else if (prequest->path.find("/manual_cases") != string::npos) {	// invoke by index.html to get all manual cases
		cancel_time_check();	// should not timeout in manual mode
		if (!m_test_cases) {
			json_str = "{\"Error\":\"no case\"}";
		} else if (m_exeType == "auto") {
			json_str = "{\"none\":0}";
		} else {
			Json::Value arrayObj;
			for (int i = 0; i < m_block_case_count; i++)
				arrayObj.append(m_test_cases[i].to_json());

			json_str = arrayObj.toStyledString();
			set_timer(60);	// check every 60 seconds to make sure widget alive when run manual cases.
		}
	} else if (prequest->path.find("/commit_manual_result") != string::npos) {	// invoke by index.html to provide result of a manual case.
		set_cl_timeout(0);
		if ((prequest->content.length() == 0) || (!m_test_cases)) {
			json_str = "{\"Error\":\"no manual result\"}";
		} else {
			find_id(splitContent(prequest->content), false);	// will set index in find_id
		}
	} else if (prequest->path.find("/check_execution_progress") != string::npos) {	//invoke by index.html to get test result of last auto case
		set_cl_timeout(0);
		char *total_count = new char[16];
		sprintf(total_count, "%d", m_totalcaseCount);
		char *current_index = new char[16];
		sprintf(current_index, "%d", m_total_case_index + 1);

		string count_str(total_count);
		string index_str(current_index);
		json_str =
		    "{\"total\":" + count_str + ",\"current\":" + index_str +
		    ",\"last_test_result\":\"" + m_last_auto_result + "\"}";

		delete[]total_count;
		delete[]current_index;
	}
	//generate_xml:from index_html, a maually block finished when click done in widget, or no manual cases anymore(when run auto block)
	else if (prequest->path == "/generate_xml") {
		set_cl_timeout(0);
		if (m_exeType != "auto") {
			cancel_time_check();
			m_block_finished = true;
			if (m_current_block_index == m_totalBlocks)
				m_set_finished = true;
		}
	}
	//from com module,when m_set_finished is true
	else if (prequest->path == "/get_test_result") {
		cancel_time_check();
		if (!m_test_cases) {
			json_str = "{\"Error\":\"no case\"}";
		} else {
			Json::Value root;
			root["count"] = "0";
			root["cases"] = m_result;	// m_result will always be empty here
			json_str = root.toStyledString();
		}
	}
	// index.html invoke this with id and result of an auto case, auto case commit result. 
	// we need find correct test case by id, and record test result to it.
	else if (prequest->path == "/commit_result") {
		set_cl_timeout(0);
		if ((prequest->content.length() == 0) || (!m_test_cases)) {
			json_str = "{\"Error\":\"no result\"}";
			m_last_auto_result = "BLOCK";
		} else {
			Json::Value paras = splitContent(prequest->content);
			if (m_running_session == paras["session_id"].asString()) {
				find_id(paras, true);
			}
		}
	} else if (prequest->path == "/set_capability") {	// by com-module to send capability data
		Json::Reader reader;

		reader.parse(prequest->content, m_capability);
	} else if (prequest->path.find("/capability") != string::npos) {	// by test suite. only one query parameter each time
		json_str = "{\"support\":0}";

		Json::Value paras = splitContent(prequest->content);
		string value = paras["value"].asString();
		string name = paras["name"].asString();
		for (unsigned int i = 0; i < name.size(); i++)
			name[i] = tolower(name[i]);

		if (m_capability[name].isBool()) {	// for bool value, omit the value part
			json_str = "{\"support\":1}";
		} else if (m_capability[name].isInt()) {
			if (m_capability[name].asInt() == atoi(value.c_str()))
				json_str = "{\"support\":1}";
		} else if (m_capability[name].isString()) {
			if (m_capability[name].asString() == value)
				json_str = "{\"support\":1}";
		}
	} else {
		json_str = "{\"Error\":\"unknown request\"}";
	}

	if (json_str != "")
		sendresponse(s, 200, prequest, json_str);

#if defined(__WIN32__) || defined(__WIN64__)
	cout << "server response is:" << json_str << endl;
#else
	if (g_show_log)
		DBG_ONLY("server response is:" << json_str);
#endif
}