posix

Convert Date-Time to Milliseconds - C++ - cross platform

孤街浪徒 提交于 2019-12-13 14:07:45
问题 I want to convert a string in the format of "20160907-05:00:54.123" into milliseconds. I know that strptime is not available in Windows and I want to run my program in both windows and linux. I can't use third party libraries as well. I can tokenize the string and convert it. But is there a more elegant way like using the strptime to do so? 回答1: Given the format of your string, it is fairly easy to parse it as follows (although a regex or get_time might be more elegant): tm t; t.tm_year =

TCP Dead link detection

你离开我真会死。 提交于 2019-12-13 13:17:13
问题 How can i detect a dead link in a TCP Connection? Dead Link occurs when someone pulls the network cable, Shut down the Network Interface, turns of WiFi or, as in my case... unplugs the power supply. Keep-alive only works for the client but not for the server. The Server is just receiving data, never sending so using TCP_USER_TIMEOUT will not work cause the send q will always be empty. I don't want to implement an Application Layer protocol cause of the speed loss. (no zero copy anymore) One

Differences between empty and blank lines in regexps

心不动则不痛 提交于 2019-12-13 12:21:31
问题 There are already several good discussions of regular expressions and empty lines on SO. I'll remove this question if it is a duplicate. Can anyone explain why this script outputs 5 3 4 5 4 3 instead of 4 3 4 4 4 3 ? When I run it in the debugger $blank and $classyblank stay at "4" (which I assume is the correct value) until the just before the print statement. my ( $blank, $nonblank, $non_nonblank, $classyblank, $classyspace, $blanketyblank ) = 0 ; while (<DATA>) { $blank++ if /\p{IsBlank}/

How to get name of frontmost app with AppleScript and use it to get the filepath

时间秒杀一切 提交于 2019-12-13 11:40:31
问题 What I try to do: When I'm in one of my text editors (TextEdit, Byword, FoldingText) I want this AppleScript to display the file path. I figured asking for the frontmost window app get's me the apps name nice and easily and then I can ask for the POSIX path in the next step. The Problem: The script is already 99% there, but I'm missing something. When I try to use the variable of activeApp it doesn't work and I get this error: Error Number:System Events got an error: Can’t get application {

Listing only folders in directory

自作多情 提交于 2019-12-13 11:37:52
问题 I want to list folders in a directory in C++, ideally in a portable (working in the major Operating Systems) way. I tried using POSIX, and it works correctly, but how can i identify whether the found item is a folder? 回答1: Using the C++17 std::filesystem library: std::vector<std::string> get_directories(const std::string& s) { std::vector<std::string> r; for(auto& p : std::filesystem::recursive_directory_iterator(s)) if (p.is_directory()) r.push_back(p.path().string()); return r; } 回答2: You

Which system calls can return EINTR or EAGAIN error codes? [closed]

若如初见. 提交于 2019-12-13 11:29:52
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Which of the following system calls can return EINTR or EAGAIN/EWOULDBLOCK? getsockname() chdir() bind() fcntl() listen() setsid() setsockopt() socket() stat() unlink() access() accept() open() usleep() dup2() fork() waitpid() wait() read() write() Some of these system calls

What type should be used to loop through an array? [duplicate]

好久不见. 提交于 2019-12-13 10:14:47
问题 This question already has answers here : What is the correct type for array indexes in C? (9 answers) For iterating though an array should we be using size_t or ptrdiff_t? (3 answers) Closed 9 months ago . Let's have this array: char arr[SIZE_MAX]; And I want to loop through it (and one past its last element): char *x; for (i = 0; i <= sizeof(arr); i++) x = &arr[i]; (Edited to add some valid code with pointers). What is the most adequate type for i ? I accept non-standard types such as POSIX

How are signals handled in Unix?

萝らか妹 提交于 2019-12-13 09:54:30
问题 My question is that how are signals handled in Unix. Are they handled by making new thread or there is something else? Also what is the flow of execution of programme when a signal comes? What I mean by flow of execution is that, let's say I am in middle of a function X and call to another function Y comes. So the compiler goes pauses the execution of function X and goes to function Y . After finishing the function Y the compiler again continues the execution of programme X from where it was

Copy file skipping first n lines and last m lines

匆匆过客 提交于 2019-12-13 07:15:01
问题 I want to copy a file with skipping first n of its lines and last m lines using open, read, write and lseek (eg. n = 1, m = 2, source file: AAAAAAA BBBBBBB CCCCCCC DDDDDDD dest file: BBBBBBB ) I know how to copy a file but don't know how to skip the lines. Here is my code for copy: char buf[128]; size_t size; int source = open(argv[1], O_RDONLY); int dest = open(argv[2], O_CREAT | O_APPEND | O_WRONLY); if(source == -1) { printf("error"); return; } if(dest == -1) { printf("error"); return; }

Is the bug in the grammar or in the code?

时光怂恿深爱的人放手 提交于 2019-12-13 07:14:11
问题 I'm not sure if this grammar is correct for a shell command language that should also be able to execute single-quotes and double-quotes. It seems that non-trivial commands work e.g. ls -al | sort | wc -l but the simple one does not work with single-quotes: echo 'foo bar' does not work. %{ #include "shellparser.h" %} %option reentrant %option noyywrap %x SINGLE_QUOTED %x DOUBLE_QUOTED %% "|" { return PIPE; } [ \t\r] { } [\n] { return EOL; } [a-zA-Z0-9_\.\-]+ { return FILENAME; } ['] { BEGIN