Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Guildorn Tanaleth

« Newer Snippets
Older Snippets »
Showing 11-19 of 19 total

String equality tester

If you want to test the equality of two strings and don't want the overhead of strcmp(), then this is the function for you.
_Bool strequals(char* a, char* b) {
 if (!a || !b) return 0;
 do {if (*a != *b) return 0; } while (*a++ && *b++);
 return 1;
}

"Unget string" function

As a complement to ungetc(), this C function pushes a string back onto an input stream, character by character. It returns the number of characters pushed, or -1 if an error occurred.
void ungets(char* str, FILE* stream) {
 if (!str || !file) return -1;
 size_t len = strlen(str);
 for (int i=len-1; i>=0; i--) if (ungetc(str[i], stream) == EOF) return -1;
 return len;
}

Dice roller

Given a di(c)e roll in standard d20 format on the command line, this script will roll the dice & output the result.
#!/usr/bin/perl -wl
use strict;
$_ = shift or die 'Invalid argument passed';
s/\s+//g;
/^(\d+)[dD](\d+)([+-]\d+)?$/ or die 'Invalid argument passed';
my $sum = 0;
$sum += 1 + int rand $2 for 1..$1;
$sum += $3 if defined $3;
print $sum;

Hexadecimal-to-text converter

This script converts all 2-digit hexadecimal numbers (without 0x's) in the standard input into text, though there's probably a better way to do this.
#!/usr/bin/perl -wn
use strict;
s/\s//g;
print chr hex $1 while /([[:xdigit:]]{2})/g;
print "\n";

File extension counter

Produces a count of the frequencies of each file extension in the directories named on the command line
#!/usr/bin/perl -w
use strict;
my %exten;
foreach (@ARGV) {
 /(\.[^.]+)$/ && $exten{$1}++ foreach glob "$_/*.*"
}
print "$_: $exten{$_}\n" foreach sort keys %exten;

Binary-to-text converter

Converts all 8-bit binary numbers in the standard input into text, though there's probably a better way to do this
#!/usr/bin/perl -wn
use strict;
s/\s//g;
print chr oct "0b$1" while /([01]{8})/g;
print "\n";

Prime number generator

This program will calculate & list the first n primes, where n is a number specified on the command line or (if none is specified) 100. n is only limited by the size of ints on your platform.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char** argv) {
 int qty = (argc > 1) ? (int) strtol(argv[1], NULL, 10) : 100;
 if (qty < 3) qty = 100;
 unsigned int primes[qty];
 primes[0] = 2U; primes[1] = 3U;
 printf("2\n3\n");
 for (int i=2; i<qty; i++) {
  int j = primes[i-1];
  iter: j += 2U;
  unsigned bound = (unsigned) sqrt((double) j);
  for (int k=1; k<i; k++) {
   if (primes[k] > bound) break; /*Not a viable shortcut for small quantities*/
   if (!(j % primes[k])) goto iter;
  }
  primes[i] = j;
  printf("%u\n", j);
 }
 return 0;
}

Server name/IP converter

When passed either an IPv4 address or the name of a domain or server, this script will return either a name or an IP, respectively.
#!/usr/bin/perl -w
use strict;
use Socket;
my $arg = shift;
if ($arg =~ /^(\d+\.){3}\d+$/) {
 print scalar gethostbyaddr(inet_aton($arg), AF_INET), "\n"
} else { printf "%vd\n", scalar gethostbyname $arg }

Paragraph formatter

Converts any nonempty lines in a document that don't start with whitespace into more conventional paragraphs
#!/usr/bin/perl -wni
# para.pl
use strict;

/^\s+/ || /^$/ ? print : write;
format ARGVOUT =
        ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$_
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
$_
.
« Newer Snippets
Older Snippets »
Showing 11-19 of 19 total