Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

furtheraway posted:

I understand that we're supposed to keep language specific questions in their related threads, but I don't see any current threads on shell scripting or sed. If I'm wrong, feel free to ridicule me.

With that out of the way, my question is about using regular expressions with sed. I'm searching a line of numbers and symbols and only want a certain group of numbers. My input string will look like this:
code:
16(100%)

It might be simpler (and possibly faster) to just 'cut' twice instead of sed:

code:
[jacob@tw-133]$ echo '16(100%)'|cut -d'(' -f2|cut -d'%' -f1
100
[jacob@tw-133]$ 

Adbot
ADBOT LOVES YOU

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

axolotl farmer posted:

I'm making a bash script where I need to echo $VARIABLE number of something on a line, let's say Xs.

Like this, the unknown command is [?]:

alice=5
bob=7
echo "Alice [?]"
echo "Bob [?]"


...and get this output:

Alice XXXXX
Bob XXXXXXX

I totally misread this and thought that you wanted to print the number of characters in a string. Which is actually awesome because I learned how to find out the length of a string in bash (it's ${#var_name} btw).

All that you need to do is
code:
alice=5
for(( i = 0; i < alice; i++ ))
do
   echo -n "X"
done
echo

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
What is the most efficient way to find the location in an integer of a specific byte value? For instance, suppose that I have an integer of 0x43001928, how do I find the byte position of the value 0x00? Obviously I could just cast to a char pointer and walk the bytes, but it seems like there is a faster (no matter how marginally faster) way to do this.

Specifically I have a large buffer from which I need to find strings of bytes in a specific range (pretend that range is 0x00-0x3f). A comparison of every byte is easy but non-optimal so I'm casting to an unsigned integer pointer, allowing me to look at 4 bytes in one instruction. However, I need to know if the last 1, 2, or 3 bytes in that integer are in my approved range, otherwise I'll skip over them when I increment my pointer. That sounds confusing, so assume the following (and ignore endianness as a concern for now):

code:
unsigned char *my_buf, *puc;
unsigned char *pui;

...Read bytes into my_buf...

puc = my_buf;
pui = (unsigned int *)puc;
Will result in this
code:
[byte#] 0     1     2     3     4     5     6     7     8
        puc
        |
        v
my_buf [\x64, \xde, \x34, \x54, \xff, \x32, \x12, \x39, \x12 ...]
        ^                     ^
        |                     |
        pui                   |
        |_____________________|
At this point (*pui & 0xc0c0c0c0) will equal 0x40c000c0. I need an operation that will let me know that byte #2 is 0x00 (which lets me know that the byte in that position in buffer is <=0x3f). I also need to know (hopefully in the same operation) that byte #2 has no adjacent bytes that are in my desired range (remember, I'm looking for series of bytes inside a specific range). I would then know that I need to bump puc up sizeof(unsigned int) bytes which will result in:

code:
[byte#] 0     1     2     3     4     5     6     7     8
                                puc
                                |
                                v
my_buf [\x64, \xde, \x34, \x54, \xff, \x32, \x12, \x39, \x12 ...]
                                ^                     ^
                                |                     |
                                pui                   |
                                |_____________________|
This time (*pui & 0xc0c0c0c0) gives me 0xc0000000 meaning that I've found the beginning of a series of desirable bytes, so I need to bump puc only 1 byte for my next comparison.

Is there a mathematical or logical operation that I'm missing here? I don't mind inlining IA86 asm if necessary. I considered the following:

code:
val = *pui & 0xc0c0c0c0;
if(val == 0)
   goto good_series;

if((~val & 0x00ffffff)) == 0x00ffffff)
   puc++;
else if((~val & 0x0000ffff)) == 0x0000ffff)
   puc += 2;
else if((~val & 0x000000ff)) == 0x000000ff)
   puc += 3;
else
   puc += 4;
but I don't think that buys me much over walking the buffer a byte at a time.

Suggestions? Does any of this make any sense at all?

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Alan Greenspan posted:

Undocumented source code is available

This rules, thanks.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

black candy posted:

The performance concern here is that your method appears to require misaligned loads from puc. I don't know the details for x86, but most hardware hates this. You may want to consider making only aligned loads to keep 64 bits of the buffer in memory, search for the sequence within those bits, and if not found slide the 'window' forward by 32 bits.

Well I'll be damned. I always assumed that because data didn't have to be aligned (and since x86 doesn't even have standard length opcodes) that it didn't matter. But I just found this:

Intel Architecture Software Developer’s Manual Volume 1: Basic Architecture posted:

Words, doublewords, and quadwords do not need to be aligned in memory on natural boundaries...However, to improve the performance of programs, data structures (especially stacks) should be aligned on natural boundaries whenever possible. The reason for this is that the processor requires two memory accesses to make an unaligned memory access; whereas, aligned accesses require only one memory access. A word or doubleword operand that crosses a 4-byte boundary or a quadword operand that crosses an 8-byte boundary is considered unaligned and requires two separate memory bus cycles to access it; a word that starts on an odd
address but does not cross a word boundary is considered aligned and can still be accessed in one bus cycle.

Just to think out loud here- I can keep puc pointing to the beginning of what appears to be a series and only cast pui onto 4-byte boundaries, correct? This will complicate things a little, as the series of interest are of a fixed size so my AND masks will have to be variable when checking the series end. But that's not a big deal.

God I love it when I get direct, usable information about questions. Thanks guys.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Dauq posted:

If you say you're looking into asm as well, the x86 asm code has instructions specifically for finding bytes in strings.
You should look at REP SCANSB for your task, something like this maybe:

I thought about this, but the byte of interest will be part of an integer (and in a register already), not a string of characters.

On a related note, does anyone know if Microsoft's implementation of things like strlen() and strchr() make use of the REP SCAN instructions? I suppose that if I wasn't so lazy I'd just open in up in IDA right now. But I am.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

hey wiz posted:

If he used an on change event, why not deselect all other check boxes before selecting the current one?

Because that's really confusing for the user.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

clockwork automaton posted:

Actually, that's perfectly fine. The way I figure it there is an easier way to do what I am doing, I'm just being stubborn and not seeing it.

Can't you just do strchr('\n') (or strstr("\r\n") on windows)?




Ohhhhhhhhh. I misread. Then, yes, seconding the idea that you may be approaching the problem wrong.
vvvvvvvvvvvvvvv

Plastic Jesus fucked around with this message at 17:50 on Apr 9, 2008

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

clockwork automaton posted:

:D It's okay though, because I figured it out. It was an issue with user space memory and kernel space memory all I had to do was use strcpy and it was saved.

I've responded to you here.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Factor Mystic posted:

This is kind of a programming related question: Is there any way to identify the function called in a compiled module? Example, given this output from Process Monitor, can I find out what the functions are?

Do you want to know which functions an application is using in a DLL? dumpbin (which comes with Visual Studio) will dump imports for you. If you don't have VS installed, periscope can do the same thing.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

bitprophet posted:

Given that the X11 terminal looks and behaves like complete rear end, there's no reason to use it unless you're just using it to launch a GTK app like e.g. Wireshark.

alias wireshark='open -a /Applications/Wireshark.app'

There is no reason to use xterm/iterm/anydamnedterm on OS X.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

GT_Onizuka posted:

Except for, you know, using terminal applications.

I meant that there's no reason to use anything other than Terminal.app

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
Can anyone recommend a spell checker for source code? I'll need it mostly for checking C sources, but hopefully there's something multi-language.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Mustach posted:

Just use realloc()
code:
size_t len = strlen(p->messageid) + sprintf(buff, "%d", pagecount++);
char* messageid = realloc(p->messageid, len+1);
if(!messageid) oops();
p->messageid = strcat(messageid, buff);
edit: made it warning-free and less lines to boot

You can't re-use a pointer passed to realloc (that's why it returns a pointer) and your call to sprintf() could still overflow buff.

Also, this is very silly stuff. The dude asking the question isn't splitting the atom, he's creating a string. I have no idea why this is so confusing to people, but it scares the hell out of me to think that people get such basic things wrong.

code:
#define MAX_UINT32_AS_STR_LEN      strlen("4294967295") + 1
...
char buff[MAX_UINT32_AS_STR_LEN]; 
...

if(p == NULL)
    return;

if(p->messageid == NULL)
    return;

snprintf(buff, "%d", MAX_UINT32_AS_STR_LEN, pagecount++);
strncat(p->messageid, buff, (MAX_MESSAGEID_LEN - 1));
You didn't talk about the format of messageid so I have no idea what MAX_MESSAGEID_LEN would be, but strncat() will append the specified number of character then append a NULL. This is different from snprintf() which expects the length specifier to include the NULL terminator.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
What are good papers/books that offer an introduction to financial market analysis algorithms? I'm mostly interested in analyzing historical trends, but I know absolutely nothing about this area of research and will happily read anything pertinent to the field.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

The B Man posted:

Check out Fama 1970 and Fama 91, they're both an overview of market efficiency which will basically tell you that technical analysis (ie some sort of algorithm) has not and will not yield profit. I'm not sure exactly what you're looking for but you can't do any sort of futuristic prediction. If you want to analyse past data you're be better off looking at statistical methods for doing so.

No, I'm not trying to open my own quant shop. I'm mostly looking to learn about software and mechanisms are used by analysts to evaluate markets and trends. I'm also specifically wanting to analyze interactions/dependencies between index fund components, in particular when these dependencies contradict overall market performance.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

tripwire posted:

In c, how should I be declaring strings, and when should I make them dynamically allocated? How should I return strings from functions? Which of the following makes sense inside a function:
char * my_string;
char my_string[100];
char * my_string = (char *) xmalloc(100);

If my inner function returns a pointer to a const string do I have to worry about garbage collection? What if this function gets called a million times, will it be making a million static strings in memory that stay forever?

C doesn't have garbage collection, so no worries there.

You can use a char array when you won't be using the string outside of the current function. You can pass a char array to another function to fill in, but you can't return a char array from a function. Also, the stack size is limited in compiler-specific ways. As a rule of thumb I don't have more than 512 bytes worth of local variables (the actual limit is something like 3k for VS8 but I'm weird). Finally, you can't use char arrays when you don't know the required buffer size at compile-time.

Use heap-based buffers when you don't know how large of a buffer you'll need until run-time, you need large buffers or if you need the string to persist outside of function scope (e.g. when you need to return a string).

You will very rarely want to declare a local string static.

Finally, you shouldn't cast with malloc() in c.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

tripwire posted:

Thanks for the helpful responses everyone. One thing I want my progrma to do is store a file into a buffer on the heap, and my difficulty was not knowing the size of the file beforehand.

Use fseek() to move to the end of the file, then ftell() to get the size of the file. Then just malloc(), fseek() back to the beginning of the file, and read it all in one shot.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

ante posted:

I'm writing a program that injects a Dll into another process in C. I've got the injector and the dll working fine, I just can't actually figure out how to call the dll's functions. Considering the way that most hits on google gloss over this point, I get the impression that it's really easy using LoadLibrary or GetModuleHandle or something.

What are you trying to do after injection? Are you attempting to hook calls to another DLL or just execute on your own code? You can't really force a process to call your DLL's functions (well, except DllMain()).

Adbot
ADBOT LOVES YOU

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

ante posted:

I'm trying to pass variables to a function in my Dll. One way or another I need to get my program and the injected Dll communicating.

There isn't any real way to force the host process to execute anything in your DLL (outside of hooking). You get a chance to do whatever you want in your DllMain(), but after that your DLL won't be doing anything unless explicitly called by the host process.

I guess that I still don't understand precisely what you're trying to do.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply