Website Feedback
Closed Threads
IRC Web ChatTeamSpeak 3 (86 users) Active: 9044 users | |
|
Dephy Lithuania. August 10 2012 19:19. Posts 163 | Profile # |
| i wanted to ask how to make stream link appear and dissapear from the list depeding if its on or off, like in TL? is there some short workaround? it shouldnt be hard? Last edit: 2012-08-10 19:19:42 |
|
|
| EscPlan9 United States. August 12 2012 13:22. Posts 2676 | Profile Blog # |
Without using any modules in Perl, I need to create a script that recursively traverses the directory and then output the total size of all the files and a few other statistics like the last modified file, etc. I have the recursion part somewhat down correctly in that everytime a directory is found the subroutine is called again to traverse that next directory, but the problem is it doesn't backtrack to the parent directories.
The pertinent section is below:
sub scanDir { my $curDir = shift; chdir($curDir) or die "Unable to change to dir $curDir: $!\n"; opendir DIR, "." or die "Unable to open $curDir: $!\n"; my @contents = readdir(DIR) or die "Unable to read $curDir: $!\n"; closedir(DIR); for (@contents) { # ignore the "." and ".." listings next if ($_ eq "." || $_ eq ".."); if (-d "$_") { evaluateFile($_); print "$_ is a dir of size " . $fileSize . " descending into...\n"; scanDir($_); } elsif (-f "$_") { evaluateFile($_); print "$_ is a file, size = " . $fileSize . "\n"; } } }
I'm trying to think of ways to make sure it runs through every subdirectory without any repetitions. The problem right now is once it finds a directory, it jumps into it and scans all the files and directories there, and it keeps doing that until no more directories are found. This means that in a directory with multiple subdirectories, only the first subdirectory is traversed through. It never returns through the other subdirectories.
From google searches I always see that people recommend using modules for this. My teacher wants us to tackle this problem without using modules though.
One possible method I can think of is creating a hash structure of Absolute-path-of-dir : Boolean (traversed or not). I would create new keys when new directories are found and update the values for the directories that I have traversed until all the keys -> values evaluate to True. While I think this might work, it'll be tricky and I feel like there must be a much simpler method I am missing. Any ideas? |
| | Undefeated TL Tecmo Super Bowl League Champion |
|

|
| supereddie Netherlands. August 12 2012 17:07. Posts 77 | Profile # |
| $_ is a global, so you'll probably run into weird stuff if you depend too much on it. Also, $fileSize seems to be global as well. Why don't you use it as a return from evaluateFile? Lastly, do you want the total size of a directory including subdirectorys? In that case you'll want to return the total size as well in scanDir. |
| | "Do not try to make difficult things possible, but make simple things simple." - David Platt on Software Design |
|
|
| EscPlan9 United States. August 12 2012 23:11. Posts 2676 | Profile Blog # |
Yeah I cut off some of the code (like the evaluateFile subroutine). Yes, $fileSize is a global. You're right I can just use it as a return from evaluateFile. And finally, yes, the size of the directory and subdirectories themselves must be included.
I'm not sure what you mean by $_ is a global here. If I understand Perl's scope correctly, $_ scope is only within the for loop and gets assigned the value of the current element iterated through within the @contents array (listing of the directory). |
| | Undefeated TL Tecmo Super Bowl League Champion |
|
|
| EscPlan9 United States. August 12 2012 23:43. Posts 2676 | Profile Blog # |
I figured it out. As I was thinking about possible problems with $_ (thanks for the suggestions supereddie), I decided to try passing the absolute path as the parameter this time rather than just the current path I was in (although I have only found out how to grab the absolute path by using the CWD module in Perl). I also took out the chdir line.
The changed portion:
sub scanDir { my $curDir = shift; opendir DIR, $curDir or die "Unable to open $curDir: $!\n"; my @contents = readdir(DIR) or die "Unable to read $curDir: $!\n"; closedir(DIR); for (@contents) { # ignore the "." and ".." listings next if ($_ eq "." || $_ eq ".."); if (-d "$curDir/$_") { my $abs_path = abs_path("$curDir/$_"); my $dirSize = evaluateFile("$abs_path"); print "$abs_path" . " is a dir of size " . $dirSize . " descending into...\n"; scanDir("$abs_path"); } elsif (-f "$curDir/$_") { my $fileSize = evaluateFile("$curDir/$_"); print "$_" . " is a file, size = " . $fileSize . "\n"; } } }
And if curious on the other subroutines and how it all comes together, full script in spoiler + Show Spoiler + #!/usr/bin/perl # dirwalk.pl
use strict; use warnings; use Cwd 'abs_path'; # module for Current Working Directory, evaluates absolute path
my $totalSize = 0; my $lastModName = ""; my $lastModTime = 0;
# update totalSize, check for last modified and update if necessary sub evaluateFile { my $fileName = $_[0]; my @info = stat($fileName); my $bytes = $info[7]; $totalSize += $bytes; my $epochModTime = $info[9]; if ($epochModTime > $lastModTime) { $lastModTime = $epochModTime; $lastModName = substr($fileName, rindex($fileName,"/")+1); } return $bytes; }
# open the directory and scan all the contents sub scanDir { my $curDir = shift; opendir DIR, $curDir or die "Unable to open $curDir: $!\n"; my @contents = readdir(DIR) or die "Unable to read $curDir: $!\n"; closedir(DIR); for (@contents) { # ignore the "." and ".." listings next if ($_ eq "." || $_ eq ".."); if (-d "$curDir/$_") { my $abs_path = abs_path("$curDir/$_"); my $dirSize = evaluateFile("$abs_path"); print "$abs_path" . " is a dir of size " . $dirSize . " descending into...\n"; scanDir("$abs_path"); } elsif (-f "$curDir/$_") { my $fileSize = evaluateFile("$curDir/$_"); print "$_" . " is a file, size = " . $fileSize . "\n"; } } }
# validate parameter is a readable dir and only 1 argument passed if ( !defined($ARGV[0]) || scalar @ARGV > 1 ) { print STDERR "Enter a directory\n"; exit 1; } elsif ( (!(-d $ARGV[0])) || (!(-r $ARGV[0])) ) { print STDERR "Enter a readable directory\n"; exit 1; }
scanDir($ARGV[0]);
print "Total size is " . $totalSize . " Bytes\n"; print "Last file modified was: " . $lastModName . "\n"; print "Last mod was " . $lastModTime . "\n"; print scalar localtime($lastModTime); # easy conversion, but does not list timezone EDT
Last edit: 2012-08-13 00:09:25 |
| | Undefeated TL Tecmo Super Bowl League Champion |
|

|
| supereddie Netherlands. August 13 2012 01:59. Posts 77 | Profile # |
Personall I would count the totalSize within the scanDir function and return it as a value. Something like:
sub scanDir { .... my $totalSize = 0; .... for ..... $totalSize += scanDir if isDir; $totalSize += evaluateFile if isFile; .... print "total size (incl. subfolders) of dir " . $dirName . " = " . $totalSize . " bytes"; return $totalSize; }
.... print "Total size is " . scanDir($ARGV[0]) . " Bytes\n";
I feel like it would be easier to maintain/expand later on if needed.Last edit: 2012-08-13 02:00:19 |
| | "Do not try to make difficult things possible, but make simple things simple." - David Platt on Software Design |
|
|
| EscPlan9 United States. August 13 2012 02:08. Posts 2676 | Profile Blog # |
| My professor is really specific with the formatting and instructions. He would mark off if I did something like that, though I understand what you're getting at. My output exactly matches his required now and I've submitted the script in the dropbox. Thanks for all the help! |
| | Undefeated TL Tecmo Super Bowl League Champion |
|
|
| SiPa Germany. August 13 2012 20:45. Posts 125 | Profile # |
Quick Question: I have a few lines of code, written in C++. I have to write code, that does the same stuff in C. the C++-code contains vectors (std::vector<>) It uses the push_back-function and clear-function of the vector. Is there an equivalent to the C++-vector in C? (Array with variable size?) Google pretty much tells me to make my own thing.
|
|
|
| Morfildur Germany. August 13 2012 20:53. Posts 3534 | Profile Blog # |
On August 13 2012 20:45 SiPa wrote: Quick Question: I have a few lines of code, written in C++. I have to write code, that does the same stuff in C. the C++-code contains vectors (std::vector<>) It uses the push_back-function and clear-function of the vector. Is there an equivalent to the C++-vector in C? (Array with variable size?) Google pretty much tells me to make my own thing.
There is no equivalent. You can make your own implementation of a variable size array, though depending on the actual use, it might actually be easier to go with a linked list instead.
|
| | "Remember kids, the 3 most important things for becoming a good player: Micro, Macro and always take your Dailies!" - Rastaban |
|
|
| heishe Germany. August 13 2012 20:59. Posts 2135 | Profile Blog # |
On August 13 2012 20:45 SiPa wrote: Quick Question: I have a few lines of code, written in C++. I have to write code, that does the same stuff in C. the C++-code contains vectors (std::vector<>) It uses the push_back-function and clear-function of the vector. Is there an equivalent to the C++-vector in C? (Array with variable size?) Google pretty much tells me to make my own thing.
There's not, but things without inheritance are convertible to C in a pretty straightforward manner (and inheritance is easy, too, just a bit bothersome), with some handicaps. But templates just won't work, you flat out won't be able to get that in C. So if you want multiple types to be handled by that vector, you either have to implement it multiple times each time for one certain type, or you have to store void* in the pointer and cast ("with certainty") around to get the types into and out of that you want. The rest works like this:
If you have some class with some method, e.g.
class vector { attribute a; attribute b; attribute c; public: //... size_t size() { //... } }
it can be done by creating a struct vector and a free method size(vector* vec);
struct vector { attribute a; attribute b; attribute c; }
size_t size(vector *vec) { //... }
Similarly, methods with parameters would transform from return_type class::method(a,b,c) to return_type method(class*,a,b,c) (this is actually exactly like it looks in assembly code, without optimizations). Constructors can be implemented by simply making a free method Constructor[yourstructname](struct *str) or something.
Of course, usage changes too, from classobject.method(params); to method(&classobject,params);
Converting the vector class like this should be trivial, since you can look up its source code of whatever compiler/IDE you're using.
However, you can not recreate other built-in features to C++ like RAII, unfortunately. Last edit: 2012-08-13 21:05:12 |
| | If you value your soul, never look into the eye of a horse. Your soul will forever be lost in the void of the horse. |
|

|
| waxypants United States. August 14 2012 11:05. Posts 450 | Profile Blog # |
On August 13 2012 20:45 SiPa wrote: Quick Question: I have a few lines of code, written in C++. I have to write code, that does the same stuff in C. the C++-code contains vectors (std::vector<>) It uses the push_back-function and clear-function of the vector. Is there an equivalent to the C++-vector in C? (Array with variable size?) Google pretty much tells me to make my own thing.
Not in standard C. I like to use glib for generic data structures among other things.
http://developer.gnome.org/glib/stable/glib-data-types.html http://developer.gnome.org/glib/stable/Last edit: 2012-08-14 11:11:39 |
|
|
| Olsson Sweden. August 15 2012 21:41. Posts 929 | Profile # |
| Does anyone know a good website to learn C#? You guys got me interested, I tried C++ abit but I think it was too much like most of you guys told me. |
| |
|
| bo1b August 16 2012 04:02. Posts 1982 | Profile # |
| I just noticed a new edition of c++ primer was released, does anyone think it would be a good idea to buy it or are they're better resources for learning c++? |
|
|
| IreScath Canada. August 16 2012 05:38. Posts 513 | Profile # |
| edit: nvm... I figured it out. Last edit: 2012-08-16 05:53:55 |
| |
|
tofucake United States. August 16 2012 05:40. Posts 11646 | Profile Blog # |
I'm sure this doesn't really help you at all but personally I hate handbrake.
...yeah |
| | kwark > i will mentor you if you wish, but i'm like the absent drunk father you never wanted | |
|
|
| IreScath Canada. August 16 2012 05:53. Posts 513 | Profile # |
On August 16 2012 05:40 tofucake wrote: I'm sure this doesn't really help you at all but personally I hate handbrake.
...yeah
open scource + opencl = yay.
but I think I go it now.  |
| |
|
| SiPa Germany. August 16 2012 18:02. Posts 125 | Profile # |
GLib is awesome! Now some questions regarding GLib: I get tons of "expression must be an lvalue or a function designator" and "a nonstatic member reference must be relative to a specific object" errors. I get, that this isnt necessarily connected to GLib, so i will just give you the code:
struct trajectory{ GArray *position1; GArray *position2; GArray *velocity1; GArray *velocity2; position1 = g_array_new(FALSE, FALSE, sizeof (double)); velocity1 = g_array_new(FALSE, FALSE, sizeof (double)); position2 = g_array_new(FALSE, FALSE, sizeof (GArray)); velocity2 = g_array_new(FALSE, FALSE, sizeof (GArray)); _Fragment fragment; };
GArray *startVelocity; startVelocity = g_array_new(FALSE, FALSE, sizeof(double)); g_array_append_val(startVelocity, g_array_index(iDir, double, 0) * iFragment.Speed); g_array_append_val(startVelocity, g_array_index(iDir, double, 1) * iFragment.Speed); g_array_append_val(startVelocity, g_array_index(iDir, double, 2) * iFragment.Speed);
g_array_free(trajectory.velocity2, TRUE); g_array_append_val(trajectory.velocity2, startVelocity); g_array_free(trajectory.position2, TRUE); g_array_append_val(trajectory.position2, iPos);
iPos and iDir are pointers to GArrays.
trajectory calcTrajectory( _Fragment iFragment, GArray *iPos, GArray *iDir ){}
Errors are at g_array_append_val, velocity2, position2
I hope some1 could provide help, thx in advance!
Edit: I also would love to know how to get the size of a GArray, meaning how many Elements it contains. Last edit: 2012-08-16 18:11:28 |
|

|
| waxypants United States. August 17 2012 12:18. Posts 450 | Profile Blog # |
Don't have time to figure out what you are trying to do right now, but I can tell you:
1)position1 = g_array_new(FALSE, FALSE, sizeof (double)); In C, you can't have member initialization inside the struct definition.
2)trajectory.velocity2 In C, you can't have static struct members. |
|
|
| waxypants United States. August 17 2012 12:50. Posts 450 | Profile Blog # |
This is a response to a PM about where to get Glib. I put installation instructions for Visual Studio. These instructions are actually pretty much the same for most of the "open source" projects you may want to download and use with Visual Studio. Let me know if it doesn't work and I will try to fix my instructions if I left something out or something.
You can build it yourself (enormous pain), or get it from here:
http://www.gtk.org/download/win32.php
Not the newest version, but fairly recent. You may be able to find a newer version elsewhere but this one has worked well enough for me.
I'm assuming you are using Visual Studio. In that case you will need to do the following:
To build your program: 1) Download the Glib "Dev". 2) Extract it to something like C:\glib-dev_2.28.8-1_win32 (or whatever directory you want) 3) In your project properties, go to Configuration Properties->C/C++->General and add "C:\glib-dev_2.28.8-1_win32\lib\glib-2.0\include" and "C:\glib-dev_2.28.8-1_win32\include\glib-2.0" to the list of Additional Include Directories. 4) In your project properties, go to Configuration Properties->Linker->General and add "C:\glib-dev_2.28.8-1_win32\lib" to your list of Additional Library Directories. 5) In your project properties, go to Configuration Properties->Linker->Input and add "glib-2.0.lib" to your list of Additional Dependencies.
For steps 3 and 5 you may need to add other directories and/or other lib files to the list, but these things have been enough for what I have done. You will know what you need to add whenever it complains that it can't find "blahblah.h" or "blahblah.lib".
To run your program: 1) Download the Glib "Run-time" 2) Extract whatever files you need from the bin directory into the same directory as your program's exe file. You can add one at a time until your program is able to run, or you can just copy all of them if you don't care about having extra unnecessary files. In theory, I think could also put them in C:\Windows\System32 if you want, and then you won't have to copy them to all of your projects, but if somebody else wants to run your program they will need to download the necessary dll's.
Last edit: 2012-08-17 12:52:30 |
|

|
| waxypants United States. August 17 2012 20:23. Posts 450 | Profile Blog # |
The lvalue error is because the second argument in g_array_append_val has to have an address (in other words it has to be a variable). It's because it seems that they have defined g_array_append_val as:
#define g_array_append_val(a,v) g_array_append_vals (a, &(v), 1)
The &(v) is what is throwing the error. Anyway it sucks, but you can do something like:
double speed;
speed = g_array_index(iDir, double, 0) * iFragment.Speed; g_array_append_val(startVelocity, speed); speed = g_array_index(iDir, double, 1) * iFragment.Speed; g_array_append_val(startVelocity, speed); speed = g_array_index(iDir, double, 2) * iFragment.Speed; g_array_append_val(startVelocity, speed);
Or even:
double speeds[3];
speeds[0] = g_array_index(iDir, double, 0) * iFragment.Speed; speeds[1] = g_array_index(iDir, double, 1) * iFragment.Speed; speeds[2] = g_array_index(iDir, double, 2) * iFragment.Speed; g_array_append_vals(startVelocity, speeds, 3); |
|
|
| Prev 1 2 3 4 5 6 7 8 9 10 100 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 200 300 303 304 305 306 307 308 309 310 311 312 313 Next |
|
|
|
|
|
|
| |
|
Sidebar Settings...

|